Skip to content

fix(cli): validate a community's own Anthropic key, not just OpenRouter - #391

Merged
neuromechanist merged 2 commits into
feature/issue-360-epic-anthropic-platformfrom
fix/issue-390-validate-anthropic-key
Sep 17, 2026
Merged

neuromechanist merged 2 commits into
feature/issue-360-epic-anthropic-platformfrom
fix/issue-390-validate-anthropic-key

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Closes #390. Part of #360.

Found while writing the Phase 5 troubleshooting docs: osa validate had not been migrated with the rest of the platform.

What was wrong

src/cli/validate.py read only openrouter_api_key_env_var. A community that funds its own Anthropic usage through anthropic_api_key_env_var fell into the else branch and was told the opposite of the truth:

API Key Env Var    Not configured (using platform key)

It also never got the missing-env-var warning, which is the one that exists to prevent surprise platform billing. --test-api-key could only test an OpenRouter key, and said so in its own help text.

The runtime already resolves this correctly, in both places that matter: _resolve_provider (src/api/routers/community.py) and src/api/routers/health.py:30.

What changed

  • _community_key_env_var resolves the community's own key env var with the runtime's precedence, Anthropic first, and returns the provider that owns it. Output now names the provider: ✓ ANTHROPIC_API_KEY_HED is set (Anthropic).
  • --test-api-key tests the key against that provider. A community Anthropic key goes to the first-party GET /v1/models with x-api-key, because create_anthropic_llm pins base_url to the first-party API whenever an explicit key is passed, so that is the endpoint the community's requests use. Probing the AWS platform endpoint instead would fail a perfectly good key.
  • The endpoint constant moved to a new dependency-free src/core/services/anthropic_endpoints.py. Importing it from anthropic_llm.py instead would have been a quiet regression: that module needs langchain-anthropic from the server extra, and src/cli/main.py replaces osa validate with a "requires server dependencies" stub when src/cli/validate.py fails to import, so CLI-only installs would have lost file-mode validation entirely. Caught by trying it.

Tests

  • Env var resolution through the CLI: Anthropic reported when set, warned when unset, and preferred when both fields are configured; platform key when neither is.
  • --test-api-key routing per provider, asserted at the HTTP boundary (respx) on URL and auth header, plus the 401 path.
  • One network-marked test against the live endpoint, which is the assertion a fixture cannot make: that the path and header names are the ones Anthropic actually accepts (a wrong path answers 404, and a fixture would happily fake either).
  • A subprocess test that imports src/cli/validate.py with the server extra blocked, so the packaging trap above cannot come back. It is paired with a test that the blocker really blocks, so it cannot pass vacuously.

Each fix was verified by reverting it and confirming the new tests fail: removing the Anthropic precedence fails 6 tests, importing the constant from anthropic_llm fails the CLI-only import test.

Full offline suite: 2115 passed. The one failure, test_version_shows_version, is pre-existing on the base branch (Rich ANSI codes in the version string).

The CLI's light paths cannot import anthropic_llm: it pulls in
langchain-anthropic from the server extra, and src/cli/main.py downgrades
osa validate to a stub when importing src/cli/validate.py fails. A
dependency-free module lets the validator use the same endpoint constant
the runtime does.

No behavior change.
osa validate read only openrouter_api_key_env_var, so a community funding
its own Anthropic usage was told 'Not configured (using platform key)' and
never got the warning that exists to catch surprise platform billing.
Resolve the env var with the same precedence the runtime uses (Anthropic
first, matching _resolve_provider and health.py) and report which provider
owns it.

--test-api-key now tests the key against its own provider: a community
Anthropic key against the first-party /v1/models, because create_anthropic_llm
pins base_url there whenever an explicit key is passed, so that is where the
community's requests go. The flag's help text no longer says OpenRouter.

Tests cover both providers, the precedence between them, and that
src/cli/validate.py still imports with the server extra blocked, which is
what keeps file-mode validation working on a CLI-only install. Verified by
reverting each fix and watching the new tests fail.

Closes #390
@neuromechanist
neuromechanist merged commit b60b367 into feature/issue-360-epic-anthropic-platform Sep 17, 2026
3 of 4 checks passed
@neuromechanist
neuromechanist deleted the fix/issue-390-validate-anthropic-key branch September 17, 2026 16:20
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