singularity-forge/src/wizard.ts
Jeremy McSpadden 74c1736372 fix(remote-questions): empty-key entry in auth.json shadows valid Discord bot token (#2737)
* fix(remote-questions): empty-key entry in auth.json shadows valid Discord bot token

removeProviderToken() called auth.set(provider, { key: '' }) instead of
auth.remove(provider). Since AuthStorage.set() appends for api_key type
(deduplicating by exact key match), this inserted an empty-key entry at
index 0. Every credential lookup (.get(), .find()) matched the empty
entry first, shadowing valid tokens at later indices.

Fixes:
- remote-command.ts: use auth.remove() instead of auth.set() with empty key
- config.ts: hydrateRemoteTokensFromAuth .find() now requires non-empty key
- wizard.ts: loadStoredEnvKeys uses getCredentialsForProvider + .find()
  instead of .get() which returns creds[0]
- onboarding.ts: check existing tokens via .some() over full credentials
  array instead of .get() which only returns first entry
- key-manager.ts: filter empty-key entries in getAllKeyStatuses, add/remove/
  rotate provider pickers, and doctor env-conflict check

Tests: 3186 pass, 0 fail across full GSD test suite

* fix(config): ignore empty shadowing tool keys
2026-03-26 16:16:42 -06:00

35 lines
1.5 KiB
TypeScript

import type { AuthStorage } from '@gsd/pi-coding-agent'
// ─── Env hydration ────────────────────────────────────────────────────────────
/**
* Hydrate process.env from stored auth.json credentials for optional tool keys.
* Runs on every launch so extensions see Brave/Context7/Jina keys stored via the
* wizard on prior launches.
*/
export function loadStoredEnvKeys(authStorage: AuthStorage): void {
const providers: Array<[string, string]> = [
['brave', 'BRAVE_API_KEY'],
['brave_answers', 'BRAVE_ANSWERS_KEY'],
['context7', 'CONTEXT7_API_KEY'],
['jina', 'JINA_API_KEY'],
['tavily', 'TAVILY_API_KEY'],
['slack_bot', 'SLACK_BOT_TOKEN'],
['discord_bot', 'DISCORD_BOT_TOKEN'],
['telegram_bot', 'TELEGRAM_BOT_TOKEN'],
['groq', 'GROQ_API_KEY'],
['ollama-cloud', 'OLLAMA_API_KEY'],
['custom-openai', 'CUSTOM_OPENAI_API_KEY'],
]
for (const [provider, envVar] of providers) {
if (!process.env[envVar]) {
// Use getCredentialsForProvider to skip empty-key entries at index 0
// (left by legacy removeProviderToken which used set() with empty key)
const creds = authStorage.getCredentialsForProvider(provider)
const cred = creds.find((c: any) => c.type === 'api_key' && c.key)
if (cred?.type === 'api_key' && (cred as any).key) {
process.env[envVar] = (cred as any).key as string
}
}
}
}