fix(cli): use atomic write in save_config_value to prevent config loss on interrupt

save_config_value() used bare open(path, 'w') + yaml.dump() which truncates
the file to zero bytes on open. If the process is interrupted mid-write,
config.yaml is left empty. Replace with atomic_yaml_write() (temp file +
fsync + os.replace), matching the gateway config write path.

Co-authored-by: Hermes Agent <hermes@nousresearch.com>
This commit is contained in:
binhnt92
2026-03-31 12:19:10 -07:00
committed by Teknium
parent 7f78deebe7
commit c94a5fa1b2
2 changed files with 84 additions and 3 deletions

7
cli.py
View File

@@ -991,9 +991,10 @@ def save_config_value(key_path: str, value: any) -> bool:
current = current[key]
current[keys[-1]] = value
# Save back
with open(config_path, 'w') as f:
yaml.dump(config, f, default_flow_style=False, sort_keys=False)
# Save back atomically — write to temp file + fsync + os.replace
# so an interrupt never leaves config.yaml truncated or empty.
from utils import atomic_yaml_write
atomic_yaml_write(config_path, config)
# Enforce owner-only permissions on config files (contain API keys)
try: