Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project are documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Credential resolution reads the login cache through `ProviderProfile.resolve_api_key_secret()`
instead of relying on the `MUSE_CODE_SUB_TOKEN` export. The export only happens in the process
that imports the plugin, so under `plugins.isolation: host` (the profile runs as a host proxy) or
in a gateway that started before the login, Hermes reported "No usable credentials found for
provider 'muse-code'" while the cache held a valid key. An explicit `MUSE_CODE_SUB_TOKEN` that
differs from the cache still wins.

## [0.6.3] - 2026-11-11

### Changed
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ extends that consent to those calls too. Pick the plain id when you want the sta
No completed login was found: run `hermes auth add muse-code`. A missing or empty cache means no
login completed yet under the same `HERMES_HOME` as the Hermes process.

If `hermes auth status muse-code` reports a login but chat says "No usable credentials found for
provider 'muse-code'", the running Hermes has a plugin older than 0.6.4, which only exported the key
inside the process that imported it. Update the plugin and restart the gateway.

### Meta rejects requests from this client

A 403 with `client_attestation_denied` means Meta applied its client restriction (Muse Code
Expand All @@ -139,10 +143,12 @@ Meta's terms.

### The gateway keeps the old key

The cache is read once per process: `hermes chat` always sees a fresh login, but restart gateway
workers after each login. After `hermes plugins update muse-code-subscription`,
likewise restart: the running gateway keeps the previous plugin version until `hermes gateway
restart --yes`.
Since 0.6.4 the provider reads the cache when Hermes resolves the credential, so a completed login
is used by the next turn without a restart. Older versions exported the key only at import, which
left workers that started before the login on the previous key or on none at all.

Restart after `hermes plugins update muse-code-subscription`: the running gateway keeps the previous
plugin version until `hermes gateway restart --yes`.

### Requests fail with 402 billing_not_configured

Expand Down
34 changes: 34 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ def _ensure_subscription_key() -> None:
_ENV_KEY_OWNED = None


def _explicit_env_key() -> str:
"""``MUSE_CODE_SUB_TOKEN`` as a user set it (``.env`` first, then the environment).

Hermes prefers ``$HERMES_HOME/.env`` over a stale shell export; use its own
helper so this keeps that order instead of a bare ``os.getenv``.
"""
try:
from agent.credential_pool import get_env_prefer_dotenv

value = get_env_prefer_dotenv(ENV_VAR)
except Exception: # noqa: BLE001 - hook absent on older hosts or in a stub tree
value = os.getenv(ENV_VAR, "")
return (value or "").strip()


def _adapter_base_url() -> str:
"""Start the loopback wire adapter once and return the provider base URL.

Expand Down Expand Up @@ -245,6 +260,25 @@ def runtime_base_url(
"""
return self.base_url or None

def resolve_api_key_secret(self) -> tuple[str, str] | None:
"""Hand Hermes the cached key directly instead of an env side effect.

``_ensure_subscription_key`` fills ``MUSE_CODE_SUB_TOKEN`` at import,
in this process only: a resolver that runs elsewhere (a plugin-host
profile under ``plugins.isolation: host``, or a gateway that started
before the login) never observes it, and then the provider reports
"No usable credentials". The cache file has no such scope, so read it
here. An explicit env value that differs from the cache stays
authoritative: returning None leaves that value to core's ladder.
"""
cached = read_cached_key()
if not cached:
return None
explicit = _explicit_env_key()
if explicit and explicit != cached:
return None
return cached, "muse-code-sub"

def fetch_models(
self,
*,
Expand Down
29 changes: 29 additions & 0 deletions tests/test_muse_code_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,35 @@ def test_cached_key_resolution(tmp_path, monkeypatch):
assert os.environ["MUSE_CODE_SUB_TOKEN"] == "LLM|k"


def test_api_key_secret_hook_reads_the_cache(tmp_path, monkeypatch):
"""The credential must come from the hook, not the env var it mirrors.

``MUSE_CODE_SUB_TOKEN`` is only set in the process that imported the plugin:
a resolver running in another process (plugin-host profile) or in a gateway
that outlived the login never sees it and reports "No usable credentials".
"""
cache = tmp_path / "cache.json"
cache.write_text(json.dumps({"apiKey": "LLM|seam"}))
monkeypatch.setenv("MUSE_CODE_SUB_CREDENTIALS", str(cache))
_, registered = _load_plugin()
profile = registered["muse-code"]
assert profile.resolve_api_key_secret() == ("LLM|seam", "muse-code-sub")


def test_api_key_secret_hook_leaves_an_explicit_env_value_alone(tmp_path, monkeypatch):
cache = tmp_path / "cache.json"
cache.write_text(json.dumps({"apiKey": "LLM|cached"}))
monkeypatch.setenv("MUSE_CODE_SUB_CREDENTIALS", str(cache))
_, registered = _load_plugin(env={"MUSE_CODE_SUB_TOKEN": "LLM|explicit"})
assert registered["muse-code"].resolve_api_key_secret() is None


def test_api_key_secret_hook_is_a_miss_without_a_cache(tmp_path, monkeypatch):
monkeypatch.setenv("MUSE_CODE_SUB_CREDENTIALS", str(tmp_path / "absent.json"))
_, registered = _load_plugin()
assert registered["muse-code"].resolve_api_key_secret() is None


def test_base_url_uses_the_loopback_adapter(tmp_path, monkeypatch):
monkeypatch.setenv("MUSE_CODE_SUB_CREDENTIALS", str(tmp_path / "c.json"))
monkeypatch.setenv("META_BASE_URL", "https://attacker.test/v1")
Expand Down
Loading