The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(config): guard against scalar overwrite of mapping sections (#74995)
Baophan0099cfa8f06392
diff --git a/hermes_cli/config.py b/hermes_cli/config.pyindex 590de96b754..a00d37643bb 100644--- a/hermes_cli/config.py+++ b/hermes_cli/config.py@@ -4856,6 +4856,56 @@ def set_config_value(key: str, value: str, force: bool = False):coerced_value = float(value)value = coerced_value+ # Guard against #74995: a single-segment key that names an existing+ # mapping would silently overwrite the entire section with a scalar+ # (e.g. ``hermes config set model gpt-5.6-sol`` when model already+ # contains default/provider/context_length). Bare ``model`` is a+ # documented shorthand — redirect to ``model.default`` and preserve+ # siblings. All other mapping sections are rejected unless --force.+ if "." not in key:+ _existing = user_config.get(key)+ if isinstance(_existing, dict):+ if key == "model":+ # Redirect bare-model shorthand to model.default while+ # keeping every sibling mapping key intact.+ key = "model.default"+ print(+ f"✓ Redirecting bare 'model' to 'model.default' "+ f"(preserving {len(_existing)} existing model sub-key(s))"+ )+ # value was already coerced above; proceed to _set_nested+ elif not force:+ _sub = [k for k in _existing if isinstance(k, str)]+ print(+ f"✗ Cannot set '{key}' to a scalar — '{key}' is a "+ f"configuration section with {len(_sub)} sub-key(s).",+ file=sys.stderr,+ )+ if _sub:+ _sub_list = ", ".join(_sub[:8])+ print(f" Sub-keys: {_sub_list}", file=sys.stderr)+ if len(_sub) > 8:+ print(+ f" ... and {len(_sub) - 8} more",+ file=sys.stderr,+ )+ print(+ " Use a dotted path to set a specific leaf key:",+ file=sys.stderr,+ )+ print(+ f" hermes config set {key}.<sub-key> <value>",+ file=sys.stderr,+ )+ print(+ " Or use --force to replace the entire section:",+ file=sys.stderr,+ )+ print(+ f" hermes config set --force {key} {value!r}",+ file=sys.stderr,+ )+ sys.exit(1)_set_nested(user_config, key, value)# Normalize the api_base → base_url alias at set-time too (issue #8919),# so a fresh `hermes config set model.api_base ...` lands on the canonicaldiff --git a/tests/hermes_cli/test_set_config_value.py b/tests/hermes_cli/test_set_config_value.pyindex 7b25a1ea0b0..ec588453b51 100644--- a/tests/hermes_cli/test_set_config_value.py+++ b/tests/hermes_cli/test_set_config_value.py@@ -564,3 +564,82 @@ class TestDisplaySkinTouch:set_config_value("display.skin", "neon")assert (skins / "neon.yaml").read_text() == body+++# ---------------------------------------------------------------------------+# Mapping guard — regression tests for #74995+# ---------------------------------------------------------------------------++class TestMappingGuard:+ """``hermes config set <section> <scalar>`` must not silently destroy an+ existing mapping. Bare ``model`` is a documented shorthand — redirect to+ ``model.default``. All other mapping sections are refused without --force.+ """++ def _write_config(self, tmp_path, data: dict):+ import yaml as _yaml+ (tmp_path / "config.yaml").write_text(_yaml.dump(data))++ def test_bare_model_shorthand_preserves_siblings(self, _isolated_hermes_home):+ """hermes config set model <id> → model.default, siblings survive."""+ self._write_config(_isolated_hermes_home, {+ "model": {+ "default": "gpt-4o",+ "provider": "openai-api",+ "context_length": 128_000,+ "base_url": "https://api.example.com/v1",+ }+ })+ set_config_value("model", "claude-sonnet-4-20250514")+ config_text = _read_config(_isolated_hermes_home)+ import yaml as _yaml+ parsed = _yaml.safe_load(config_text)+ assert parsed["model"]["default"] == "claude-sonnet-4-20250514"+ assert parsed["model"]["provider"] == "openai-api"+ assert parsed["model"]["context_length"] == 128_000+ assert parsed["model"]["base_url"] == "https://api.example.com/v1"++ def test_bare_model_shorthand_creates_default_when_none(self, _isolated_hermes_home):+ """Bare model shorthand still works when config is empty (legacy behaviour)."""+ set_config_value("model", "gpt-5.6-sol")+ assert "gpt-5.6-sol" in _read_config(_isolated_hermes_home)++ def test_non_model_mapping_is_refused(self, _isolated_hermes_home):+ """hermes config set terminal bash → refuse, terminal has sub-keys."""+ self._write_config(_isolated_hermes_home, {+ "terminal": {+ "backend": "docker",+ "docker_image": "python:3.12",+ "shell": "bash",+ }+ })+ with pytest.raises(SystemExit) as exc:+ set_config_value("terminal", "zsh")+ assert exc.value.code == 1++ def test_non_model_mapping_force_overwrites(self, _isolated_hermes_home):+ """hermes config set --force terminal bash → proceed, section wiped."""+ self._write_config(_isolated_hermes_home, {+ "terminal": {+ "backend": "docker",+ "shell": "bash",+ }+ })+ set_config_value("terminal", "zsh", force=True)+ import yaml as _yaml+ parsed = _yaml.safe_load(_read_config(_isolated_hermes_home))+ assert parsed["terminal"] == "zsh"++ def test_model_default_dotted_path_is_not_guarded(self, _isolated_hermes_home):+ """model.default is already a dotted path — guard must not fire."""+ self._write_config(_isolated_hermes_home, {+ "model": {+ "default": "gpt-4o",+ "provider": "openai-api",+ }+ })+ set_config_value("model.default", "claude-opus-4")+ import yaml as _yaml+ parsed = _yaml.safe_load(_read_config(_isolated_hermes_home))+ assert parsed["model"]["default"] == "claude-opus-4"+ assert parsed["model"]["provider"] == "openai-api"