Skip to content

The agent that grows with you

fix(honcho): cli writes hold the refresh lock and merge only their edits onto disk

Erosika69667057519e
diff --git a/plugins/memory/honcho/cli.py b/plugins/memory/honcho/cli.py
index b453ad8a455..72c75dcfe6a 100644
--- a/plugins/memory/honcho/cli.py
+++ b/plugins/memory/honcho/cli.py
@@ -66,8 +66,21 @@ def _local_config_path() -> Path:
return get_hermes_home() / "honcho.json"
+class _ReadConfig(dict):
+ """The config a command works on, carrying what disk held when it was read (``snapshot``) and where
+ (``path``), so _write_config() can apply only the command's edits onto a fresh read."""
+
+ snapshot: dict
+ path: Path
+
+
def _read_config() -> dict:
- return read_json_or_empty(_config_path())
+ import copy
+ path = _config_path()
+ raw = read_json_or_empty(path)
+ cfg = _ReadConfig(raw)
+ cfg.snapshot, cfg.path = copy.deepcopy(raw), path
+ return cfg
class ConfigWriteRefused(Exception):
@@ -85,12 +98,46 @@ def _refuse_unparseable(path: Path) -> None:
"Fix or move the file, then re-run.") from e
+def _apply_edits(base: dict, edited: dict, current: dict) -> dict:
+ """Return ``current`` with every root key and ``hosts.<h>.<k>`` the command changed between ``base``
+ and ``edited`` applied. Keys the command left alone keep their on-disk value, so a token rotation
+ that landed while the command ran survives; a key the command set deliberately wins."""
+ import copy
+ out = copy.deepcopy(current)
+ for key in (set(base) | set(edited)) - {"hosts"}:
+ if key in edited and edited[key] != base.get(key):
+ out[key] = copy.deepcopy(edited[key])
+ elif key not in edited and key in base:
+ out.pop(key, None)
+ base_hosts, edited_hosts = base.get("hosts") or {}, edited.get("hosts") or {}
+ out_hosts = out.setdefault("hosts", {}) if (base_hosts or edited_hosts or "hosts" in current) else None
+ for host in set(base_hosts) | set(edited_hosts):
+ if host not in edited_hosts:
+ out_hosts.pop(host, None)
+ continue
+ b, e = base_hosts.get(host) or {}, edited_hosts[host]
+ block = out_hosts.setdefault(host, {})
+ for key in set(b) | set(e):
+ if key in e and e[key] != b.get(key):
+ block[key] = copy.deepcopy(e[key])
+ elif key not in e and key in b:
+ block.pop(key, None)
+ return out
+
+
def _write_config(cfg: dict, path: Path | None = None) -> None:
- path = path or _local_config_path()
- _refuse_unparseable(path)
- path.parent.mkdir(parents=True, exist_ok=True)
+ """Persist ``cfg`` under the same cross-process lock the token refresh holds. When ``cfg`` is the
+ object _read_config() returned for this path, only the command's edits are applied onto a fresh read
+ of disk; a plain dict is written whole."""
+ from plugins.memory.honcho.oauth import _config_refresh_lock, _read_config_strict
from utils import atomic_json_write
- atomic_json_write(path, cfg, mode=0o600)
+ path = path or _local_config_path()
+ with _config_refresh_lock(path):
+ _refuse_unparseable(path)
+ if getattr(cfg, "path", None) == path:
+ cfg = _apply_edits(cfg.snapshot, cfg, _read_config_strict(path))
+ path.parent.mkdir(parents=True, exist_ok=True)
+ atomic_json_write(path, cfg, mode=0o600)
def _label(host: str) -> str:
diff --git a/tests/honcho_plugin/test_cli.py b/tests/honcho_plugin/test_cli.py
index 6f8f412a7d3..6587687d2ac 100644
--- a/tests/honcho_plugin/test_cli.py
+++ b/tests/honcho_plugin/test_cli.py
@@ -774,12 +774,11 @@ class TestWriteRefusesUnparseableStore:
honcho_cli._write_config({"apiKey": "k"})
assert json.loads(cfg_path.read_text(encoding="utf-8")) == {"apiKey": "k"}
- def test_enable_prints_one_sentence_and_writes_nothing(self, monkeypatch, tmp_path, capsys):
+ def test_command_prints_one_sentence_and_writes_nothing(self, monkeypatch, tmp_path, capsys):
cfg_path = tmp_path / "honcho.json"
cfg_path.write_text("{not json", encoding="utf-8")
honcho_cli = self._point_at(monkeypatch, cfg_path)
- monkeypatch.setenv("HONCHO_API_KEY", "env-key")
- honcho_cli.honcho_command(SimpleNamespace(honcho_command="enable", target_profile=None))
+ honcho_cli.honcho_command(SimpleNamespace(honcho_command="mode", mode="tools", target_profile=None))
out = capsys.readouterr().out
assert "could not be read as JSON" in out
assert "Nothing was written" in out
@@ -904,6 +903,7 @@ class TestEnabledRequiresACredential:
assert honcho_cli.clone_honcho_for_profile("dreamer") is True
assert written["cfg"]["hosts"]["hermes_dreamer"]["enabled"] is True
+
def test_clone_with_self_hosted_url_is_enabled(self, monkeypatch, tmp_path):
cfg = {"baseUrl": "http://localhost:8000", "hosts": {"hermes": {"workspace": "hermes"}}}
honcho_cli, written = self._env(monkeypatch, tmp_path, cfg)
@@ -942,3 +942,82 @@ class TestEnabledRequiresACredential:
out = capsys.readouterr().out
assert "Run 'hermes honcho setup' to sign in" in out
assert written == {}
+
+
+class TestWriteConfigMergesOntoDisk:
+ """A command reads honcho.json, works, then writes. A refresh in another process may have rotated
+ the token in between; the write must keep that rotation and apply only the command's own edits."""
+
+ def _paths(self, monkeypatch, tmp_path, disk):
+ import plugins.memory.honcho.cli as honcho_cli
+ cfg_path = tmp_path / "honcho.json"
+ cfg_path.write_text(json.dumps(disk))
+ monkeypatch.setattr(honcho_cli, "_config_path", lambda: cfg_path)
+ monkeypatch.setattr(honcho_cli, "_local_config_path", lambda: cfg_path)
+ return honcho_cli, cfg_path
+
+ def _rotated(self, cfg_path, disk):
+ disk["hosts"]["hermes"]["apiKey"] = "hch-at-new"
+ disk["hosts"]["hermes"]["oauth"] = {"refreshToken": "hch-rt-new"}
+ cfg_path.write_text(json.dumps(disk))
+
+ def test_rotation_between_read_and_write_survives(self, monkeypatch, tmp_path):
+ disk = {"apiKey": "root", "hosts": {"hermes": {"apiKey": "hch-at-old", "oauth": {"refreshToken": "hch-rt-old"},
+ "recallMode": "hybrid"}}}
+ honcho_cli, cfg_path = self._paths(monkeypatch, tmp_path, disk)
+ cfg = honcho_cli._read_config()
+ self._rotated(cfg_path, json.loads(cfg_path.read_text()))
+ cfg["hosts"]["hermes"]["recallMode"] = "tools"
+ cfg["dialecticCadence"] = 3
+ honcho_cli._write_config(cfg)
+ out = json.loads(cfg_path.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["dialecticCadence"] == 3
+
+ def test_a_credential_the_command_set_wins(self, monkeypatch, tmp_path):
+ disk = {"hosts": {"hermes": {"apiKey": "hch-at-old", "oauth": {"refreshToken": "hch-rt-old"}}}}
+ honcho_cli, cfg_path = self._paths(monkeypatch, tmp_path, disk)
+ cfg = honcho_cli._read_config()
+ self._rotated(cfg_path, json.loads(cfg_path.read_text()))
+ cfg["hosts"]["hermes"]["apiKey"] = "hch-v3-pasted"
+ cfg["hosts"]["hermes"].pop("oauth")
+ honcho_cli._write_config(cfg)
+ out = json.loads(cfg_path.read_text())
+ assert out["hosts"]["hermes"]["apiKey"] == "hch-v3-pasted" and "oauth" not in out["hosts"]["hermes"]
+
+ def test_a_key_the_command_removed_is_removed(self, monkeypatch, tmp_path):
+ disk = {"hosts": {"hermes": {"apiKey": "k", "runtimePeerPrefix": "tg_"}}}
+ honcho_cli, cfg_path = self._paths(monkeypatch, tmp_path, disk)
+ cfg = honcho_cli._read_config()
+ cfg["hosts"]["hermes"].pop("runtimePeerPrefix")
+ honcho_cli._write_config(cfg)
+ assert "runtimePeerPrefix" not in json.loads(cfg_path.read_text())["hosts"]["hermes"]
+
+ def test_write_without_a_prior_read_writes_the_whole_config(self, monkeypatch, tmp_path):
+ honcho_cli, cfg_path = self._paths(monkeypatch, tmp_path, {"hosts": {"hermes": {"apiKey": "k"}}})
+ honcho_cli._write_config({"hosts": {"other": {"apiKey": "o"}}})
+ assert json.loads(cfg_path.read_text()) == {"hosts": {"other": {"apiKey": "o"}}}
+
+ def test_a_rebuilt_plain_dict_is_written_whole(self, monkeypatch, tmp_path):
+ disk = {"hosts": {"hermes": {"apiKey": "hch-at-old", "recallMode": "hybrid"}}}
+ honcho_cli, cfg_path = self._paths(monkeypatch, tmp_path, disk)
+ cfg = dict(honcho_cli._read_config())
+ self._rotated(cfg_path, json.loads(cfg_path.read_text()))
+ honcho_cli._write_config(cfg)
+ assert json.loads(cfg_path.read_text())["hosts"]["hermes"]["apiKey"] == "hch-at-old"
+
+ def test_write_holds_the_refresh_file_lock(self, monkeypatch, tmp_path):
+ import contextlib
+ import plugins.memory.honcho.oauth as oauth
+ honcho_cli, cfg_path = self._paths(monkeypatch, tmp_path, {})
+ locked = []
+
+ @contextlib.contextmanager
+ def _lock(path):
+ locked.append(path)
+ yield
+
+ monkeypatch.setattr(oauth, "_config_refresh_lock", _lock)
+ honcho_cli._write_config({"apiKey": "k"})
+ assert locked == [cfg_path]