fix(presets): persist presets atomically to avoid corruption on crash (#2169)

PresetManager.save() used a plain open("w") + json.dump, which truncates
presets.json before writing the new content. A crash, power loss, or
serialization error mid-write leaves the file truncated/empty and every
saved preset is lost.

Route the write through core.atomic_io.atomic_write_json (tmp file +
os.replace), matching how the rest of the codebase persists JSON state.
The helper is imported lazily so this module stays free of the heavy core
package import graph at module load time.

Adds tests/test_preset_atomic_save.py covering the source contract, a
failed-write leaving the existing file intact, and a round trip.
This commit is contained in:
Mazen Tamer Salah
2026-06-08 20:16:37 +03:00
committed by GitHub
parent 1209f258d7
commit d58202d10e
2 changed files with 49 additions and 3 deletions
+6 -3
View File
@@ -115,9 +115,12 @@ Use precise language. Show causal relationships explicitly. Quantify uncertainty
def save(self, presets: Dict[str, Any]) -> bool:
"""Save presets to file"""
try:
os.makedirs(os.path.dirname(self.presets_file), exist_ok=True)
with open(self.presets_file, 'w', encoding="utf-8") as f:
json.dump(presets, f, indent=2)
# Atomic write (tmp file + os.replace) so a crash or serialization
# error mid-write can't truncate presets.json and lose every saved
# preset. Lazy import keeps this module free of the heavy core
# package import graph at load time.
from core.atomic_io import atomic_write_json
atomic_write_json(self.presets_file, presets, indent=2)
self.presets = presets
return True
except Exception as e: