diff --git a/plugins/memory/honcho/cli.py b/plugins/memory/honcho/cli.py index 411ce049d2..36a5c94703 100644 --- a/plugins/memory/honcho/cli.py +++ b/plugins/memory/honcho/cli.py @@ -140,12 +140,15 @@ def _write_config(cfg: dict, path: Path | None = None) -> None: path = path or _local_config_path() with _config_refresh_lock(path): _refuse_unparseable(path) + out = cfg if getattr(cfg, "path", None) == path: - cfg = _apply_edits(cfg.snapshot, cfg, _read_config_strict(path)) + out = _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))) + out = _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) + atomic_json_write(path, out, mode=0o600) + if isinstance(cfg, _ReadConfig): # a later write on the same object applies only edits made after this one + cfg.snapshot, cfg.path = copy.deepcopy(dict(cfg)), path def _label(host: str) -> str: diff --git a/tests/honcho_plugin/test_cli.py b/tests/honcho_plugin/test_cli.py index 954ac7120d..201bbf2885 100644 --- a/tests/honcho_plugin/test_cli.py +++ b/tests/honcho_plugin/test_cli.py @@ -892,6 +892,18 @@ class TestWriteConfigMergesOntoDisk: honcho_cli._write_config(cfg) assert json.loads(cfg_path.read_text())["hosts"]["hermes"] == {"apiKey": "hch-v3-pasted"} + def test_a_second_write_on_the_same_read_applies_only_the_edits_made_since_the_first(self, monkeypatch, tmp_path): + disk = {"hosts": {"hermes": {"apiKey": "hch-at-old", "oauth": {"refreshToken": "hch-rt-old"}, "workspace": "A"}}} + honcho_cli, cfg_path = self._paths(monkeypatch, tmp_path, disk) + cfg = honcho_cli._read_config() + cfg["hosts"]["hermes"]["workspace"] = "B" + honcho_cli._write_config(cfg) + self._rotate_on_disk(cfg_path) + cfg["hosts"]["hermes"]["workspace"] = "A" + honcho_cli._write_config(cfg) + out = json.loads(cfg_path.read_text())["hosts"]["hermes"] + assert out == {"apiKey": "hch-at-new", "oauth": {"refreshToken": "hch-rt-new"}, "workspace": "A"} + def test_a_grant_the_login_installed_yields_to_a_later_rotation(self, monkeypatch, tmp_path): import plugins.memory.honcho.oauth as oauth honcho_cli, cfg_path = self._paths(monkeypatch, tmp_path, {"hosts": {"hermes": {"peerName": "alice"}}})