Skip to content

Prefer the logged-in key over VALYU_API_KEY#72

Merged
yorkeccak merged 1 commit into
mainfrom
fix/login-key-precedence
Jul 1, 2026
Merged

Prefer the logged-in key over VALYU_API_KEY#72
yorkeccak merged 1 commit into
mainfrom
fix/login-key-precedence

Conversation

@yorkeccak

@yorkeccak yorkeccak commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Problem

A user who already has VALYU_API_KEY set in their shell runs valyu login, approves the device flow, and sees "Logged in" - but every valyu account command then fails with missing scope 'keys:read' / 'billing:read' / 'keys:write'.

Cause: key resolution precedence was flag → env → config. valyu login mints a scoped key into the config file, but the ambient VALYU_API_KEY (often an older, inference-only key) kept winning, so the freshly-minted key was never used. valyu whoami gives it away - Source: environment variable (VALYU_API_KEY). The login was effectively a silent no-op, and the only fix was to manually unset VALYU_API_KEY.

Change

Precedence is now flag → config (logged-in key) → env:

  • An explicit valyu login is a stronger signal of intent than an ambient env var, so the logged-in key wins. Users no longer need to unset VALYU_API_KEY.
  • VALYU_API_KEY remains the fallback when there is no login (CI, unauthenticated) - those flows are unchanged.
  • --api-key stays the ultimate one-off override.
  • valyu login now prints a note when VALYU_API_KEY is set, so the precedence is visible rather than surprising.

Tradeoff: a user who has both a login and VALYU_API_KEY set and wants the env var to win must now use --api-key (or valyu logout). That's the intended behaviour - the logged-in identity should be authoritative.

Tests

Rewrote config.test.ts to be hermetic (temp XDG_CONFIG_HOME, so it never reads the developer's real credentials) and added coverage:

  • logged-in key (config) beats VALYU_API_KEY ← the fix
  • flag beats both config and env
  • env fallback when there is no login / requested profile has no key

pnpm typecheck + pnpm test (49 tests) green. Bumps 1.2.0 → 1.2.1.

🤖 Generated with Claude Code


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.

Change key resolution precedence from (flag > env > config) to
(flag > config > env). A `valyu login` mints a scoped key and stores it in
the config; previously an ambient VALYU_API_KEY env var still won, so users
who already had that env var set would log in, mint a key, and silently keep
using the old env-var key - hitting confusing 'missing scope' errors on every
account command.

Now the logged-in key takes precedence, so nobody has to unset VALYU_API_KEY
after logging in. The env var remains the fallback when there is no login
(CI, unauthenticated), so those flows are unchanged, and `--api-key` stays the
ultimate one-off override. `valyu login` now also prints a note when
VALYU_API_KEY is set, so the precedence is visible.

Adds hermetic tests (temp XDG_CONFIG_HOME) covering config-over-env, flag
priority, and the no-login env fallback. Bumps to 1.2.1.
@yorkeccak yorkeccak merged commit 753b9f2 into main Jul 1, 2026
1 check passed
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.

1 participant