fix: propagate openai_api_key and anthropic_api_key from config to SDK env#141
Open
Hybirdss wants to merge 1 commit intogarrytan:masterfrom
Open
fix: propagate openai_api_key and anthropic_api_key from config to SDK env#141Hybirdss wants to merge 1 commit intogarrytan:masterfrom
Hybirdss wants to merge 1 commit intogarrytan:masterfrom
Conversation
…K env Both fields were declared on GBrainConfig but neither was consumed at runtime — new OpenAI() (embedding.ts:24) and new Anthropic() (search/expansion.ts:19) both instantiate without an explicit apiKey, so they rely on the SDK ambient *_API_KEY env var lookup. Writing either key to ~/.gbrain/config.json had no effect. loadConfig() now: - mirrors ANTHROPIC_API_KEY env var into the merged config (parity with the existing OPENAI_API_KEY merge line) - propagates merged.openai_api_key / merged.anthropic_api_key into process.env when the env var is unset, so the SDKs' ambient lookup picks it up at new OpenAI() / new Anthropic() construction 12 new subprocess-isolated tests (parameterized over both keys) in test/config.test.ts cover all precedence cases. Subprocess pattern matches test/upgrade.test.ts — os.homedir() caches HOME at process start on Bun. Tests: 846 → 858 pass, 0 regressions. Version bumped to 0.10.2. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
beb50ed to
31d1c1a
Compare
Author
|
Scope amended shortly after submission: originally anthropic-only claiming parity with Note: Happy to split into two PRs (env-merge consistency vs. config→env propagation) if you prefer a smaller change, or to narrow back to anthropic-only if this is already in a queued wave commit. Just let me know. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Both
openai_api_keyandanthropic_api_keyin~/.gbrain/config.jsonwere dead fields at runtime. The SDK clients are instantiated without an
explicit
apiKeyatsrc/core/embedding.ts:24(new OpenAI()) andsrc/core/search/expansion.ts:19(new Anthropic()), so both fall backto the SDK's ambient
process.env.*_API_KEYlookup. Writing either keyto the config file produced silent failure.
Only the interface declarations and two
init.tswrites (which set thefield from a CLI flag) — no runtime reader for either key.
Fix (
src/core/config.ts):ANTHROPIC_API_KEYenv var into the merged config, matchingthe existing
OPENAI_API_KEYmerge line (parity at the config-objectlayer).
env var is unset, propagate the config value into
process.envsothe SDK's ambient lookup picks it up on next client construction.
Env var still wins when both are set.
No refactor to
embedding.tsorexpansion.ts— the singletons'instantiation is unchanged and the SDK contract is preserved.
Test Coverage
Tests: 846 → 858 (+12 new, parameterized over both keys, +0 test files).
984 total runs, 0 failures.
Subprocess-per-test pattern (same as
test/upgrade.test.ts) becauseos.homedir()caches HOME at process start on Bun — in-process HOMEoverrides do not affect
homedir()resolution, so each case spawns afresh
bun -ewith a scopedHOMEenv.Pre-Landing Review
No issues found. Both Anthropic and OpenAI SDK constructors resolve
their API key from
process.envat first request time, so propagatinginto the environment before the singleton is constructed (
loadConfig()runs at
src/mcp/server.ts:65before everyop.handlercall) issufficient and non-invasive.
Eval Results
No prompt-related files changed — evals skipped.
TODOS
No TODO items completed in this PR.
Test plan
bun test test/config.test.ts— 20 pass, 0 fail (12 new)bun test— 858 pass, 126 skip, 0 fail (984 total)bun run build— compiledbin/gbrainDocumentation
CHANGELOG.md: added0.10.2 — 2026-04-16entry under "Fixed"covering both keys
VERSIONandpackage.json: bumped to0.10.2restores the behavior the existing config-file shape already implied.
🤖 Generated with Claude Code