fix(config): refuse to write when config.yaml has YAML syntax errors

set_config_value() and unset_config_value() silently replaced the
entire config with an empty dict when config.yaml could not be
parsed. A single YAML syntax error would cause 'hermes config set'
to wipe all settings and write only the new key. Now exits with
error and preserves the existing file.
This commit is contained in:
Baophan00 2026-07-31 20:27:46 +07:00 committed by Teknium
parent 41e55679ee
commit df09a90cd6
1 changed files with 18 additions and 4 deletions

View File

@ -4837,8 +4837,15 @@ def set_config_value(key: str, value: str, force: bool = False):
try:
with open(config_path, encoding="utf-8") as f:
user_config = fast_safe_load(f) or {}
except Exception:
user_config = {}
except Exception as exc:
print(
f"✗ Cannot parse {config_path}: {exc}\n"
f" The file contains a YAML syntax error. Fix the error\n"
f" in your config file first, then retry.\n"
f" (hermes config edit will open it in your editor.)",
file=sys.stderr,
)
sys.exit(1)
# Handle nested keys (e.g., "tts.provider") including numeric list
# indices (e.g., "custom_providers.0.api_key"). Delegates to
@ -5045,8 +5052,15 @@ def unset_config_value(key: str):
try:
with open(config_path, encoding="utf-8") as f:
user_config = fast_safe_load(f) or {}
except Exception:
user_config = {}
except Exception as exc:
print(
f"✗ Cannot parse {config_path}: {exc}\n"
f" The file contains a YAML syntax error. Fix the error\n"
f" in your config file first, then retry.\n"
f" (hermes config edit will open it in your editor.)",
file=sys.stderr,
)
sys.exit(1)
removed = _unset_nested(user_config, key)