Skip to content

The agent that grows with you

fix(honcho): keep a rotation on honcho.json when the read was seeded from another file

Erosika2f81f6a83171
diff --git a/plugins/memory/honcho/cli.py b/plugins/memory/honcho/cli.py
index 7c8598d3a50..411ce049d2a 100644
--- a/plugins/memory/honcho/cli.py
+++ b/plugins/memory/honcho/cli.py
@@ -119,9 +119,22 @@ def _apply_edits(base: dict, edited: dict, current: dict) -> dict:
return out
+def _overlay_local(seed: dict, local: dict) -> dict:
+ """Return ``seed`` with ``local``'s root keys and ``hosts.<h>.<k>`` keys on top. The local file wins,
+ so a grant or rotation it already holds is what the command's edits land on."""
+ out = copy.deepcopy(seed)
+ for key, value in local.items():
+ if key != "hosts":
+ out[key] = copy.deepcopy(value)
+ for host, block in (local.get("hosts") or {}).items():
+ out.setdefault("hosts", {}).setdefault(host, {}).update(copy.deepcopy(block))
+ return out
+
+
def _write_config(cfg: dict, path: Path | None = None) -> None:
"""Persist ``cfg`` under the token refresh's cross-process lock. The object _read_config() returned
- for this path has only its edits applied onto a fresh read of disk; a plain dict is written whole."""
+ has only its edits applied onto a fresh read of disk; a plain dict is written whole. A read that
+ resolved to a seed file (~/.honcho or a profile) is written whole only while ``path`` does not exist."""
from plugins.memory.honcho.oauth import _config_refresh_lock, _read_config_strict
from utils import atomic_json_write
path = path or _local_config_path()
@@ -129,6 +142,8 @@ def _write_config(cfg: dict, path: Path | None = None) -> None:
_refuse_unparseable(path)
if getattr(cfg, "path", None) == path:
cfg = _apply_edits(cfg.snapshot, cfg, _read_config_strict(path))
+ elif isinstance(cfg, _ReadConfig) and path.exists():
+ cfg = _apply_edits(cfg.snapshot, cfg, _overlay_local(cfg.snapshot, _read_config_strict(path)))
path.parent.mkdir(parents=True, exist_ok=True)
atomic_json_write(path, cfg, mode=0o600)
diff --git a/tests/honcho_plugin/test_cli.py b/tests/honcho_plugin/test_cli.py
index 257f04c86b7..954ac7120d9 100644
--- a/tests/honcho_plugin/test_cli.py
+++ b/tests/honcho_plugin/test_cli.py
@@ -907,6 +907,39 @@ class TestWriteConfigMergesOntoDisk:
assert out["apiKey"] == "hch-at-new" and out["oauth"] == {"refreshToken": "hch-rt-new"}
assert out["peerName"] == "alice" and out["recallMode"] == "tools"
+ _SEED = {"dialecticCadence": 3, "hosts": {"hermes": {"peerName": "alice"}}}
+
+ def _seeded(self, monkeypatch, tmp_path):
+ seed, local = tmp_path / "seed.json", tmp_path / "honcho.json"
+ seed.write_text(json.dumps(self._SEED))
+ return _point_cli_at(monkeypatch, local, _config_path=lambda: seed, _host_key=lambda: "hermes"), local
+
+ def test_a_read_seeded_from_another_file_applies_only_its_edits_onto_the_local_file(self, monkeypatch, tmp_path):
+ import plugins.memory.honcho.oauth as oauth
+ honcho_cli, local = self._seeded(monkeypatch, tmp_path)
+ cfg = honcho_cli._read_config()
+ grant = {"access_token": "hch-at-login", "refresh_token": "hch-rt-login", "expires_in": 3600}
+ cred = oauth.install_grant(local, "hermes", grant, client_id="c", token_endpoint="e", apply_config=False)
+ honcho_cli._apply_grant_to_host(cfg, cfg["hosts"]["hermes"], cred)
+ rotated = oauth.OAuthCredential.from_token_response(
+ {"access_token": "hch-at-new", "refresh_token": "hch-rt-new", "expires_in": 3600},
+ now=0.0, client_id="c", token_endpoint="e")
+ oauth._persist_credential(local, "hermes", rotated)
+ cfg["hosts"]["hermes"]["recallMode"] = "tools"
+ honcho_cli._write_config(cfg)
+ out = json.loads(local.read_text())
+ assert out["hosts"]["hermes"]["apiKey"] == "hch-at-new"
+ assert out["hosts"]["hermes"]["oauth"]["refreshToken"] == "hch-rt-new"
+ assert out["hosts"]["hermes"]["recallMode"] == "tools" and out["hosts"]["hermes"]["peerName"] == "alice"
+ assert out["dialecticCadence"] == 3
+
+ def test_a_read_seeded_from_another_file_is_written_whole_while_no_local_file_exists(self, monkeypatch, tmp_path):
+ honcho_cli, local = self._seeded(monkeypatch, tmp_path)
+ cfg = honcho_cli._read_config()
+ cfg["hosts"]["hermes"]["recallMode"] = "tools"
+ honcho_cli._write_config(cfg)
+ assert json.loads(local.read_text()) == {"dialecticCadence": 3, "hosts": {"hermes": {"peerName": "alice", "recallMode": "tools"}}}
+
@pytest.mark.parametrize("build", [lambda cli: {"hosts": {"other": {"apiKey": "o"}}}, lambda cli: dict(cli._read_config())],
ids=["never read", "rebuilt from the read"])
def test_a_plain_dict_is_written_whole(self, monkeypatch, tmp_path, build):