Skip to content

fix(cli): setup-opencode respects --api-key/OMNIROUTE_API_KEY over context token (#12783) - #13246

Merged
diegosouzapw merged 1 commit into
release/v3.8.51from
fix/12783-setup-opencode-key
Sep 12, 2026
Merged

diegosouzapw merged 1 commit into
release/v3.8.51from
fix/12783-setup-opencode-key

Conversation

@diegosouzapw

Copy link
Copy Markdown
Collaborator

Closes #12783

Root cause

Two compounding bugs in the setup-opencode CLI path:

  1. Flag collision: bin/cli/program.mjs registers a top-level --api-key <key> option bound to .env("OMNIROUTE_API_KEY"). Commander parses that flag against the ancestor program regardless of where it appears in argv, so omniroute setup-opencode --remote <url> --api-key sk-... stores the value on program.opts().apiKey, never on setup-opencode's own local --api-key option. The action handler only read its own local opts, so opts.apiKey was always undefined even when the user explicitly passed the flag.
  2. Precedence bug: resolveOpencodeTarget() tried the active context's accessToken/apiKey before falling back to process.env.OMNIROUTE_API_KEY. A CLI management token (oma_live_...) has no /v1/* inference scope, so a purely env-var-supplied inference key also lost to the context's management token — causing the reported 401 from /v1/models.

Fix

  • registerSetupOpencode's action handler now reads cmd.optsWithGlobals().apiKey (falling back to the local opts.apiKey) so the CLI-supplied value — wherever Commander actually stored it — reaches resolveOpencodeTarget().
  • resolveOpencodeTarget() now checks the explicit flag, then OMNIROUTE_API_KEY, and only then the active context's token — matching the explicit > ambient-env > context precedence already documented and used by bin/cli/api.mjs's buildHeaders().

Regression test (TDD)

tests/unit/repro-12783-setup-opencode-apikey.test.ts:

  • RED (pre-fix, confirmed via the plan-file's probe worktree): capturedLocalApiKey stayed undefined while program.opts().apiKey held the passed sk-... value.
  • GREEN (post-fix): 5/5 tests pass — the CLI-supplied --api-key reaches the action handler, and resolveOpencodeTarget() is unit-tested directly for all three precedence cases (flag > context, env > context, context-only fallback) plus the empty-fallback case.
✔ setup-opencode: --api-key typed AFTER the subcommand name is not stolen by the parent program's global option
✔ resolveOpencodeTarget: (a) explicit --api-key flag wins over an active context's management token
✔ resolveOpencodeTarget: (b) OMNIROUTE_API_KEY env wins over an active context's management token when no flag is passed
✔ resolveOpencodeTarget: (c) the context's token is used only when neither a flag nor the env var is set
✔ resolveOpencodeTarget: falls back to '' when neither a flag, env var, nor a resolvable context is present
ℹ tests 5, pass 5, fail 0

Gates run

  • node scripts/check/check-file-size.mjs → OK
  • node scripts/check/check-complexity.mjs → OK (2798 violations, baseline 3218)
  • node scripts/check/check-cognitive-complexity.mjs → OK (1265 violations, baseline 1437)
  • npm run typecheck:core → clean, no errors
  • npx eslint --suppressions-location config/quality/eslint-suppressions.json <changed files> → 0 errors (bin/** is globally eslint-ignored in this project; only the new test file is linted)
  • New regression test: node --import tsx/esm --test tests/unit/repro-12783-setup-opencode-apikey.test.ts → 5/5 pass
  • Existing tests of the touched area: tests/unit/cli/setup-opencode.test.ts, tests/unit/cli-setup-opencode.test.ts, tests/unit/cli/alias-resolver-7791.test.ts, tests/unit/opencode-target-format-alias-11045.test.ts, tests/unit/api/cli-tools/apply-container-guard.test.ts → 41/41 pass
  • node scripts/check/check-changelog-integrity.mjs → OK

Scope note

The same local-flag-collides-with-global-flag pattern exists in several other setup-* commands (e.g. setup-claude.mjs), but the plan-file for #12783 scopes this fix to setup-opencode.mjs only — left untouched here to keep the diff surgical to the reported issue.

⚠️ base-red inherited: #12732 — unit #12058, integration codex-cache, package-artifact, tarball-smoke, agent-skills-sync

@diegosouzapw
diegosouzapw merged commit 6207868 into release/v3.8.51 Sep 12, 2026
15 of 21 checks passed
Githab-capibara added a commit to Githab-capibara/OmniRoute that referenced this pull request Sep 17, 2026
…ntext token (cheaper-inference#12783) (cheaper-inference#13246)

Merged as part of the 39-PR owner batch of 2026-09-11, validated as a unit.

Boarded into one consolidated worktree cut from `release/v3.8.51` with the other 38 — zero conflicts between them.

- ESLint over every changed file: no errors (the only finding was one suppression entry the batch emptied, pruned on cheaper-inference#13243)
- `typecheck:core` clean; `check:dashboard-typecheck` OK (206 pre-existing, within baseline); `check:changelog-integrity` OK
- complexity 2821 / baseline 3218 and cognitive-complexity 1272 / baseline 1437 — both under baseline
- 256 assertions green: 246 under node:test and 10 under vitest, which is where `tests/unit/**/*.test.tsx` actually runs
- `check-file-size`: `chatCore.ts` rebaselined 6144 → 6146 for cheaper-inference#13278 and cheaper-inference#13276, annotated and landed on cheaper-inference#13243

⚠️ base-red inherited: cheaper-inference#12732 — the provider count (356 in the docs vs the 358 the modules define) and `open-sse/utils/stream.ts` at 3115 > frozen 3098 both reproduce on the pure tip with zero contribution from this batch.
muhamadgalihsaputra pushed a commit to niyatna/NiyatnaRoute that referenced this pull request Sep 27, 2026
…ntext token (cheaper-inference#12783) (cheaper-inference#13246)

Merged as part of the 39-PR owner batch of 2026-09-11, validated as a unit.

Boarded into one consolidated worktree cut from `release/v3.8.51` with the other 38 — zero conflicts between them.

- ESLint over every changed file: no errors (the only finding was one suppression entry the batch emptied, pruned on cheaper-inference#13243)
- `typecheck:core` clean; `check:dashboard-typecheck` OK (206 pre-existing, within baseline); `check:changelog-integrity` OK
- complexity 2821 / baseline 3218 and cognitive-complexity 1272 / baseline 1437 — both under baseline
- 256 assertions green: 246 under node:test and 10 under vitest, which is where `tests/unit/**/*.test.tsx` actually runs
- `check-file-size`: `chatCore.ts` rebaselined 6144 → 6146 for cheaper-inference#13278 and cheaper-inference#13276, annotated and landed on cheaper-inference#13243

⚠️ base-red inherited: cheaper-inference#12732 — the provider count (356 in the docs vs the 358 the modules define) and `open-sse/utils/stream.ts` at 3115 > frozen 3098 both reproduce on the pure tip with zero contribution from this batch.
@diegosouzapw
diegosouzapw deleted the fix/12783-setup-opencode-key branch October 18, 2026 23:48
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.

fix(cli): setup-opencode ignores --api-key/OMNIROUTE_API_KEY when a context is active, sends context's management token to /v1/models (401)

1 participant