fix(honcho): advance the read baseline after each write so a revert reaches disk

_write_config applied a command's edits relative to the snapshot the read took, but never moved that snapshot after a successful write. A second write on the same object therefore compared A -> B -> A against A, saw no change, and left disk at B. After a write the snapshot and path now follow the caller's dict, so the next write applies only the edits made since.
This commit is contained in:
Erosika
2026-09-09 16:40:23 -04:00
committed by kshitij
parent 2f81f6a831
commit 56231e51ad
2 changed files with 18 additions and 3 deletions
+6 -3
View File
@@ -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:
+12
View File
@@ -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"}}})