fix: Faiss read/write failure on Windows with non-ASCII user paths (reopened)
Summary
Fix Faiss index read/write failure on Windows when the path contains non-ASCII characters (Chinese, Japanese, Korean, Cyrillic, etc.).
Root Cause
Faiss C++ fopen() uses ANSI codepage on Windows, but Python passes paths as UTF-8 bytes. Non-ASCII characters get garbled → RuntimeError: could not open.
Fix
Bridge Faiss I/O through pure ASCII temp files using Python's shutil (which uses Windows wide-char CreateFileW API):
_safe_temp_dir()— guaranteed pure-ASCII writable dir (C:\WINDOWS\Tempon Windows)_make_temp_file()— unique temp files with UUID for thread/coroutine safety_read_index()— direct read first, temp file copy fallback_write_index()— temp file write thenshutil.move(atomic on same drive)
Additionally fixes:
insert()/insert_batch(): explicitdtype=np.int64for IDssearch(): usevector.reshape(1, -1)for consistencysearch(): removed incorrectly appliedfaiss.normalize_L2onIndexFlatL2save_index(): guard againstNoneindex andNonepath
Why Reopened
Original PR (#8185) was accidentally closed when the fork repository was temporarily set to private. This resubmits the same fix against the correct upstream AstrBotDevs/AstrBot.
Summary by Sourcery
Fix Faiss index persistence on Windows when file paths contain non-ASCII characters by routing read/write operations through ASCII-only temporary files and updating related index operations for robustness.
Bug Fixes:
- Resolve Faiss index read/write failures on Windows user directories with non-ASCII characters by adding an ASCII-only temporary file bridge.
- Ensure insert and insert_batch use int64 ID arrays to avoid type-related issues with Faiss.
- Fix search behavior by reshaping query vectors consistently and removing inappropriate L2 normalization for IndexFlatL2.
- Prevent errors when saving by skipping save_index if the index or target path is not set.
- Make delete operations more resilient by safely handling Faiss RuntimeError when removing IDs.
Enhancements:
- Introduce helper utilities to locate a safe ASCII-only temp directory and create unique temp files for Faiss index I/O.
- Simplify and tighten EmbeddingStorage method docstrings and validation for vector dimensionality.
Thanks for the update, @irmia2026. The implementation of the ASCII-only temporary file bridge for Faiss I/O on Windows is a robust solution to the reported encoding issues. The added unit tests in test_embedding_storage_fixes.py provide good coverage for these changes. I have no further comments on this implementation.
You're welcome, @irmia2026.
Thanks @sourcery-ai and @gemini-code-assist! All suggestions addressed in 54607b7:
Great to hear that the changes are working as expected, @irmia2026. The ASCII-only temporary file bridge is a solid approach for handling Windows path encoding issues with Faiss. Since all concerns have been addressed, this looks good to go.
please fix code format checking.
@Soulter 已修复 ruff format 问题(style: ruff format),CI 等待审批后应全绿。需要重新 approve,谢谢!
Hi, please note that this PR introduces a regression for the issue in #8695.