The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): refuse a list under bare model even with no model section
kshitijk4poor824c2ad4466e
diff --git a/hermes_cli/config.py b/hermes_cli/config.pyindex 2a0391cfda3..7e83b8cba69 100644--- a/hermes_cli/config.py+++ b/hermes_cli/config.py@@ -3531,24 +3531,26 @@ def _guard_section_overwrite(key: str, value: Any, user_config: Dict[str, Any],Bare ``model`` is a documented shorthand — redirected to ``model.default`` so siblings survive.Returns the (possibly redirected) key."""existing = user_config.get(key)+ # The bare-name shorthand takes a model id. A mapping/list literal under it is a section+ # write attempt: redirecting it into model.default would store a container in the string+ # slot every model reader ignores while config get echoed it back (#131435) — the same+ # silent no-op shape _refuse_container_type_mismatch guards for container slots. A list+ # has no reader at all, so it is refused even with no mapping section to protect.+ if key == "model" and not force and (+ isinstance(value, list) or (isinstance(value, dict) and isinstance(existing, dict))):+ _exit_invalid(+ f"✗ Cannot set 'model' to a {'mapping' if isinstance(value, dict) else 'list'} — "+ "the bare 'model' shorthand takes a model id, and a container value has no "+ "slot there.\n"+ " Set the section's keys individually instead:\n"+ " hermes config set model.provider <provider>\n"+ " hermes config set model.default <model-id>\n"+ " Or replace the whole section deliberately:\n"+ " hermes config set --force model '{provider: <provider>, default: <model-id>}'")if "." in key or not isinstance(existing, dict):return keyif key == "model":- # The bare-name shorthand takes a model id. A mapping/list literal under it is a section- # write attempt: redirecting it into model.default would store a container in the string- # slot every model reader ignores while config get echoed it back (#131435) — the same- # silent no-op shape _refuse_container_type_mismatch guards for container slots.if isinstance(value, (dict, list)):- if not force:- _exit_invalid(- f"✗ Cannot set 'model' to a {'mapping' if isinstance(value, dict) else 'list'} — "- "the bare 'model' shorthand takes a model id, and a container value has no "- "slot there.\n"- " Set the section's keys individually instead:\n"- " hermes config set model.provider <provider>\n"- " hermes config set model.default <model-id>\n"- " Or replace the whole section deliberately:\n"- " hermes config set --force model '{provider: <provider>, default: <model-id>}'")print(f"⚠ Replacing entire 'model' section with the given "f"{'mapping' if isinstance(value, dict) else 'list'} "diff --git a/tests/hermes_cli/test_set_config_value.py b/tests/hermes_cli/test_set_config_value.pyindex 22801e0d11b..79cdde57f55 100644--- a/tests/hermes_cli/test_set_config_value.py+++ b/tests/hermes_cli/test_set_config_value.py@@ -680,26 +680,25 @@ class TestMappingGuard:set_config_value("model", "gpt-5.6-sol")assert "gpt-5.6-sol" in _read_config(_isolated_hermes_home)- def test_bare_model_mapping_literal_is_refused(self, _isolated_hermes_home):- """A JSON/YAML mapping under bare ``model`` must be refused, not redirected (#131435).+ @pytest.mark.parametrize("start, literal", [+ ({"model": {"default": "glm-5.3-flash", "provider": "zai"}},+ '{"provider": "openai-codex", "default": "gpt-6.1-sol"}'),+ ({}, "[a, b]"),+ ({"model": "gpt-4o"}, "[a, b]"),+ ])+ def test_bare_model_mapping_literal_is_refused(self, _isolated_hermes_home, start, literal):+ """A JSON/YAML mapping/list under bare ``model`` must be refused, not redirected (#131435).``model`` is seeded as a string default, so the generic coerce kept the literalverbatim and the shorthand wrote the raw JSON text into ``model.default`` as themodel id — a route every runtime reader then fails to resolve, while``config get`` echoes it back. Fail closed instead, like #114471 did for- container slots."""- self._write_config(_isolated_hermes_home, {- "model": {- "default": "glm-5.3-flash",- "provider": "zai",- }- })+ container slots. A list has no reader even when no model section exists yet."""+ self._write_config(_isolated_hermes_home, start)with pytest.raises(SystemExit) as exc:- set_config_value("model", '{"provider": "openai-codex", "default": "gpt-6.1-sol"}')+ set_config_value("model", literal)assert exc.value.code == 1- parsed = yaml.safe_load(_read_config(_isolated_hermes_home))- assert parsed["model"]["provider"] == "zai"- assert parsed["model"]["default"] == "glm-5.3-flash"+ assert yaml.safe_load(_read_config(_isolated_hermes_home)) == startdef test_bare_model_mapping_force_replaces_section(self, _isolated_hermes_home):"""--force keeps its documented meaning for mappings too: replace the whole