refactor(cli): write the saved selections through the shared file writer

- save_last_run uses files.replace_file, with a temp file of its own, in place of a fixed .tmp name
This commit is contained in:
Yijia-Xiao
2026-09-29 05:57:18 +00:00
parent e1a19fd78e
commit b21f8ce1c9
+3 -3
View File
@@ -22,6 +22,7 @@ from pathlib import Path
from cli.models import AnalystType, AssetType
from cli.prompts import _llm_provider_table, filter_analysts_for_asset_type
from tradingagents.dataflows.files import replace_file
from tradingagents.llm_clients.model_catalog import get_model_options
_PREFS_PATH = Path(os.path.expanduser("~")) / ".tradingagents" / "cli_prefs.json"
@@ -52,9 +53,8 @@ def save_last_run(selections: dict) -> None:
kept = {k: v for k, v in kept.items() if v is not None}
try:
_PREFS_PATH.parent.mkdir(parents=True, exist_ok=True)
temp = _PREFS_PATH.with_suffix(".tmp")
temp.write_text(json.dumps(kept, indent=2), encoding="utf-8")
os.replace(temp, _PREFS_PATH) # a concurrent run reads one file or the other
# A concurrent run reads one file or the other.
replace_file(_PREFS_PATH, lambda temp: Path(temp).write_text(json.dumps(kept, indent=2), encoding="utf-8"))
except OSError:
return