Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(ai): name the real API-key variable and the overridden embedding model in key-source diagnostics (#5137)
garrytan368bf2386e6a
diff --git a/src/commands/doctor/checks/embedding-health.ts b/src/commands/doctor/checks/embedding-health.tsindex 64319405d7..68d3ae959d 100644--- a/src/commands/doctor/checks/embedding-health.ts+++ b/src/commands/doctor/checks/embedding-health.ts@@ -12,7 +12,7 @@ import { checkEmbeddingEnvOverride, checkEmbeddingMigrationState } from './searcimport type { GBrainConfig } from '../../../core/config.ts';import { DEFAULT_EMBEDDING_MODEL } from '../../../core/ai/defaults.ts';import { providerKeyShadows, providerKeySource } from '../../../core/ai/provider-env.ts';-import { keyShadowWarning } from '../../../core/ai/key-warnings.ts';+import { credentialEnvName, keyShadowWarning } from '../../../core/ai/key-warnings.ts';import { getRecipe } from '../../../core/ai/recipes/index.ts';import type { Check } from '../../doctor.ts';import { connectedEngine, type DoctorContext, type DoctorEntry } from '../context.ts';@@ -451,8 +451,8 @@ const KEY_SOURCE_SCOPE = 'This check sees only the environment `gbrain doctor` rexport function embeddingKeySource(fileCfg: GBrainConfig | null, env: Record<string, string | undefined>, file: string): Pick<Check, 'status' | 'message' | 'details'> {const shadows = providerKeyShadows(fileCfg, env);- const model = fileCfg?.embedding_model ?? DEFAULT_EMBEDDING_MODEL;- const variable = getRecipe(model.split(':')[0] ?? '')?.auth_env?.required[0];+ const model = env.GBRAIN_EMBEDDING_MODEL || fileCfg?.embedding_model || DEFAULT_EMBEDDING_MODEL;+ const variable = credentialEnvName(getRecipe(model.split(':')[0] ?? '')?.auth_env);const source = variable ? providerKeySource(fileCfg, env, variable) : null;const inEffect = !source ? `The embedding model ${model} reads no API key.`: source.kind === 'env' ? `The embedding key in effect is ${source.variable} from this environment.`diff --git a/src/core/ai/key-warnings.ts b/src/core/ai/key-warnings.tsindex e8671b6e10..a45a478605 100644--- a/src/core/ai/key-warnings.ts+++ b/src/core/ai/key-warnings.ts@@ -43,12 +43,17 @@ export function warnShadowedProviderKeys(fileCfg: GBrainConfig | null = loadConf} catch { /* an invalid GBRAIN_HOME is reported by the command itself */ }}-export interface AuthFailedProvider { id: string; name: string; auth_env?: { required: readonly string[] } }+export interface AuthFailedProvider { id: string; name: string; auth_env?: { required: readonly string[]; optional?: readonly string[] } }++/** The env variable holding a recipe's API key, which need not be required (Azure, LiteLLM) or first. */+export function credentialEnvName(authEnv: AuthFailedProvider['auth_env']): string | undefined {+ return [...authEnv?.required ?? [], ...authEnv?.optional ?? []].find(name => /_API_KEY$/.test(name));+}/** DX-O8: names the key source in effect and the fixes for it. */export function embeddingAuthFailedError(provider: AuthFailedProvider, status: number,fileCfg: GBrainConfig | null = loadConfigFileOnly(), env: Env = process.env, file = configPath()): OperationError {- const name = provider.auth_env?.required[0];+ const name = credentialEnvName(provider.auth_env);const rejected = `The ${provider.name} embedding provider rejected its key (HTTP ${status})`;const backfill = 'Then run `gbrain embed --stale` to embed what was saved while the key was rejected.';if (!name) return catalogueError('embedding_auth_failed', `${rejected}.`, `Check the ${provider.name} credentials, restart the process that reported this, then run \`gbrain embed --stale\`.`);@@ -59,7 +64,7 @@ export function embeddingAuthFailedError(provider: AuthFailedProvider, status: n`Run \`gbrain config set ${source.config_key} <valid key>\` and restart any daemon that uses it. ${backfill}`);}if (source.kind === 'missing') {- return catalogueError('embedding_auth_failed', `${rejected}; ${source.variable} is not set in this process, so the key came from the caller's gateway configuration.`,+ return catalogueError('embedding_auth_failed', `${rejected}; no API key is set in this process (${source.variable}${source.config_key ? ` or ${source.config_key}` : ''}), so it authenticated another way (an injected gateway environment or a token such as Azure Entra).`,`${source.config_key ? `Run \`gbrain config set ${source.config_key} <valid key>\`` : `Set ${source.variable} to a valid key in the environment of the process that reported this`}, restart that process, then run \`gbrain embed --stale\`.`);}const differs = source.shadows_config && source.config_key ? `, which differs from ${source.config_key} in ${file}` : '';diff --git a/test/embedding-key-source.test.ts b/test/embedding-key-source.test.tsindex b0bb1a1092..848739b2fe 100644--- a/test/embedding-key-source.test.ts+++ b/test/embedding-key-source.test.ts@@ -10,7 +10,8 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { mergedProviderEnv, providerKeyShadows, providerKeySource } from '../src/core/ai/provider-env.ts';-import { _setKeyWarningSinkForTests, embeddingAuthFailedError, warnShadowedProviderKeys } from '../src/core/ai/key-warnings.ts';+import { _setKeyWarningSinkForTests, credentialEnvName, embeddingAuthFailedError, warnShadowedProviderKeys } from '../src/core/ai/key-warnings.ts';+import { getRecipe } from '../src/core/ai/recipes/index.ts';import { redactProviderKeys } from '../src/core/ai/key-redact.ts';import { configureGateway, embed, resetGateway } from '../src/core/ai/gateway.ts';import { embeddingKeySource } from '../src/commands/doctor/checks/embedding-health.ts';@@ -85,6 +86,18 @@ describe('doctor embedding_key_source', () => {expect(leaks(JSON.stringify(check))).toEqual([]);});+ test('names the real credential for Azure and LiteLLM, and follows GBRAIN_EMBEDDING_MODEL over the file model', () => {+ expect(credentialEnvName(getRecipe('azure-openai')?.auth_env)).toBe('AZURE_OPENAI_API_KEY');+ expect(credentialEnvName(getRecipe('litellm')?.auth_env)).toBe('LITELLM_API_KEY');+ const check = embeddingKeySource(cfg({ embedding_model: 'openai:text-embedding-3-small' }),+ { GBRAIN_EMBEDDING_MODEL: 'voyage:voyage-4', VOYAGE_API_KEY: ENV_KEY }, '/home/example/.gbrain/config.json');+ expect(check.message).toContain('The embedding key in effect is VOYAGE_API_KEY from this environment.');+ expect(check.details).toMatchObject({ embedding_model: 'voyage:voyage-4' });+ const azure = embeddingAuthFailedError(getRecipe('azure-openai')!, 401, cfg({ azure_openai_api_key: CONFIG_KEY } as Partial<GBrainConfig>), {}, '/home/example/.gbrain/config.json');+ expect(azure.message).toContain('the key in effect is azure_openai_api_key in /home/example/.gbrain/config.json (AZURE_OPENAI_API_KEY is not set)');+ expect(azure.message).not.toContain('AZURE_OPENAI_ENDPOINT');+ });+test('is ok without a shadow and reports a config-plane embedding key', () => {const check = embeddingKeySource(cfg({ embedding_model: 'openai:text-embedding-3-small' }), {}, '/home/example/.gbrain/config.json');expect(check.status).toBe('ok');