Skip to content

fix: rebuild on corrupt persisted HNSW index - #311

Merged
hurttlocker merged 1 commit into
mainfrom
fix/hnsw-corrupt-load
Mar 8, 2026
Merged

hurttlocker merged 1 commit into
mainfrom
fix/hnsw-corrupt-load

Conversation

@hurttlocker

Copy link
Copy Markdown
Owner

What this fixes

Stops cortex search from 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 range

That bubbled out of LoadOrBuildHNSW() before the engine had a chance to fall back to a rebuild.

What changed

  1. Defensive HNSW load validation

    • validate header values before allocation
    • validate node levels / friend counts / friend refs while loading
    • recover from malformed persisted data and return a normal error instead of panicking
  2. Graceful rebuild path

    • LoadOrBuildHNSW() now logs a warning and rebuilds the index when persisted load fails
  3. Regression coverage

    • added ANN load test for corrupt node level
    • added search-engine test that verifies a corrupt persisted HNSW file gets rebuilt successfully

Live verification

I reproduced the panic against the real local DB and confirmed the immediate remediation path:

  • preserved the bad ~/.cortex/hnsw.idx out of the load path
  • reran the installed cortex search
  • confirmed search now returns results again and regenerates a clean HNSW file

Tests

  • go test ./internal/ann ./internal/search ./cmd/cortex -count=1
  • go test ./...
  • live verification with cortex search ... --mode hybrid --embed ollama/nomic-embed-text

Risk

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.

@hurttlocker
hurttlocker merged commit 76e288a into main Mar 8, 2026
4 checks passed
@hurttlocker
hurttlocker deleted the fix/hnsw-corrupt-load branch March 8, 2026 22:49
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.

1 participant