Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(vector): HNSW persistence is durable — fsync file + dir around rename

xerj-org5d6191682567
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 160363ed3..ba134aeb7 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -4823,15 +4823,21 @@ impl Index {
"seq_no": self.store.current_seq_no(),
"stale": self.hnsw_stale.load(Ordering::Acquire),
});
- let tmp = self.hnsw_ids_path().with_extension("tmp");
- if let Err(e) = std::fs::write(&tmp, serde_json::to_vec(&snapshot).unwrap_or_default()) {
+ // Durable atomic publish (RC4 W2 item 19 — same treatment as
+ // graph.bin in HnswIndex::save_to): fsync file + rename + fsync
+ // parent dir, so power loss can't leave a torn/evaporating
+ // ids.json. Write order is graph.bin THEN ids.json: a crash
+ // between the two renames leaves new-graph/old-ids, whose seq_no
+ // stamp then mismatches the replayed WAL position → the loader
+ // marks the pair stale and the rebuild path re-derives it (never
+ // serves a mixed pair).
+ if let Err(e) = xerj_common::fsio::write_file_durable(
+ &self.hnsw_ids_path(),
+ &serde_json::to_vec(&snapshot).unwrap_or_default(),
+ ) {
warn!(error = %e, "HNSW save: ids.json write failed");
return Ok(());
}
- if let Err(e) = std::fs::rename(&tmp, self.hnsw_ids_path()) {
- warn!(error = %e, "HNSW save: ids.json rename failed");
- return Ok(());
- }
debug!(
graph = %self.hnsw_graph_path().display(),
nodes = hnsw.len(),
diff --git a/engine/crates/xerj-vector/src/hnsw.rs b/engine/crates/xerj-vector/src/hnsw.rs
index 059f67db7..7bd9010f8 100644
--- a/engine/crates/xerj-vector/src/hnsw.rs
+++ b/engine/crates/xerj-vector/src/hnsw.rs
@@ -711,14 +711,14 @@ impl HnswIndex {
// becomes O(file size / disk bw) and the graph is byte-identical
// to what was running pre-restart.
- /// Atomically write the graph to `path`. Writes to `<path>.tmp`
- /// then `rename`s, so a crash during save leaves either the old
- /// file or no file (caller falls back to WAL replay).
+ /// Atomically **and durably** write the graph to `path` (temp file +
+ /// fsync + rename + parent-dir fsync via `xerj_common::fsio`), so a
+ /// crash or power loss during save leaves either the old file or the
+ /// complete new file (caller falls back to WAL replay when absent).
pub fn save_to(&self, path: &Path) -> Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(io_to_xerj)?;
}
- let tmp_path = path.with_extension("tmp");
let mut buf: Vec<u8> = Vec::with_capacity(1024 * 1024);
buf.write_all(HNSW_MAGIC).map_err(io_to_xerj)?;
@@ -781,9 +781,16 @@ impl HnswIndex {
let crc = crc32fast::hash(&buf);
buf.write_u32::<LittleEndian>(crc).map_err(io_to_xerj)?;
- // Atomic publish.
- std::fs::write(&tmp_path, &buf).map_err(io_to_xerj)?;
- std::fs::rename(&tmp_path, path).map_err(io_to_xerj)?;
+ // Atomic + durable publish (RC4 W2 item 19). The previous
+ // fs::write + rename left both the file bytes and the directory
+ // entry in the volatile page cache: a power loss shortly after a
+ // flush could leave a truncated/empty graph.bin (CRC-rejected at
+ // the next open) or no file at all — and because the id-map/graph
+ // pair then fails to load or fails its freshness stamp, every kNN
+ // after recovery silently degraded to permanent-stale/brute.
+ // write_file_durable fsyncs the temp file, renames, then fsyncs
+ // the parent directory so the rename itself survives power loss.
+ xerj_common::fsio::write_file_durable(path, &buf).map_err(io_to_xerj)?;
debug!(
path = %path.display(),
bytes = buf.len(),