Skip to content

fix: clear stale api_key on provider switch#18906

Closed
zons-zhaozhy wants to merge 4 commits into
NousResearch:mainfrom
zons-zhaozhy:fix/api-key-drift-on-provider-switch
Closed

fix: clear stale api_key on provider switch#18906
zons-zhaozhy wants to merge 4 commits into
NousResearch:mainfrom
zons-zhaozhy:fix/api-key-drift-on-provider-switch

Conversation

@zons-zhaozhy

Copy link
Copy Markdown

Summary

When switching providers via hermes model, the previous provider's
api_key (if stored in config.yaml model.api_key) was not cleared,
causing cross-provider key contamination.

Reproduction

  1. Configure OpenRouter with an API key → key gets saved to model.api_key
  2. Switch to Ollama Cloud (or any provider that uses env vars / OAuth)
  3. The stale OpenRouter key remains in model.api_key
  4. New provider receives wrong key → 401 auth errors

Fix

Add model.pop("api_key", None) to all 14 provider switch paths in
hermes_cli/main.py, mirroring the existing correct behavior in
auth.py:3643.

  • Standard providers (OpenRouter, Anthropic, Bedrock, Copilot, Kimi,
    Stepfun, Azure Foundry, API-key providers): unconditional pop — these
    authenticate via env vars or OAuth and never write model.api_key.
  • Custom providers (_model_flow_custom, _model_flow_named_custom):
    conditional write — set api_key when user provides one, pop when absent.

Scope

  • 1 file changed: hermes_cli/main.py (+29 lines)
  • No behavioral change for existing correct flows
  • Purely defensive cleanup of stale state

Fixes #14134

zzy-git added 4 commits April 30, 2026 22:35
…elapsed to footer

Minimal change to response box framing:
- Header (╭): appends HH:MM:SS after the branding label
- Footer (╰): appends end time (HH:MM:SS) and elapsed duration (e.g. 3.2s or 1m38s) on the right side

Uses existing _prompt_start_time for elapsed calculation, so no new state needed.
No config toggle — always visible (matches status bar behavior).
…splay

Each streaming response within a turn now gets its own _stream_box_start
timestamp.  Header shows when that specific response started; footer
shows elapsed for that response only — not cumulative turn time.

Previously _prompt_start_time (set once per turn, cleared after the
agent thread exits) was reused, so every sub-response box accumulated
elapsed from the turn start.
When switching providers via `hermes model`, the previous provider's
api_key (if stored in config.yaml model.api_key) was not cleared,
causing cross-provider key contamination. For example, switching from
OpenRouter to Ollama Cloud would leave the OpenRouter key in place,
leading to auth errors on the new provider.

Add model.pop("api_key", None) to all 14 provider switch paths in
main.py, mirroring the existing correct behavior in auth.py:3643.

Custom provider paths (_model_flow_custom, _model_flow_named_custom)
retain their conditional write: set api_key when present, pop when
absent. All other providers authenticate via env vars or OAuth and
never write model.api_key, so the pop is purely defensive.

Fixes NousResearch#14134
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/cli CLI entry point, hermes_cli/, setup wizard area/auth Authentication, OAuth, credential pools area/config Config system, migrations, profiles labels May 2, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #14373 — both clear stale model.api_key on provider switch. Fixes #14134. Also related to #8571 (same pattern in _model_flow_named_custom).

@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #14373

@alt-glitch alt-glitch marked this as a duplicate of #14373 May 2, 2026
@zons-zhaozhy

Copy link
Copy Markdown
Author

Duplicate of #14373 (submitted Apr 23, earlier fix for the same issue #14134). Closing in favor of the existing PR.

@zons-zhaozhy zons-zhaozhy deleted the fix/api-key-drift-on-provider-switch branch May 4, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/auth Authentication, OAuth, credential pools area/config Config system, migrations, profiles comp/cli CLI entry point, hermes_cli/, setup wizard P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: api_key drift on provider switch — real Hermes bug

3 participants