Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(gateway): fold config-plane voyage_api_key into VOYAGE_API_KEY like the other hosted keys (#3236)
Masa2a0c51d093c8
diff --git a/src/commands/autopilot.ts b/src/commands/autopilot.tsindex d5d68be34e..554bc26fd2 100644--- a/src/commands/autopilot.ts+++ b/src/commands/autopilot.ts@@ -871,9 +871,19 @@ export async function runAutopilot(engine: BrainEngine, args: string[]) {} catch {embeddingModel = (await engine.getConfig('embedding_model')) ?? undefined;}- const embedKeyCfg: Record<string, string | null> = {};+ // #2662 (codex round-3): HOSTED_EMBED_KEY_CONFIG entries are keys+ // buildGatewayConfig folds from the FILE plane only — `gbrain config+ // set <key> X` writes the DB plane, which never reaches the gateway+ // for these fields. Reading via engine.getConfig() here (DB plane)+ // would report a provider "configured" from a DB-only key that the+ // gateway can never actually use, dispatching a doomed embed job.+ // Read the same file-plane source context.ts (doctor) reads instead,+ // so autopilot and doctor agree with what the gateway can see.+ const { loadConfigFileOnly } = await import('../core/config.ts');+ const fileCfg = loadConfigFileOnly() as Record<string, unknown> | null;+ const embedKeyCfg: Record<string, unknown> = {};for (const field of Object.values(HOSTED_EMBED_KEY_CONFIG)) {- embedKeyCfg[field] = await engine.getConfig(field);+ embedKeyCfg[field] = fileCfg?.[field];}const ctx = {repoPath,diff --git a/src/core/ai/build-gateway-config.ts b/src/core/ai/build-gateway-config.tsindex 2dd4bd8ae4..c63812b4c9 100644--- a/src/core/ai/build-gateway-config.ts+++ b/src/core/ai/build-gateway-config.ts@@ -9,7 +9,7 @@* import it from `../../src/cli.ts`.** The single ownership site for: (a) folding file-plane API keys- * (openai/anthropic/zeroentropy) into the gateway env, and (b) threading+ * (openai/anthropic/zeroentropy/openrouter/voyage) into the gateway env, and (b) threading* local-server `*_BASE_URL` env vars into base_urls. Both matter for the* init-time embedding-key probe — without (a) it would false-warn on* config.json-keyed users, and without (b) a live probe could hit the wrong@@ -38,6 +38,12 @@ export function buildGatewayConfig(c: GBrainConfig): AIGatewayConfig {// config.json) must reach the openrouter recipe's OPENROUTER_API_KEY.// process.env still wins via the later spread.if (c.openrouter_api_key) envFromConfig.OPENROUTER_API_KEY = c.openrouter_api_key;+ // #2662: same seam for Voyage. Before this, config.json's voyage_api_key+ // was accepted at the file plane but never threaded into the gateway env,+ // so launchd/daemon/MCP contexts (no process-env export) silently failed+ // multimodal/image embeds despite config.json looking complete. process.env+ // still wins via the later spread.+ if (c.voyage_api_key) envFromConfig.VOYAGE_API_KEY = c.voyage_api_key;// v0.32 codex finding #4+#5 fix: thread local-server _BASE_URL env vars// into base_urls so the gateway hits the user's configured port. Withoutdiff --git a/src/core/brain-score-recommendations.ts b/src/core/brain-score-recommendations.tsindex 810b43bedc..83a92c404b 100644--- a/src/core/brain-score-recommendations.ts+++ b/src/core/brain-score-recommendations.ts@@ -11,21 +11,35 @@ import { parseModelId } from './ai/model-resolver.ts';* RecommendationContext (doctor + autopilot) use this to build a sync* `resolveKey` closure without re-parsing recipes.*- * Only OPENAI_API_KEY and ZEROENTROPY_API_KEY appear here because those are the- * only embedding keys `buildGatewayConfig` (src/cli.ts) folds from config into- * the gateway env. VOYAGE_API_KEY / GOOGLE_GENERATIVE_AI_API_KEY are deliberately- * absent: their config fields are NOT threaded to the gateway today, so the- * producer closures fall through to checking `process.env` ONLY for them. That- * matches what the gateway can actually use (the recipes read those keys from- * env). Counting a config-plane voyage_api_key/google_api_key here would be a- * false positive: doctor/autopilot would call the provider "configured" and- * dispatch an embed.stale job that then fails auth at the gateway. When a future- * change threads voyage_api_key/google_api_key into buildGatewayConfig (the open- * voyage-config-mapping work), re-add the matching entry here in the same change.+ * Only keys that `buildGatewayConfig` (src/core/ai/build-gateway-config.ts)+ * actually folds from config into the gateway env may appear here.+ * GOOGLE_GENERATIVE_AI_API_KEY is deliberately absent: its config field is NOT+ * threaded to the gateway today, so the producer closures fall through to+ * checking `process.env` ONLY for it. That matches what the gateway can+ * actually use (the recipe reads that key from env). Counting a config-plane+ * google_api_key here would be a false positive: doctor/autopilot would call+ * the provider "configured" and dispatch an embed.stale job that then fails+ * auth at the gateway. When a future change threads google_api_key into+ * buildGatewayConfig, re-add the matching entry here in the same change.+ *+ * VOYAGE_API_KEY → voyage_api_key was the same kind of gap (#2662) until+ * buildGatewayConfig started folding it — now safe to list here too.+ *+ * Caveat inherited from the existing OPENAI_API_KEY/ZEROENTROPY_API_KEY+ * entries (unchanged by #2662, noted here for anyone extending this map):+ * autopilot's resolveKey resolves these fields via `engine.getConfig()`+ * (DB plane), while `buildGatewayConfig` only folds the FILE-plane+ * (config.json) value. A `gbrain config set voyage_api_key X` with no+ * matching config.json entry can therefore still read "configured" here+ * while the gateway has no key — a pre-existing false-positive class, not+ * introduced or fixed by this change. Closing it requires threading+ * `*_api_key` DB values through `loadConfigWithEngine()` before+ * `buildGatewayConfig`, which is a separate, larger change.*/export const HOSTED_EMBED_KEY_CONFIG: Record<string, string> = {OPENAI_API_KEY: 'openai_api_key',ZEROENTROPY_API_KEY: 'zeroentropy_api_key',+ VOYAGE_API_KEY: 'voyage_api_key',};/**diff --git a/src/core/config.ts b/src/core/config.tsindex aa1c750ebc..bf81d77f94 100644--- a/src/core/config.ts+++ b/src/core/config.ts@@ -48,6 +48,21 @@ export interface GBrainConfig {* reads OPENROUTER_API_KEY.*/openrouter_api_key?: string;+ /**+ * Voyage AI API key (#2662). File-plane slot so `~/.gbrain/config.json`'s+ * `voyage_api_key` reaches the voyage recipe the same way+ * zeroentropy_api_key/openrouter_api_key do: file plane →+ * buildGatewayConfig env dict → recipe reads VOYAGE_API_KEY. Before this,+ * launchd/daemon/MCP contexts without a process-env export silently+ * failed multimodal embeds despite config.json looking complete.+ *+ * NOTE (scoped to what this fix covers): `gbrain config set+ * voyage_api_key X` writes the DB plane, which `loadConfigWithEngine()`+ * does NOT merge for any `*_api_key` field (zeroentropy_api_key /+ * openrouter_api_key have the same pre-existing gap) — only the+ * config.json file-plane route is wired through today.+ */+ voyage_api_key?: string;/** AI gateway config (v0.14+). v0.36+ default: "zeroentropyai:zembed-1" / 1280 / "anthropic:claude-haiku-4-5-20251001". */embedding_model?: string;embedding_dimensions?: number;@@ -885,6 +900,7 @@ export const KNOWN_CONFIG_KEYS: readonly string[] = ['anthropic_api_key','zeroentropy_api_key','openrouter_api_key',+ 'voyage_api_key','embedding_model','embedding_dimensions','embedding_disabled',diff --git a/test/ai/build-gateway-config.test.ts b/test/ai/build-gateway-config.test.tsindex e94ff39fe8..3b3e6d390d 100644--- a/test/ai/build-gateway-config.test.ts+++ b/test/ai/build-gateway-config.test.ts@@ -117,6 +117,28 @@ describe('buildGatewayConfig config-plane API-key folding', () => {expect(cfg.env.OPENROUTER_API_KEY).toBe('sk-or-env-plane');});});++ // #2662: voyage_api_key was accepted at the file plane (config.json) but+ // never folded into the gateway env, so daemons/launchd/MCP callers with+ // no process-env export silently failed multimodal embeds. Same fold+ // pattern as zeroentropy/openrouter above.+ test('voyage_api_key folds into gateway env as VOYAGE_API_KEY', async () => {+ await withEnv({ VOYAGE_API_KEY: undefined }, async () => {+ const cfg = buildGatewayConfig({+ voyage_api_key: 'pa-config-plane',+ } as unknown as GBrainConfig);+ expect(cfg.env.VOYAGE_API_KEY).toBe('pa-config-plane');+ });+ });++ test('a real VOYAGE_API_KEY process.env value wins over the config-plane fallback', async () => {+ await withEnv({ VOYAGE_API_KEY: 'pa-env-plane' }, async () => {+ const cfg = buildGatewayConfig({+ voyage_api_key: 'pa-config-plane',+ } as unknown as GBrainConfig);+ expect(cfg.env.VOYAGE_API_KEY).toBe('pa-env-plane');+ });+ });});describe('buildGatewayConfig env empty-string clobber guard (#1249)', () => {diff --git a/test/brain-score-recommendations.test.ts b/test/brain-score-recommendations.test.tsindex d3935de9c6..b76a98df69 100644--- a/test/brain-score-recommendations.test.ts+++ b/test/brain-score-recommendations.test.ts@@ -60,11 +60,58 @@ describe('embeddingProviderConfigured (recipe-aware helper)', () => {test('HOSTED_EMBED_KEY_CONFIG only maps gateway-propagated config keys', () => {expect(HOSTED_EMBED_KEY_CONFIG.OPENAI_API_KEY).toBe('openai_api_key');expect(HOSTED_EMBED_KEY_CONFIG.ZEROENTROPY_API_KEY).toBe('zeroentropy_api_key');+ // #2662: buildGatewayConfig now folds voyage_api_key → VOYAGE_API_KEY,+ // so this producer-facing map must recognize it as gateway-propagated.+ expect(HOSTED_EMBED_KEY_CONFIG.VOYAGE_API_KEY).toBe('voyage_api_key');// Not propagated to the gateway today → must NOT be backed by a config field- // (producer closures fall through to process.env only for these).- expect(HOSTED_EMBED_KEY_CONFIG.VOYAGE_API_KEY).toBeUndefined();+ // (producer closures fall through to process.env only for this one).expect(HOSTED_EMBED_KEY_CONFIG.GOOGLE_GENERATIVE_AI_API_KEY).toBeUndefined();});++ // #2662: end-to-end regression through the REAL file-plane loader+ // (`loadConfigFileOnly`, the same helper src/core/remediation/context.ts+ // calls) and the REAL gateway-config builder. Writes an actual+ // ~/.gbrain/config.json whose ONLY source of the Voyage key is the+ // config-plane field (no process.env export — the launchd/daemon/MCP+ // scenario from the bug report). Note: `resolveKey` below still mirrors+ // (does not literally invoke) context.ts's closure shape, since the real+ // loadRecommendationContext() needs a live BrainEngine; what this test+ // adds over the plain unit test above is exercising the real+ // loadConfigFileOnly() → buildGatewayConfig() file-plane path end to end.+ test('config-plane-only voyage_api_key: real config.json flows through loadConfigFileOnly + buildGatewayConfig', async () => {+ const { mkdtempSync, mkdirSync, writeFileSync, rmSync } = await import('fs');+ const { join } = await import('path');+ const { tmpdir } = await import('os');+ const { withEnv } = await import('./helpers/with-env.ts');+ const tmpHome = mkdtempSync(join(tmpdir(), 'gbrain-voyage-cfg-test-'));+ try {+ mkdirSync(join(tmpHome, '.gbrain'), { recursive: true });+ writeFileSync(+ join(tmpHome, '.gbrain', 'config.json'),+ JSON.stringify({ engine: 'pglite', database_path: '/tmp/x', voyage_api_key: 'pa-config-plane-only' }),+ );+ await withEnv({ GBRAIN_HOME: tmpHome, VOYAGE_API_KEY: undefined }, async () => {+ const { loadConfigFileOnly } = await import('../src/core/config.ts');+ const { buildGatewayConfig } = await import('../src/core/ai/build-gateway-config.ts');+ const fileCfg = loadConfigFileOnly();+ expect(fileCfg?.voyage_api_key).toBe('pa-config-plane-only');++ // Same resolveKey shape as loadRecommendationContext (context.ts).+ const resolveKey = (envVar: string) => {+ const cfgField = HOSTED_EMBED_KEY_CONFIG[envVar];+ const fromCfg = cfgField ? (fileCfg as Record<string, unknown> | null)?.[cfgField] : undefined;+ return !!(process.env[envVar] || fromCfg);+ };+ expect(embeddingProviderConfigured('voyage:voyage-3', resolveKey)).toBe(true);++ // The actual gateway builder must receive the key too.+ const gwCfg = buildGatewayConfig(fileCfg!);+ expect(gwCfg.env.VOYAGE_API_KEY).toBe('pa-config-plane-only');+ });+ } finally {+ rmSync(tmpHome, { recursive: true, force: true });+ }+ });});/**