Skip to content

The agent that grows with you

fix(honcho): let the setup wizard's apikey answer replace a dead oauth grant

Erosika3eb3dad994a7
diff --git a/plugins/memory/honcho/cli.py b/plugins/memory/honcho/cli.py
index fb9dcd78683..6b8f38c485a 100644
--- a/plugins/memory/honcho/cli.py
+++ b/plugins/memory/honcho/cli.py
@@ -613,7 +613,7 @@ def _setup_browser_login(hermes_host: dict, write_path: Path) -> bool:
def _setup_cloud_auth(cfg: dict, hermes_host: dict, write_path: Path) -> bool:
"""Cloud auth: OAuth (browser), device code, or API key. Returns False on abort."""
cfg.pop("baseUrl", None) # cloud uses SDK default
- from plugins.memory.honcho.oauth import OAuthCredential
+ from plugins.memory.honcho.oauth import OAuthCredential, is_oauth_access_token
existing_oauth = OAuthCredential.from_host_block(hermes_host)
device_available = _device_login_available()
is_remote, can_browse = _headless()
@@ -638,14 +638,20 @@ def _setup_cloud_auth(cfg: dict, hermes_host: dict, write_path: Path) -> bool:
return _setup_device_login(hermes_host, write_path, open_browser=can_browse and not is_remote)
if method in {"oauth", "o"}:
return _setup_browser_login(hermes_host, write_path)
- print(f"\n Current API key: {_mask(cfg.get('apiKey', ''))}")
+ # A grant left on the host block shadows the key (#97990): its access token is not "current".
+ stale_grant = existing_oauth is not None or is_oauth_access_token(hermes_host.get("apiKey"))
+ current = ("" if stale_grant else hermes_host.get("apiKey", "")) or cfg.get("apiKey", "")
+ print(f"\n Current API key: {_mask(current)}")
if new_key := _prompt("Honcho API key (leave blank to keep current)", secret=True):
cfg["apiKey"] = new_key
- if cfg.get("apiKey"):
- return True
- print("\n No API key configured. Get yours at https://app.honcho.dev\n"
- " Run 'hermes honcho setup' again once you have a key.\n")
- return False
+ key = new_key or current
+ if not key:
+ print("\n No API key configured. Get yours at https://app.honcho.dev\n"
+ " Run 'hermes honcho setup' again once you have a key.\n")
+ return False
+ hermes_host.pop("oauth", None)
+ hermes_host["apiKey"] = key
+ return True
def _menu(header: str, *lines: str) -> None:
diff --git a/tests/honcho_plugin/test_cli.py b/tests/honcho_plugin/test_cli.py
index 3b3528a0cd9..487f67cfe84 100644
--- a/tests/honcho_plugin/test_cli.py
+++ b/tests/honcho_plugin/test_cli.py
@@ -793,3 +793,58 @@ class TestWriteRefusesUnparseableStore:
honcho_cli.cmd_setup(SimpleNamespace())
assert "Nothing was written" in capsys.readouterr().out
assert cfg_path.read_text(encoding="utf-8") == "{not json"
+
+
+class TestSetupApiKeyReplacesStaleGrant:
+ """#97990: choosing apikey after a revoked grant left hosts.<name>.oauth in place, so the dead
+ grant kept shadowing the fresh key across every later setup run."""
+
+ def _cfg_with_grant(self):
+ return {"hosts": {"hermes": {
+ "apiKey": "hch-at-dead",
+ "oauth": {"refreshToken": "hch-rt-dead", "expiresAt": 1, "clientId": "hermes-agent",
+ "tokenEndpoint": "https://api.honcho.dev/oauth/token"},
+ }}}
+
+ def _cloud_auth(self, monkeypatch, tmp_path, cfg, *, key_answer):
+ import plugins.memory.honcho.cli as honcho_cli
+ monkeypatch.setattr(honcho_cli, "_device_login_available", lambda: False)
+ monkeypatch.setattr(honcho_cli, "_headless", lambda: (False, True))
+ shown = []
+
+ def _prompt(label, default=None, secret=False):
+ shown.append(label)
+ return "apikey" if "OAuth" in label else key_answer
+ monkeypatch.setattr(honcho_cli, "_prompt", _prompt)
+ ok = honcho_cli._setup_cloud_auth(cfg, cfg["hosts"]["hermes"], tmp_path / "honcho.json")
+ return ok, cfg["hosts"]["hermes"]
+
+ def test_new_key_clears_oauth_and_lands_on_the_host_block(self, monkeypatch, tmp_path):
+ cfg = self._cfg_with_grant()
+ ok, host = self._cloud_auth(monkeypatch, tmp_path, cfg, key_answer="hch-v3-fresh")
+ assert ok is True
+ assert "oauth" not in host
+ assert host["apiKey"] == "hch-v3-fresh"
+ assert cfg["apiKey"] == "hch-v3-fresh"
+
+ def test_kept_root_key_still_clears_oauth(self, monkeypatch, tmp_path, capsys):
+ cfg = self._cfg_with_grant()
+ cfg["apiKey"] = "hch-v3-rootkey"
+ ok, host = self._cloud_auth(monkeypatch, tmp_path, cfg, key_answer="")
+ assert ok is True
+ assert "oauth" not in host
+ assert host["apiKey"] == "hch-v3-rootkey"
+ assert "...-rootkey" in capsys.readouterr().out
+
+ def test_dead_access_token_is_not_offered_as_current(self, monkeypatch, tmp_path, capsys):
+ cfg = self._cfg_with_grant()
+ ok, host = self._cloud_auth(monkeypatch, tmp_path, cfg, key_answer="")
+ assert ok is False
+ assert "Current API key: not set" in capsys.readouterr().out
+
+ def test_static_host_key_without_grant_is_kept(self, monkeypatch, tmp_path):
+ cfg = {"hosts": {"hermes": {"apiKey": "hch-v3-hostkey"}}}
+ ok, host = self._cloud_auth(monkeypatch, tmp_path, cfg, key_answer="")
+ assert ok is True
+ assert host["apiKey"] == "hch-v3-hostkey"
+ assert "apiKey" not in cfg