AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

fix: Faiss read/write failure on Windows with non-ASCII user paths (reopened)

Open irmia2026 opened this issue 6 months ago • 6 comments

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):

  1. _safe_temp_dir() — guaranteed pure-ASCII writable dir (C:\WINDOWS\Temp on Windows)
  2. _make_temp_file() — unique temp files with UUID for thread/coroutine safety
  3. _read_index() — direct read first, temp file copy fallback
  4. _write_index() — temp file write then shutil.move (atomic on same drive)

Additionally fixes:

  • insert()/insert_batch(): explicit dtype=np.int64 for IDs
  • search(): use vector.reshape(1, -1) for consistency
  • search(): removed incorrectly applied faiss.normalize_L2 on IndexFlatL2
  • save_index(): guard against None index and None path

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.

irmia2026 avatar May 25 '26 06:05 irmia2026

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.

gemini-code-assist avatar May 25 '26 06:05 gemini-code-assist

You're welcome, @irmia2026.

gemini-code-assist avatar May 25 '26 06:05 gemini-code-assist

Thanks @sourcery-ai and @gemini-code-assist! All suggestions addressed in 54607b7:

irmia2026 avatar May 25 '26 07:05 irmia2026

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.

gemini-code-assist avatar May 25 '26 07:05 gemini-code-assist

please fix code format checking.

Soulter avatar May 27 '26 12:05 Soulter

@Soulter 已修复 ruff format 问题(style: ruff format),CI 等待审批后应全绿。需要重新 approve,谢谢!

irmia2026 avatar May 27 '26 13:05 irmia2026

Hi, please note that this PR introduces a regression for the issue in #8695.

Soulter avatar Jul 22 '26 03:07 Soulter