"""Keep temp, HF cache, and scratch I/O on the configured DATA_DIR volume. Windows returns ``[Errno 22] Invalid argument`` when the system temp drive (often C:) is nearly full while FAISS / upload / embedding caches still write there. """ from __future__ import annotations import os from pathlib import Path from backend.config import settings def ensure_data_drive_runtime_dirs() -> Path: """Point TMP/TEMP and HF caches at ``DATA_DIR`` (safe on D: etc.).""" root = settings.data_dir_path tmp = root / "tmp" tmp.mkdir(parents=True, exist_ok=True) os.environ["TMP"] = str(tmp) os.environ["TEMP"] = str(tmp) hf = root / "hf_cache" hf.mkdir(parents=True, exist_ok=True) os.environ.setdefault("HF_HOME", str(hf)) os.environ.setdefault("SENTENCE_TRANSFORMERS_HOME", str(hf / "sentence_transformers")) os.environ.setdefault("TRANSFORMERS_CACHE", str(hf / "transformers")) os.environ.setdefault("TORCH_HOME", str(hf / "torch")) return tmp