Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(embedded): shrink, no_key and structured-output memory are per engine; cached query vectors go at close (B3a E)

garrytana27ff25aa186
diff --git a/src/core/ai/engine-gateway.ts b/src/core/ai/engine-gateway.ts
index dc3fe030d7..7459060e51 100644
--- a/src/core/ai/engine-gateway.ts
+++ b/src/core/ai/engine-gateway.ts
@@ -11,6 +11,7 @@ import { embeddedEngineInScope } from '../engine-scope.ts';
import { DEFAULT_EMBEDDING_DIMENSIONS, DEFAULT_EMBEDDING_MODEL } from './defaults.ts';
import { getRecipe } from './recipes/index.ts';
import type { AIGatewayConfig, Recipe } from './types.ts';
+import { dropQueryEmbeddingsTagged } from './query-embed-cache.ts';
/** Each native SDK reads its base URL from this env name when it is not given one, so an engine always gives one. */
const NATIVE_BASE_URL: Partial<Record<Recipe['implementation'], { env: string; url: string }>> = {
@@ -83,8 +84,24 @@ 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 }
+/**
+ * The config a gateway call uses, the clients built from it and what its calls taught the gateway. `providers` is set
+ * only on an embedded engine's state; the process keeps its own (gateway.ts), so one engine's lessons never reach another.
+ */
+export interface GatewayState {
+ config: AIGatewayConfig | null; modelCache: Map<string, any>; providers?: ReadonlySet<string>; cacheTag: string;
+ /** Batch shrink-on-miss factor by recipe id (gateway.ts effectiveSafetyFactor). */
+ shrink: Map<string, { factor: number; consecutiveSuccesses: number }>;
+ /** Reranker models whose missing key already wrote its one no_key audit row. */
+ noKeyNoticed: Set<string>;
+ /** Recipes that rejected json_schema at call time, so their structured calls go straight to the text path. */
+ structuredOutputRejected: Set<string>;
+}
+
+/** A gateway state that has learned nothing yet. */
+export function freshGatewayState(config: AIGatewayConfig | null, cacheTag: string, providers?: ReadonlySet<string>): GatewayState {
+ return { config, modelCache: new Map(), ...(providers ? { providers } : {}), cacheTag, shrink: new Map(), noKeyNoticed: new Set(), structuredOutputRejected: new Set() };
+}
/** Keyed by the settings the engine holds, so a disconnect (which drops them) drops the state; see releasedKeyEnv. */
const states = new WeakMap<EngineSettings, GatewayState>();
@@ -101,12 +118,19 @@ export function scopedGatewayState(normalize: (config: AIGatewayConfig) => AIGat
let state = states.get(settings);
if (!state) {
const { config, providers } = engineGatewayInput(settings);
- state = { config: normalize(config), modelCache: new Map(), providers, cacheTag: `engine:${crypto.randomUUID()}:` };
+ state = freshGatewayState(normalize(config), `engine:${crypto.randomUUID()}:`, providers);
states.set(settings, state);
}
return state;
}
+/** At an embedded engine's close, before its settings go: drop the query embeddings its calls cached. */
+export function releaseEngineGatewayState(engine: object): void {
+ const settings = embeddedSettingsRecord(engine);
+ const state = settings && states.get(settings);
+ if (state) dropQueryEmbeddingsTagged(state.cacheTag);
+}
+
/** Provider keys of a disconnected engine in scope, as env entries, so a call still in flight scrubs them from its error. */
export function releasedKeyEnv(): Record<string, string> {
const engine = embeddedEngineInScope();
diff --git a/src/core/ai/gateway.ts b/src/core/ai/gateway.ts
index 12c8ce0309..e498ed4051 100644
--- a/src/core/ai/gateway.ts
+++ b/src/core/ai/gateway.ts
@@ -65,7 +65,7 @@ import type { BrainEngine } from '../engine.ts';
import { dimsProviderOptions } from './dims.ts';
import { hasAnthropicKey, stashGatewayAnthropicKeyFromEnv } from './anthropic-key.ts';
import { unscopedInEmbeddedProcess, withEngineScope } from '../engine-scope.ts';
-import { engineScopeMissing, engineScopeRequired, releasedKeyEnv, scopedGatewayState, type GatewayState } from './engine-gateway.ts';
+import { engineScopeMissing, engineScopeRequired, freshGatewayState, releasedKeyEnv, scopedGatewayState, type GatewayState } from './engine-gateway.ts';
import { AIConfigError, AITransientError, isStructuredOutputRejection, isUnbilledEmbeddingRejection, normalizeAIError } from './errors.ts';
import { embeddingModelNotFoundHint, gatewayModelNotFoundHint } from './model-not-found-hint.ts';
import { fitMatryoshkaPrefix, isEmbeddingZeroNormError, screenAlignedEmbeddings, screenEmbeddings, sendableEmbeddingInputs } from './embedding-guard.ts';
@@ -140,9 +140,9 @@ const DEFAULT_CHAT_MODEL = 'anthropic:claude-sonnet-4-6';
// the mode bundles) — `voyage:rerank-3-lite` (was `voyage:rerank-2.5` from v0.48.2).
/** What `configureGateway` writes and every call reads, except a call in an embedded engine's scope (engine-gateway.ts). */
-const processState: GatewayState = { config: null, modelCache: new Map(), cacheTag: '' };
+const processState: GatewayState = freshGatewayState(null, '');
/** A call with no scope in a process holding open embedded engines: no config and no provider, so every read of it refuses or reads as unavailable. */
-const unscopedState: GatewayState = { config: null, modelCache: new Map(), providers: new Set(), cacheTag: '' };
+const unscopedState: GatewayState = freshGatewayState(null, '', new Set());
const gw = (): GatewayState => scopedGatewayState(gatewayConfigOf) ?? (unscopedInEmbeddedProcess() ? unscopedState : processState);
/** Every AI call starts here: with no scope in a process holding open embedded engines it refuses `engine_scope_required`. */
function refuseUnscoped(): void {
@@ -211,8 +211,8 @@ let _generateObjectTransport: GenerateObjectFn = sdkGenerateObject;
// reject json_schema at call time would otherwise pay the rejected attempt —
// and a pessimistic '.failed' budget record — on EVERY expand() call
// (persistent ~2x phantom overcount that can trip caps). Remember the
-// rejection per recipe for the process lifetime and go straight to viaText.
-const _structuredOutputRejectedRecipes = new Set<string>();
+// rejection per recipe for the gateway state's lifetime (the process, or one embedded
+// engine: GatewayState.structuredOutputRejected) and go straight to viaText.
// v0.41.6.0 D1: tests that install a transport stub also pass the
// embedding-creds preflight, matching the chat-transport fast-path
// pattern. Set when __setEmbedTransportForTests is called with a
@@ -228,14 +228,13 @@ let _chatTransport: ((opts: ChatOpts) => Promise<ChatResult>) | null = null;
* effective `safety_factor` so subsequent `embed()` calls pre-split smaller
* out of the gate. After 10 consecutive batch successes, the factor heals
* back toward the recipe default (×1.5 per heal, capped at the declared
- * `safety_factor`). Module-scoped because the gateway itself is module-scoped;
+ * `safety_factor`). Held on the gateway state (the process, or one embedded engine);
* `resetGateway()` and `configureGateway()` clear it.
*/
interface ShrinkEntry {
factor: number;
consecutiveSuccesses: number;
}
-const _shrinkState = new Map<string, ShrinkEntry>();
/** Floor for shrink-on-miss to prevent infinite shrinking. */
const SHRINK_FLOOR = 0.05;
@@ -477,10 +476,10 @@ export function configureGateway(config: AIGatewayConfig): void {
stashGatewayAnthropicKeyFromEnv(config.env ?? {}); // #2119: filter + rationale in anthropic-key.ts
processState.modelCache.clear();
clearQueryEmbedCache();
- _shrinkState.clear();
+ processState.shrink.clear();
// A (re)configure is a new env snapshot: a key that appeared or vanished
// since the last no_key audit row deserves a fresh once-per-process row.
- _noKeyNoticed.clear();
+ processState.noKeyNoticed.clear();
warnRecipesMissingBatchTokens();
installAICallLogFromEnv();
}
@@ -657,7 +656,7 @@ async function resolveGatewayModels(engine: BrainEngine): Promise<AIGatewayConfi
setGatewayModelSource('chat', chatFull, dbChat ? { source: 'config_key', origin: 'chat_model' } : gatewayModelSource('chat', chatDetailed, chatEffective));
processState.modelCache.clear();
clearQueryEmbedCache();
- _shrinkState.clear();
+ processState.shrink.clear();
return processState.config;
}
@@ -742,11 +741,11 @@ function clearGatewayState(): void {
stashGatewayAnthropicKeyFromEnv(undefined); // gateway-owned snapshot dies with the config
processState.modelCache.clear();
clearQueryEmbedCache();
- _shrinkState.clear();
+ processState.shrink.clear();
_embedTransport = sdkEmbedMany;
_generateTextTransport = sdkGenerateText;
_generateObjectTransport = sdkGenerateObject;
- _structuredOutputRejectedRecipes.clear();
+ processState.structuredOutputRejected.clear();
_embedTransportInstalled = false;
_chatTransport = null;
_warnedRecipes.clear();
@@ -769,7 +768,7 @@ export function _clearGatewayForTests(): void {
export function resetGateway(): void {
clearGatewayState();
- // configureGateway re-clears _modelCache/_shrinkState; transports are NOT
+ // configureGateway re-clears the model cache and shrink state; transports are NOT
// touched by it, so a stale test transport can never leak back in through
// this path.
if (_resetBaseline) configureGateway(_resetBaseline());
@@ -1434,10 +1433,9 @@ export const perplexityCompatFetch = (async (input: RequestInfo | URL, init?: Re
* modes` read it); nothing is printed — shell-per-query agents would see a
* line on every search, and today's keyless state is stderr-silent.
*/
-const _noKeyNoticed = new Set<string>();
function noKeyOnce(modelStr: string, keyName: string, query: string, docCount: number): void {
try {
- if (_noKeyNoticed.has(modelStr)) return;
+ if (gw().noKeyNoticed.has(modelStr)) return;
// Mark AFTER the write succeeds — a transient audit-dir failure must not
// permanently silence the only trace of an unreranked process.
logRerankFailure({
@@ -1449,7 +1447,7 @@ function noKeyOnce(modelStr: string, keyName: string, query: string, docCount: n
`${keyName} not set — rerank calls skipped this process (results pass through ` +
`unreranked); fix: export ${keyName}=… or gbrain config set search.reranker.enabled false`,
});
- _noKeyNoticed.add(modelStr);
+ gw().noKeyNoticed.add(modelStr);
} catch {
// Traceability must never block the fail-open path.
}
@@ -1582,7 +1580,7 @@ const MIN_SUB_BATCH = 1;
* throw normalizeAIError(err, ...)
* ```
*
- * Per-recipe state lives in `_shrinkState` and survives across `embed()`
+ * Per-recipe state lives in the gateway state (`shrink`, per engine for embedded engines) and survives across `embed()`
* calls within one process. The healing path (after `SHRINK_HEAL_AFTER`
* consecutive batch successes) walks the factor back toward the recipe's
* declared `safety_factor` so a transient miss doesn't permanently cap
@@ -1736,35 +1734,35 @@ export function isTokenLimitError(err: unknown): boolean {
*/
function effectiveSafetyFactor(recipe: Recipe): number {
const declared = recipe.touchpoints?.embedding?.safety_factor ?? DEFAULT_SAFETY_FACTOR;
- const entry = _shrinkState.get(recipe.id);
+ const entry = gw().shrink.get(recipe.id);
return entry?.factor ?? declared;
}
/** Tighten the recipe's effective safety factor on a token-limit miss. */
function shrinkOnMiss(recipe: Recipe): void {
const declared = recipe.touchpoints?.embedding?.safety_factor ?? DEFAULT_SAFETY_FACTOR;
- const current = _shrinkState.get(recipe.id)?.factor ?? declared;
+ const current = gw().shrink.get(recipe.id)?.factor ?? declared;
const next = Math.max(SHRINK_FLOOR, current * 0.5);
- _shrinkState.set(recipe.id, { factor: next, consecutiveSuccesses: 0 });
+ gw().shrink.set(recipe.id, { factor: next, consecutiveSuccesses: 0 });
}
/** Bump the win counter; heal toward declared default after enough wins. */
function recordSubBatchSuccess(recipe: Recipe): void {
const declared = recipe.touchpoints?.embedding?.safety_factor ?? DEFAULT_SAFETY_FACTOR;
- const entry = _shrinkState.get(recipe.id);
+ const entry = gw().shrink.get(recipe.id);
if (!entry || entry.factor >= declared) {
// Either no shrink active, or already at/above the declared ceiling — nothing to heal.
if (entry) {
- _shrinkState.set(recipe.id, { factor: entry.factor, consecutiveSuccesses: 0 });
+ gw().shrink.set(recipe.id, { factor: entry.factor, consecutiveSuccesses: 0 });
}
return;
}
const wins = entry.consecutiveSuccesses + 1;
if (wins >= SHRINK_HEAL_AFTER) {
const healed = Math.min(declared, entry.factor * 1.5);
- _shrinkState.set(recipe.id, { factor: healed, consecutiveSuccesses: 0 });
+ gw().shrink.set(recipe.id, { factor: healed, consecutiveSuccesses: 0 });
} else {
- _shrinkState.set(recipe.id, { factor: entry.factor, consecutiveSuccesses: wins });
+ gw().shrink.set(recipe.id, { factor: entry.factor, consecutiveSuccesses: wins });
}
}
@@ -1774,7 +1772,7 @@ function recordSubBatchSuccess(recipe: Recipe): void {
* @internal exported for tests; not part of the public gateway API.
*/
export function __getShrinkStateForTests(recipeId: string): ShrinkEntry | undefined {
- const entry = _shrinkState.get(recipeId);
+ const entry = gw().shrink.get(recipeId);
return entry ? { ...entry } : undefined;
}
@@ -2650,7 +2648,7 @@ export async function expand(query: string): Promise<string[]> {
recordExpansionUsage(result.usage, true);
const parsed = ExpansionSchema.safeParse(result.object);
expansions = parsed.success ? parsed.data.queries : [];
- } else if (recipeSupportsStructuredOutputs(recipe) && !_structuredOutputRejectedRecipes.has(recipe.id)) {
+ } else if (recipeSupportsStructuredOutputs(recipe) && !gw().structuredOutputRejected.has(recipe.id)) {
// openai-compatible backend that honors strict json_schema: request the
// schema (strict validation), and fall back to the text path if it is
// rejected at call time so a mis-declared capability never drops expansion.
@@ -2677,7 +2675,7 @@ export async function expand(query: string): Promise<string[]> {
recordExpansionFailure(err, true);
// Adversarial F5: don't re-pay this attempt on every call — the
// capability mis-declaration is stable for the process lifetime.
- _structuredOutputRejectedRecipes.add(recipe.id);
+ gw().structuredOutputRejected.add(recipe.id);
expansions = await viaText(false);
}
} else {
@@ -3779,13 +3777,13 @@ async function chatAdmitted(opts: ChatOpts, admitted: {
// lane already handles. Native + claude-cli lanes never see the field.
// A declared backend that REJECTS it at call time (older Ollama build,
// strict proxy) gets one schemaless retry below and is remembered in
- // `_structuredOutputRejectedRecipes` — the same process-lifetime memory
+ // `structuredOutputRejected` on the gateway state — the same memory
// expand() keeps — so the caller's own retry lanes don't re-pay the
// rejection and turn every call into a permanent provider_error.
const output = opts.responseSchema
&& recipe.implementation === 'openai-compatible'
&& recipeSupportsStructuredOutputs(recipe)
- && !_structuredOutputRejectedRecipes.has(recipe.id)
+ && !gw().structuredOutputRejected.has(recipe.id)
? jsonSchemaOutput(opts.responseSchema)
: undefined;
@@ -3813,7 +3811,7 @@ async function chatAdmitted(opts: ChatOpts, admitted: {
result = await generate(output);
} catch (err) {
if (!output || isAIInvocationPolicyError(err) || !isStructuredOutputRejection(err)) throw err;
- _structuredOutputRejectedRecipes.add(recipe.id);
+ gw().structuredOutputRejected.add(recipe.id);
console.warn(
`[ai.gateway] ${recipe.id} rejected response_format json_schema; retrying without a schema ` +
`and skipping it for the rest of this process: ${(err as Error)?.message ?? String(err)}`,
diff --git a/src/core/ai/query-embed-cache.ts b/src/core/ai/query-embed-cache.ts
index 4ca1f0accf..335eba6c91 100644
--- a/src/core/ai/query-embed-cache.ts
+++ b/src/core/ai/query-embed-cache.ts
@@ -44,6 +44,16 @@ export function storeQueryEmbedding(key: string, vector: Float32Array, startedAt
if (entries.size > MAX_ENTRIES) entries.delete(entries.keys().next().value!);
}
+/** Drop every entry an engine cached under `tag` (its unique cache tag, engine-gateway.ts) when it closes. */
+export function dropQueryEmbeddingsTagged(tag: string): void {
+ for (const key of entries.keys()) if (key.includes(tag)) entries.delete(key);
+}
+
+/** @internal exported for tests: how many entries the cache holds. */
+export function __queryEmbedCacheSizeForTests(): number {
+ return entries.size;
+}
+
export function clearQueryEmbedCache(): void {
entries.clear();
generation++;
diff --git a/src/core/engine-open.ts b/src/core/engine-open.ts
index deac930dfd..2a2fb5ca38 100644
--- a/src/core/engine-open.ts
+++ b/src/core/engine-open.ts
@@ -10,7 +10,7 @@ import { constructEngine } from './engine-construct.ts';
import { makeEngineChatUsageSink, registerEngineChatUsageSink } from './ai/chat-usage.ts';
import { connectionUrlProblem, markEmbeddedEngine, releaseEmbeddedEngine, type EngineAISettings, type EngineSettings } from './embedded-engine.ts';
import { assertLocalWriterValue, setEngineHostId } from './persistence/identity.ts';
-import { engineProviderProblem, recipeKeyEnv, sharedProviderEnvConflict } from './ai/engine-gateway.ts';
+import { engineProviderProblem, recipeKeyEnv, releaseEngineGatewayState, sharedProviderEnvConflict } from './ai/engine-gateway.ts';
import { getRecipe } from './ai/recipes/index.ts';
export type { EngineAISettings, EngineSettings, EmbeddedSettingsView } from './embedded-engine.ts';
@@ -153,6 +153,7 @@ export async function openEngine(settings: EngineSettings): Promise<BrainEngine>
if (insideReconnect.getStore()) return disconnect();
closed = true;
dropUsageSink();
+ releaseEngineGatewayState(engine);
releaseEmbeddedEngine(engine);
await disconnect();
};
diff --git a/test/engine-gateway-state.test.ts b/test/engine-gateway-state.test.ts
new file mode 100644
index 0000000000..2aad1f3281
--- /dev/null
+++ b/test/engine-gateway-state.test.ts
@@ -0,0 +1,112 @@
+/**
+ * GBRA-283 B3a (#8499 item 2): the gateway's adaptive memory belongs to the engine whose call taught it. The batch
+ * shrink factor, the once-per-model no_key rerank audit row and the structured-output rejection memory are per engine
+ * for engines opened from settings (the process keeps its own), and an engine's query-embedding cache entries go
+ * when it disconnects. Every provider is a transport stub.
+ */
+import { afterEach, describe, expect, test } from 'bun:test';
+import { mkdtempSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { randomBytes, randomUUID } from 'node:crypto';
+import {
+ __getShrinkStateForTests, __setEmbedTransportForTests, __setGenerateObjectTransportForTests, __setGenerateTextTransportForTests,
+ configureGateway, embed, embedQuery, expand, rerank, resetGateway,
+} from '../src/core/ai/gateway.ts';
+import { __queryEmbedCacheSizeForTests } from '../src/core/ai/query-embed-cache.ts';
+import { readRecentRerankFailures } from '../src/core/rerank-audit.ts';
+import { openEngine, withEngineScope, withLocalScope, type EngineSettings } from '../src/core/engine-open.ts';
+import type { BrainEngine } from '../src/core/engine.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+const key = (label: string) => `sk-${label}-${randomBytes(12).toString('hex')}`;
+const open = (ai: EngineSettings['ai'], embedding = { model: 'voyage:voyage-3-large', dimensions: 1024 }) =>
+ openEngine({ embedded_core: true, engine: 'pglite', database: {}, host: { id: randomUUID() }, embedding, ai });
+const closeAll = (...engines: BrainEngine[]) => Promise.all(engines.map(engine => engine.disconnect()));
+const vectors = (values: string[], dims: number) => ({ embeddings: values.map(() => Array.from({ length: dims }, (_, i) => (i % 5) + 1)), usage: { tokens: 1 } });
+const TOKEN_LIMIT = new Error("Request to model 'voyage-3-large' failed. The max allowed tokens per submitted batch is 120000.");
+
+afterEach(() => {
+ __setEmbedTransportForTests(null);
+ __setGenerateObjectTransportForTests(null);
+ __setGenerateTextTransportForTests(null);
+ resetGateway();
+});
+
+describe('per-engine adaptive gateway state', () => {
+ test('a batch shrink learned in one engine\'s scope stays with that engine', async () => {
+ __setEmbedTransportForTests((async ({ values }: { values: string[] }) => {
+ if (values.length === 4) throw TOKEN_LIMIT;
+ return vectors(values, 1024);
+ }) as any);
+ configureGateway({ embedding_model: 'voyage:voyage-3-large', embedding_dimensions: 1024, env: { VOYAGE_API_KEY: key('process') } });
+ const a = await open({ providers: { voyage: { apiKey: key('a') } } });
+ const b = await open({ providers: { voyage: { apiKey: key('b') } } });
+ try {
+ await withEngineScope(a, () => embed(['a', 'b', 'c', 'd']));
+ expect(withEngineScope(a, () => __getShrinkStateForTests('voyage'))?.factor).toBe(0.25);
+ expect(withEngineScope(b, () => __getShrinkStateForTests('voyage'))).toBeUndefined();
+ expect(withLocalScope(() => __getShrinkStateForTests('voyage'))).toBeUndefined();
+ } finally {
+ await closeAll(a, b);
+ }
+ }, 60_000);
+
+ test('a structured-output rejection on one engine does not switch another engine to the text path', async () => {
+ const objectCalls: string[] = [];
+ __setGenerateObjectTransportForTests((async () => {
+ objectCalls.push('object');
+ throw new Error('This server does not support response_format json_schema');
+ }) as any);
+ __setGenerateTextTransportForTests((async () => ({ content: [{ type: 'text', text: '{"queries":["alternative"]}' }], text: '{"queries":["alternative"]}', finishReason: 'stop', usage: { inputTokens: 1, outputTokens: 1 } })) as any);
+ const ai = (): EngineSettings['ai'] => ({ providers: { ollama: { baseUrl: 'http://127.0.0.1:11434/v1' } }, models: { expansion: 'ollama:qwen2.5-coder:14b' } });
+ const a = await open(ai());
+ const b = await open(ai());
+ try {
+ await withEngineScope(a, () => expand('first question'));
+ await withEngineScope(a, () => expand('second question'));
+ expect(objectCalls).toEqual(['object']);
+ await withEngineScope(b, () => expand('first question on b'));
+ expect(objectCalls).toEqual(['object', 'object']);
+ } finally {
+ await closeAll(a, b);
+ }
+ }, 60_000);
+
+ test('each engine writes its own once-per-model no_key rerank audit row', async () => {
+ const home = mkdtempSync(join(tmpdir(), 'gbrain-gateway-state-'));
+ await withEnv({ GBRAIN_HOME: home, GBRAIN_AUDIT_DIR: undefined }, async () => {
+ const ai = (): EngineSettings['ai'] => ({ providers: { voyage: { baseUrl: 'https://rerank-a.example/v1' } }, models: { reranker: 'voyage:rerank-3-lite' } });
+ const a = await open(ai());
+ const b = await open(ai());
+ const noKeyRows = () => readRecentRerankFailures().filter(row => row.reason === 'no_key').length;
+ try {
+ const attempt = (engine: BrainEngine) => withEngineScope(engine, () => rerank({ query: 'q', documents: ['one'] }).catch(error => error.reason));
+ expect(await attempt(a)).toBe('no_key');
+ expect(await attempt(a)).toBe('no_key');
+ expect(noKeyRows()).toBe(1);
+ expect(await attempt(b)).toBe('no_key');
+ expect(noKeyRows()).toBe(2);
+ } finally {
+ await closeAll(a, b);
+ }
+ });
+ }, 60_000);
+
+ test('an engine\'s query-embedding cache entries are dropped when it disconnects', async () => {
+ __setEmbedTransportForTests((async ({ values }: { values: string[] }) => vectors(values, 1024)) as any);
+ const a = await open({ providers: { voyage: { apiKey: key('a') } } });
+ const b = await open({ providers: { voyage: { apiKey: key('b') } } });
+ try {
+ const before = __queryEmbedCacheSizeForTests();
+ await withEngineScope(a, () => embedQuery('a question'));
+ await withEngineScope(a, () => embedQuery('another question'));
+ await withEngineScope(b, () => embedQuery('a question'));
+ expect(__queryEmbedCacheSizeForTests()).toBe(before + 3);
+ await a.disconnect();
+ expect(__queryEmbedCacheSizeForTests()).toBe(before + 1);
+ } finally {
+ await closeAll(a, b);
+ }
+ }, 60_000);
+});