From 2dc822066f4f244063469f8eeb02ab9bc04b6159 Mon Sep 17 00:00:00 2001 From: Michael Gasperini <163041857+TheStreamCode@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:22:39 +0100 Subject: [PATCH] fix(auth): read the login cache through the credential hook The provider exported MUSE_CODE_SUB_TOKEN at import, so a resolver running in another process never saw the key: under plugins.isolation: host the profile is a host proxy, and a gateway that started before the login never re-imports the module. Both report "No usable credentials found for provider 'muse-code'" while ~/.hermes/muse-code-sub.json holds a valid key. Resolve through ProviderProfile.resolve_api_key_secret(), which Hermes consults before the env ladder and which crosses the plugin-host boundary. An explicit MUSE_CODE_SUB_TOKEN that differs from the cache still wins. Fixes #18 --- CHANGELOG.md | 11 +++++++++++ README.md | 14 +++++++++---- __init__.py | 34 ++++++++++++++++++++++++++++++++ tests/test_muse_code_provider.py | 29 +++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 373f3e5..975f432 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index f7e7dfb..2d42747 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/__init__.py b/__init__.py index 790318e..ec5e937 100644 --- a/__init__.py +++ b/__init__.py @@ -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. @@ -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, *, diff --git a/tests/test_muse_code_provider.py b/tests/test_muse_code_provider.py index 16e35d7..2e3deaa 100644 --- a/tests/test_muse_code_provider.py +++ b/tests/test_muse_code_provider.py @@ -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")