From 2dd286c1624c77ce2bbbb844639e3f410fac81ea Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sat, 28 Mar 2026 14:20:30 -0700 Subject: [PATCH] fix: write models.dev disk cache atomically (#3588) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use atomic_json_write() from utils.py instead of plain open()/json.dump() for the models.dev disk cache. Prevents corrupted cache if the process is killed mid-write — _load_disk_cache() silently returns {} on corrupt JSON, losing all model metadata until the next successful API fetch. Co-authored-by: memosr --- agent/models_dev.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/agent/models_dev.py b/agent/models_dev.py index 0ef2b62cd..283e8018f 100644 --- a/agent/models_dev.py +++ b/agent/models_dev.py @@ -15,6 +15,8 @@ import time from pathlib import Path from typing import Any, Dict, Optional +from utils import atomic_json_write + import requests logger = logging.getLogger(__name__) @@ -64,12 +66,10 @@ def _load_disk_cache() -> Dict[str, Any]: def _save_disk_cache(data: Dict[str, Any]) -> None: - """Save models.dev data to disk cache.""" + """Save models.dev data to disk cache atomically.""" try: cache_path = _get_cache_path() - cache_path.parent.mkdir(parents=True, exist_ok=True) - with open(cache_path, "w", encoding="utf-8") as f: - json.dump(data, f, separators=(",", ":")) + atomic_json_write(cache_path, data, indent=None, separators=(",", ":")) except Exception as e: logger.debug("Failed to save models.dev disk cache: %s", e)