Repository navigation
fix(usage): read OpenCode Go API key from the cached login-shell environment - #52324
Conversation
[Low impact] The PR appears safe to merge. SummaryAdds a cached login-shell fallback for
No actionable issues were found. The previous-review ancestry checks failed, so this review covered the full PR changes. Tests were inspected but not run. Reviews (2) · Last reviewed commit: "fix(usage): read OpenCode Go keys from t..." · Reviewed by Greptile |
|
No actionable comments were generated in the recent review. Walkthrough
Severity of issue fixed: Medium Merge Risk:
|
…onment When OPENCODE_API_KEY is exported only in a shell rc file, Orca started from Finder, the Dock or a systemd/SSH service never has it in process.env, so the OpenCode Go usage meter reported missing credentials. The resolver now fills just that key, trimmed, from the cached login-shell capture when the process env lacks it. The read is cache-only, so it never spawns or waits for a shell, and a process key still wins. Fixes stablyai#52298 Co-authored-by: Michael Zhou <[email protected]>
5be76d4 to
ed041bf
Compare
ELI5
If
OPENCODE_API_KEYis set only in your shell startup file (like.zshrc), an Orca started from Finder, the Dock or a systemd/SSH service never sees it. So the OpenCode Go usage bar said the key was missing even though the key works. Orca already captures the login-shell environment at startup for Claude and Codex, and OpenCode Go now reads its key from that capture too.What Changed
resolveOpenCodeGoApiKey(opencode-go-api-key-source.ts) built its environment fromprocess.envplus the managed-account overlay. A key exported only in an rc file was invisible, and the usage roster showedmissing-credentials.OPENCODE_API_KEY, the resolver copies just that one key, trimmed, from the login-shell capture that is already cached (resolveLoginShellEnvironment({ cachedOnly: true })). That read never starts a shell and never waits for one.Unchanged: the poll call site, environments that callers pass in, and the managed-account overlay (
OPENCODE_API_KEYis not one of its keys).Why
The OpenCode Go resolver was the only usage provider that never looked at the login-shell capture. Claude and Codex usage already read it. A cache-only read adds no subprocess, sync I/O or polling to the usage cycle.
Two alternatives were tried in review and dropped:
Promise.allSettledcovers every provider, so a slow rc file would hold back the first refresh for all of them.Original fix by @joei4cm in #52324 (narrowed during review). Thanks to Echokovo for reporting the issue.
Linked Issue
Fixes #52298
Visual Proof
N/A. This is a main-process credential lookup with no UI change. The only visible effect is that the existing OpenCode Go usage card shows up instead of the missing-credentials state on affected hosts.
Testing
I manually tested these changes locally
Automated tests added/updated, or explained why not below
Every added test fails without this change and avoids the test anti-patterns
New
opencode-go-api-key-source.test.tscovers three cases: a shell-only key resolves with tierenvironment, a blank process key falls back to the shell key, and a process key still wins. The mock answers only cache-only reads, so the test hangs if a wait on the capture is ever reintroduced. On main it fails withmissing.vitest run src/main/rate-limits/providers/opencode-go/: 3 files, 19 tests pass.Real-shell scratch check on macOS: a temp
ZDOTDIRwhose.zshrcexports the key, withSHELL=/bin/zsh. After the boot capture is warmed, the resolver returns the rc-file key, with 0 shell spawns across 200 resolves.Not tested against a real systemd unit or a live OpenCode Go account. The code is platform-neutral env plumbing. On Windows, env vars are already in
process.env.Performance
None that matters. This runs in the main process / orcad usage poll (15 min by default, at least 30 s apart), once per cycle, and only when
process.envlacks the key. Each run is one cached-promise read plus the existingprocess.envcopy insidecaptureLoginShellEnvironment:There are 0 shell spawns, no new sync I/O, no new polling, and no renderer or store work, so there is no per-tab fanout.
Risk
Level: Low
The riskiest part is precedence. A shell-only key now reaches the OpenCode Go endpoint for Dock- or service-launched hosts. That is the same behavior as a terminal-launched Orca, and a process key still wins. To back this out, revert the commit; it leaves no persisted state behind.
Agent skill upstream boundary
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)