Skip to content

fix(core): verify index/.keys sidecar consistency on load - #1362

Merged
ajianaz merged 1 commit into
developfrom
fix/index-keys-consistency
Oct 7, 2026
Merged

ajianaz merged 1 commit into
developfrom
fix/index-keys-consistency

Conversation

@ajianaz

@ajianaz ajianaz commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

What

  • VectorIndex::save writes a header as the first .keys line: #uteke-keys\t<sha256 of the index bytes>\t<engine rows>. The # prefix means older binaries skip it (it does not parse as <key>\t<id>), so downgrades keep working.
  • VectorIndex::load runs verify_pair_consistency and returns an error when:
    • the header digest does not match the index bytes just read (crash between the two renames);
    • the index has entries but the sidecar is missing or empty;
    • (also for header-less sidecars written by older binaries) vecq has keys at or past the row count, or usearch has more keys than entries.
  • An error here takes the existing "corrupt index" path in finish_open: both files are removed and the index is rebuilt from SQLite.

Why

#1325: index file and sidecar are published by two renames, and load only rebuilt when the whole key map was empty. After a crash between the renames the sidecar is stale but non-empty, so live vecq rows look dead and a usearch next_key can restart too low and collide. That is silent data loss from search, with SQLite intact.

Testing

  • New end-to-end test interrupted_save_pair_is_rebuilt_not_lost (both engines): saves, "crashes" by restoring the previous sidecar, reopens, and asserts the newest memory is still returned by the vector index. It fails without the fix (the memory is missing from the results) and passes with it.
  • New unit tests in memory/vector.rs: stale sidecar rejected, matching pair survives load+resave, header-less sidecar still loads, missing/empty sidecar rejected, out-of-range keys rejected.
  • cargo test -p uteke-core --lib: 621 passed. cargo clippy -p uteke-core --all-targets -- -D warnings, cargo fmt --all -- --check clean; cargo check and the vector tests also pass for --features onnx,vecq and onnx,usearch; cargo run -p docgen no diff.

Cost: one SHA-256 over the index bytes on save and on load (already in memory in both paths).

Closes #1325

🤖 Generated with Claude Code

The vector index file and its .keys sidecar are published by two separate
renames, so a crash between them left a mismatched pair that load only
caught when the whole key map was empty. A stale map silently hid live rows
(vecq) or let next_key restart too low (usearch).

save() now writes a header line with the SHA-256 of the index bytes and the row
count; load() rejects a sidecar that names a different index, a missing/empty
sidecar for a non-empty index, and (for header-less legacy sidecars) key counts
or ranges the index cannot hold. The existing corrupt-index path then discards
both files and rebuilds from SQLite. Older binaries ignore the header line.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit bab1767 into develop Oct 7, 2026
15 checks passed
@ajianaz
ajianaz deleted the fix/index-keys-consistency branch October 7, 2026 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: verify index/.keys sidecar consistency on load

1 participant