Skip to content

fix(usage): read OpenCode Go API key from the cached login-shell environment - #52324

Merged
nwparker merged 1 commit into
stablyai:mainfrom
joei4cm:fix/opencode-go-usage-login-shell-env
Nov 29, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
joei4cm:fix/opencode-go-usage-login-shell-env

Conversation

@joei4cm

@joei4cm joei4cm commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

If OPENCODE_API_KEY is 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

  • Before: resolveOpenCodeGoApiKey (opencode-go-api-key-source.ts) built its environment from process.env plus the managed-account overlay. A key exported only in an rc file was invisible, and the usage roster showed missing-credentials.
  • After: if the process environment has no non-blank 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.
  • A key in the process environment still wins. When the shell capture has no key, nothing is assigned, so every other env-derived input stays the same as on main.
  • If a poll runs before the startup shell capture finishes, it still reports the key missing, and the next normal usage poll picks it up. Nothing triggers an extra usage cycle when the capture lands.

Unchanged: the poll call site, environments that callers pass in, and the managed-account overlay (OPENCODE_API_KEY is 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:

  • Passing the whole captured environment from the poll call site. This changes HOME, XDG and PATH for the credential database and backend detection, and it applied the managed-account overlay to caller-passed environments.
  • Awaiting the running capture. One Promise.allSettled covers 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.ts covers three cases: a shell-only key resolves with tier environment, 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 with missing.

  • vitest run src/main/rate-limits/providers/opencode-go/: 3 files, 19 tests pass.

  • Real-shell scratch check on macOS: a temp ZDOTDIR whose .zshrc exports the key, with SHELL=/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.env lacks the key. Each run is one cached-promise read plus the existing process.env copy inside captureLoginShellEnvironment:

  • 116-var environment: about 5 us per call.
  • Adversarial 416-var environment: about 56 us per call.
  • End-to-end resolve: +0.11 ms (155 vars) and +0.9 ms (adversarial 455 vars).

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

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@greptile-apps

greptile-apps Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The PR appears safe to merge.RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge.

Summary

Adds a cached login-shell fallback for OPENCODE_API_KEY when the process environment has no usable key.

  • OpenCode Go usage can find keys kept only in a login shell.

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

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When the resolver creates an environment copy and that copy has no nonblank OPENCODE_API_KEY, it now checks the cached login-shell environment and copies a nonblank value if present. It preserves an existing nonblank key. Tests cover absent, whitespace-only, and nonblank process keys.

Severity of issue fixed: Medium

Merge Risk: Minimal · up to ed041

OpenCode Go usage lookup now falls back to the cached login-shell API key when the process environment lacks one. A nonblank process key still takes precedence, and nothing was found that would block merging.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Issue #52298 requires OPENCODE_API_KEY from the captured login-shell environment to reach the OpenCode Go usage fetch when the host runs under a service manager. The issue also states that the capture… Show that the login-shell environment cache is populated before or during the first OpenCode Go usage fetch on a service-started host. For example, cite the warm-up call path, or run the reproduction from the issue. Also confirm that the ad…
Passed Passed checks: 3 passed

Check name Status Explanation
Out of Scope Changes check Passed At the reviewed head, the incremental changes are limited to opencode-go-api-key-source.ts (a cache-only shell fallback for OPENCODE_API_KEY, applied only when no environment is supplied) and a new re…
Title check Passed The title names the main change: OpenCode Go API key lookup now reads from the cached login-shell environment. It also carries the conventional fix(usage) prefix. The changeset matches this scope: the…
Description check Passed The description follows the repository template. It covers ELI5, What Changed, Why, Linked Issue (Fixes #52298), Visual Proof (N/A with reason), Testing with checkboxes, Performance with a completed '…
Full details: Out of Scope Changes check

Explanation

At the reviewed head, the incremental changes are limited to opencode-go-api-key-source.ts (a cache-only shell fallback for OPENCODE_API_KEY, applied only when no environment is supplied) and a new resolver test. Both connect directly to #52298. The fallback does not change the settings, credential-database, inline, auth.json, or console-login tiers, and it does not overwrite a nonblank process key. The cookie, Claude, Codex, and WSL paths are not changed. The PR description states other changes that are not present at head: passing the captured environment from service-full-cycle-preparation.ts, removing the if (!input.environment) guard around the managed-account overlay, and copying the caller environment. The guard remains at head. These absent changes do not create out-of-scope work, but the description is stale and should be updated to match the code.

Full details: Title check

Explanation

The title names the main change: OpenCode Go API key lookup now reads from the cached login-shell environment. It also carries the conventional fix(usage) prefix. The changeset matches this scope: the resolver copies OPENCODE_API_KEY from the cached login-shell capture when the process environment has no non-blank value, plus a test.

Full details: Description check

Explanation

The description follows the repository template. It covers ELI5, What Changed, Why, Linked Issue (Fixes #52298), Visual Proof (N/A with reason), Testing with checkboxes, Performance with a completed 'None, because' line, Risk with level Low and a rollback method, the agent skill boundary, and the checklist. The AI Disclosure section is empty. Template instructions say to skip it for StablyAI team members, and the author's team membership is not stated. The Notes section from the template was also dropped. Both gaps are minor.

Full details: Linked Issues check

Explanation

Issue #52298 requires OPENCODE_API_KEY from the captured login-shell environment to reach the OpenCode Go usage fetch when the host runs under a service manager. The issue also states that the capture can be cold or empty, and that the key must fill a gap without overriding stronger sources. At the reviewed head, service-full-cycle-preparation.ts does not pass environment to fetchOpenCodeGoUsage. The previous assessment relied on that caller wiring, so it no longer applies. Instead, resolveOpenCodeGoApiKey (opencode-go-api-key-source.ts) calls resolveLoginShellEnvironment({ cachedOnly: true }) when no environment is supplied and the restored or launch environment has no nonblank OPENCODE_API_KEY. This fills the gap and keeps the existing precedence. However, the key is found only when the login-shell cache is already populated at fetch time. The reviewed evidence does not show that the cache is warmed on service start or before the periodic fetch. A new test file was added, but its contents were not inspected. The real systemd reproduction was not run.

Resolution

Show that the login-shell environment cache is populated before or during the first OpenCode Go usage fetch on a service-started host. For example, cite the warm-up call path, or run the reproduction from the issue. Also confirm that the added resolver test covers the cache-hit path.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7ea6b05d-7087-478d-9c0a-cf935494b308
Commits

Reviewing files that changed from the base of the PR and between 5be76d4 and ed041bf.

Files selected for processing: 2

  • src/main/rate-limits/providers/opencode-go/opencode-go-api-key-source.test.ts
  • src/main/rate-limits/providers/opencode-go/opencode-go-api-key-source.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

…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]>
@nwparker
nwparker force-pushed the fix/opencode-go-usage-login-shell-env branch from 5be76d4 to ed041bf Compare November 29, 2026 06:55
@nwparker nwparker changed the title fix(usage): read OpenCode Go keys from the captured login-shell environment fix(usage): read OpenCode Go API key from the cached login-shell environment Nov 29, 2026
@nwparker
nwparker merged commit a6650e9 into stablyai:main Nov 29, 2026
59 of 60 checks passed
@joei4cm
joei4cm deleted the fix/opencode-go-usage-login-shell-env branch November 29, 2026 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: OpenCode Go usage ignores the captured login-shell environment, so OPENCODE_API_KEY in ~/.bashrc never reaches a service-supervised orca serve

2 participants