fix(cli): support literal dots in config set/unset key paths (#84064)
This commit is contained in:
+39
-5
@@ -1056,6 +1056,40 @@ def get_missing_env_vars(required_only: bool = False) -> List[Dict[str, Any]]:
|
||||
return missing
|
||||
|
||||
|
||||
def _split_key_path(key: str) -> list[str]:
|
||||
"""Split a dotted config-key path, honoring backslash-escaped dots.
|
||||
|
||||
``hermes config set`` uses ``.`` as the nesting separator, so a key that
|
||||
itself contains a literal dot (e.g. provider names like
|
||||
``qwen3.5-397b-wafer``) was silently split into bogus nested segments
|
||||
(#84064). A backslash escapes a dot::
|
||||
|
||||
_split_key_path("providers.qwen3\\.5-397b.api_key")
|
||||
-> ["providers", "qwen3.5-397b", "api_key"]
|
||||
|
||||
Backslashes before any other character are preserved verbatim. Keys
|
||||
without escapes behave exactly as ``key.split(".")``.
|
||||
"""
|
||||
parts: list[str] = []
|
||||
current: list[str] = []
|
||||
i = 0
|
||||
while i < len(key):
|
||||
ch = key[i]
|
||||
if ch == "\\" and i + 1 < len(key) and key[i + 1] == ".":
|
||||
current.append(".")
|
||||
i += 2
|
||||
continue
|
||||
if ch == ".":
|
||||
parts.append("".join(current))
|
||||
current = []
|
||||
i += 1
|
||||
continue
|
||||
current.append(ch)
|
||||
i += 1
|
||||
parts.append("".join(current))
|
||||
return parts
|
||||
|
||||
|
||||
def _set_nested(config, dotted_key: str, value):
|
||||
"""Set a value at an arbitrarily nested dotted key path.
|
||||
|
||||
@@ -1078,7 +1112,7 @@ def _set_nested(config, dotted_key: str, value):
|
||||
destroying list-typed config like ``custom_providers`` whenever a
|
||||
caller used an indexed path.
|
||||
"""
|
||||
parts = dotted_key.split(".")
|
||||
parts = _split_key_path(dotted_key)
|
||||
current = config
|
||||
for part in parts[:-1]:
|
||||
if isinstance(current, list):
|
||||
@@ -1140,7 +1174,7 @@ _MISSING = object()
|
||||
def _get_nested(config, dotted_key: str):
|
||||
"""Return a dotted-path value from nested dict/list config data."""
|
||||
current = config
|
||||
for part in dotted_key.split("."):
|
||||
for part in _split_key_path(dotted_key):
|
||||
if isinstance(current, list):
|
||||
try:
|
||||
current = current[int(part)]
|
||||
@@ -1157,7 +1191,7 @@ def _get_nested(config, dotted_key: str):
|
||||
|
||||
def _unset_nested(config, dotted_key: str) -> bool:
|
||||
"""Remove a dotted-path value from nested dict/list config data."""
|
||||
parts = dotted_key.split(".")
|
||||
parts = _split_key_path(dotted_key)
|
||||
if not parts:
|
||||
return False
|
||||
|
||||
@@ -5324,7 +5358,7 @@ def _default_value_for_key(dotted_key: str):
|
||||
best-effort coercion used by ``config set``.
|
||||
"""
|
||||
node = DEFAULT_CONFIG
|
||||
for part in dotted_key.split("."):
|
||||
for part in _split_key_path(dotted_key):
|
||||
if not isinstance(node, dict) or part not in node:
|
||||
return None
|
||||
node = node[part]
|
||||
@@ -5436,7 +5470,7 @@ def _validate_config_key(key: str) -> tuple[bool, Optional[str]]:
|
||||
if not key:
|
||||
return False, None
|
||||
|
||||
segments = key.split(".")
|
||||
segments = _split_key_path(key)
|
||||
top = segments[0]
|
||||
|
||||
# ── Underscore-prefixed keys are internal/test markers ───────────
|
||||
|
||||
@@ -489,8 +489,29 @@ class TestSecretRedactionInDisplay:
|
||||
captured = capsys.readouterr()
|
||||
assert "Set model.reasoning_effort = high" in captured.out
|
||||
|
||||
# #34067: Schema validation for unknown keys
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# `config get` redacts resolved credentials (issue #84106)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_MCP_SECRETS_CONFIG = """\
|
||||
config_version: 34
|
||||
security:
|
||||
redact_secrets: true
|
||||
mcp_servers:
|
||||
demo_url:
|
||||
url: "https://example.invalid/mcp?apikey=${FMP_API_KEY}"
|
||||
demo_env:
|
||||
command: demo
|
||||
env:
|
||||
COINGECKO_PRO_API_KEY: "${COINGECKO_PRO_API_KEY}"
|
||||
demo_header:
|
||||
url: "https://example.invalid/mcp"
|
||||
headers:
|
||||
Authorization: "Bearer ${TEST_BEARER_TOKEN}"
|
||||
"""
|
||||
|
||||
|
||||
|
||||
class TestSchemaValidation:
|
||||
"""#34067: ``hermes config set`` must not report bare success for
|
||||
@@ -776,3 +797,124 @@ class TestMalformedYAMLConfigPreservation:
|
||||
assert "Failed to parse" in combined or "not valid YAML" in combined
|
||||
raw = _read_config(_isolated_hermes_home)
|
||||
assert raw == self.BROKEN_CONFIG
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Literal dots in key paths — regression tests for #84064
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestLiteralDotKeyEscaping:
|
||||
"""``hermes config set/unset/get`` must not split a key segment on a
|
||||
literal dot. Provider names routinely embed version numbers
|
||||
(``qwen3.5-397b-wafer``), and before the backslash-escape (#84064)
|
||||
``providers.qwen3.5-397b-wafer.api_key`` silently created a bogus nested
|
||||
``qwen3`` -> ``5-397b-wafer`` structure while reporting success.
|
||||
"""
|
||||
|
||||
def _write_config(self, tmp_path, data: dict):
|
||||
import yaml as _yaml
|
||||
(tmp_path / "config.yaml").write_text(_yaml.safe_dump(data, sort_keys=False))
|
||||
|
||||
def test_split_key_path_escaped_dot(self):
|
||||
from hermes_cli.config import _split_key_path
|
||||
|
||||
assert _split_key_path("providers.qwen3\\.5-397b.api_key") == [
|
||||
"providers", "qwen3.5-397b", "api_key",
|
||||
]
|
||||
assert _split_key_path("qwen3\\.5") == ["qwen3.5"]
|
||||
assert _split_key_path("a\\.b\\.c") == ["a.b.c"]
|
||||
# Unescaped keys keep plain dot-splitting semantics.
|
||||
assert _split_key_path("terminal.backend") == ["terminal", "backend"]
|
||||
assert _split_key_path("model") == ["model"]
|
||||
# Backslash before a non-dot char is preserved verbatim.
|
||||
assert _split_key_path("win\\path.key") == ["win\\path", "key"]
|
||||
|
||||
def test_set_preserves_literal_dot_in_provider_key(self, _isolated_hermes_home, capsys):
|
||||
self._write_config(_isolated_hermes_home, {
|
||||
"providers": {
|
||||
"qwen3.5-397b-wafer-non-zdr": {"api": "https://pass.wafer.ai/v1"},
|
||||
"openrouter": {"api_key": "or-keep"},
|
||||
}
|
||||
})
|
||||
|
||||
set_config_value(
|
||||
"providers.qwen3\\.5-397b-wafer-non-zdr.extra_headers",
|
||||
'{"Wafer-ZDR": "required"}',
|
||||
)
|
||||
|
||||
import yaml
|
||||
saved = yaml.safe_load(_read_config(_isolated_hermes_home))
|
||||
providers = saved["providers"]
|
||||
# No bogus ``qwen3`` nesting was created; the existing entry was updated.
|
||||
assert "qwen3" not in providers
|
||||
target = providers["qwen3.5-397b-wafer-non-zdr"]
|
||||
assert target["api"] == "https://pass.wafer.ai/v1"
|
||||
assert target["extra_headers"] == '{"Wafer-ZDR": "required"}'
|
||||
# Sibling provider untouched.
|
||||
assert providers["openrouter"] == {"api_key": "or-keep"}
|
||||
# Escaped key is schema-known (providers.* is an open dict) — no warning.
|
||||
assert "not a recognized config key" not in capsys.readouterr().out
|
||||
|
||||
def test_unset_removes_literal_dot_provider_key(self, _isolated_hermes_home, capsys):
|
||||
self._write_config(_isolated_hermes_home, {
|
||||
"providers": {
|
||||
"qwen3.5-397b-wafer-non-zdr": {"api": "https://pass.wafer.ai/v1"},
|
||||
"openrouter": {"api_key": "or-keep"},
|
||||
}
|
||||
})
|
||||
|
||||
args = argparse.Namespace(
|
||||
config_command="unset",
|
||||
key="providers.qwen3\\.5-397b-wafer-non-zdr",
|
||||
)
|
||||
config_command(args)
|
||||
|
||||
import yaml
|
||||
saved = yaml.safe_load(_read_config(_isolated_hermes_home))
|
||||
assert "qwen3.5-397b-wafer-non-zdr" not in saved["providers"]
|
||||
assert saved["providers"]["openrouter"] == {"api_key": "or-keep"}
|
||||
assert "Unset providers.qwen3\\.5-397b-wafer-non-zdr" in capsys.readouterr().out
|
||||
|
||||
def test_unset_nested_field_under_literal_dot_key(self, _isolated_hermes_home, capsys):
|
||||
self._write_config(_isolated_hermes_home, {
|
||||
"providers": {
|
||||
"qwen3.5-397b-wafer-non-zdr": {
|
||||
"api": "https://pass.wafer.ai/v1",
|
||||
"extra_headers": '{"K": "V"}',
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
args = argparse.Namespace(
|
||||
config_command="unset",
|
||||
key="providers.qwen3\\.5-397b-wafer-non-zdr.extra_headers",
|
||||
)
|
||||
config_command(args)
|
||||
|
||||
import yaml
|
||||
saved = yaml.safe_load(_read_config(_isolated_hermes_home))
|
||||
target = saved["providers"]["qwen3.5-397b-wafer-non-zdr"]
|
||||
assert "extra_headers" not in target
|
||||
assert target["api"] == "https://pass.wafer.ai/v1"
|
||||
|
||||
def test_get_reads_literal_dot_provider_key(self, _isolated_hermes_home, capsys):
|
||||
self._write_config(_isolated_hermes_home, {
|
||||
"providers": {"qwen3.5-397b": {"api": "https://pass.wafer.ai/v1"}},
|
||||
})
|
||||
|
||||
args = argparse.Namespace(
|
||||
config_command="get",
|
||||
key="providers.qwen3\\.5-397b.api",
|
||||
json=False,
|
||||
)
|
||||
config_command(args)
|
||||
|
||||
assert capsys.readouterr().out.strip() == "https://pass.wafer.ai/v1"
|
||||
|
||||
def test_unescaped_dotted_path_unchanged(self, _isolated_hermes_home):
|
||||
"""Nesting semantics for plain dotted keys are untouched."""
|
||||
set_config_value("terminal.backend", "docker")
|
||||
|
||||
import yaml
|
||||
saved = yaml.safe_load(_read_config(_isolated_hermes_home))
|
||||
assert saved["terminal"]["backend"] == "docker"
|
||||
|
||||
Reference in New Issue
Block a user