Repository navigation
fix: rebuild on corrupt persisted HNSW index - #311
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this fixes
Stops
cortex searchfrom panicking when the persisted HNSW index file is malformed or stale-corrupt.Root cause
ann.Load()trusted persisted integer counts/levels and could panic with:panic: runtime error: makeslice: len out of rangeThat bubbled out of
LoadOrBuildHNSW()before the engine had a chance to fall back to a rebuild.What changed
Defensive HNSW load validation
Graceful rebuild path
LoadOrBuildHNSW()now logs a warning and rebuilds the index when persisted load failsRegression coverage
Live verification
I reproduced the panic against the real local DB and confirmed the immediate remediation path:
~/.cortex/hnsw.idxout of the load pathcortex searchTests
go test ./internal/ann ./internal/search ./cmd/cortex -count=1go test ./...cortex search ... --mode hybrid --embed ollama/nomic-embed-textRisk
Low.
This is a persistence hardening fix plus rebuild fallback. It should only change behavior when the on-disk HNSW index is malformed; healthy indexes still load normally.