Skip to content

fix(search): harden HNSW load/rebuild against malformed-length persisted state (#313) - #328

Merged
hurttlocker merged 1 commit into
mainfrom
feat/313-ann-hnsw-reliability
Mar 11, 2026
Merged

hurttlocker merged 1 commit into
mainfrom
feat/313-ann-hnsw-reliability

Conversation

@hurttlocker

Copy link
Copy Markdown
Owner

What this does

Implements a bounded reliability fix for #313 so malformed ANN/HNSW persisted state no longer causes repeated unstable load/rebuild loops and potential panic-adjacent behavior.

This keeps the lane strictly reliability-focused (no ranking/search-quality widening).

Problem / Context

Issue: #313

Observed on current main + current DB shape:

  • repeated HNSW load failures on every semantic/hybrid search run:
    • warning: could not load HNSW index ... friend count: invalid value -1152003907; rebuilding
  • this indicates malformed persisted index state was being rebuilt into another malformed persisted index, causing repeated degraded behavior.

Root cause:

  • BuildHNSW assumed all stored embeddings share dimensions of the first vector.
  • mixed-dimension embedding rows were inserted into HNSW without dimension filtering.
  • persisted file could become structurally invalid for ann.Load parse expectations (malformed-length style failure surface).

How it works

1) Harden HNSW build path against mixed/malformed dimensions

File: internal/search/search.go

BuildHNSW now:

  • discovers first usable embedding dimensions
  • skips embeddings with empty or mismatched dimensions
  • continues building from valid vectors only
  • logs a bounded warning summary with skipped counts and indexed dims

This prevents persisting malformed HNSW structure from mixed-dimension rows.

2) Add deterministic regression for malformed-length load case

Files:

  • internal/ann/hnsw_test.go
  • internal/search/search_test.go

Added tests:

  • TestLoadRejectsMalformedFriendCountWithoutPanicking
    • covers malformed-length friend-count load case explicitly
  • TestBuildHNSW_SkipsMismatchedEmbeddingDimensionsAndPersistsValidIndex
    • proves mixed-dimension embedding store no longer produces invalid persisted index

Repro/disprove evidence on current main/current DB shape

Baseline (/tmp/cortex-313-main, 10 semantic/hybrid runs):

  • main_warnings=10 (load failure + rebuild every run)
  • recurring message: friend count: invalid value -1152003907; rebuilding

Fixed branch build (/tmp/cortex-313-fix-bin, same 10 runs):

  • load_warnings=1
  • skip_warnings=1
  • first run rebuilds once, then subsequent runs no repeated malformed-load warnings

This demonstrates graceful recovery to stable persisted state.

Testing done

Exact commands run:

  1. go test ./internal/ann -run 'RejectsCorruptNodeLevel|RejectsMalformedFriendCount' -count=1
  2. go test ./internal/search -run 'LoadOrBuildHNSW_RebuildsCorruptPersistedIndex|BuildHNSW_SkipsMismatchedEmbeddingDimensionsAndPersistsValidIndex' -count=1
  3. go test ./...
  4. Runtime repro loop on current DB with baseline and fixed binaries (10 semantic/hybrid runs each)

Screenshots / before-after

Terminal before/after evidence:

  • Before: malformed HNSW load warning on every run
  • After: one-time rebuild warning, then stable runs

Breaking changes / risks

Breaking changes: None.

Risk note:

  • mixed-dimension embeddings are now skipped from HNSW build (rather than silently inserted), so ANN index cardinality may be lower than total embedding rows when DB contains dim drift.
  • This is intentional for reliability; skipped counts are surfaced in warning output.

Explicit out-of-scope

Merge notes

@hurttlocker

Copy link
Copy Markdown
Owner Author

Runtime evidence snapshot (current DB shape, post-fix validation)

Baseline build (/tmp/cortex-313-main) over 10 semantic/hybrid runs:

  • main_warnings=10
  • recurring:
    • warning: could not load HNSW index ... friend count: invalid value -1152003907; rebuilding

Fixed build (/tmp/cortex-313-fix-bin) over same 10 runs:

  • load_warnings=1
  • skip_warnings=1
  • warning output:
    • one-time load failure/rebuild
    • one-time build summary: indexed 224 vectors with 768 dims and skipped mismatched rows
  • subsequent runs show no repeated malformed-load warnings.

This confirms #313 reliability hardening stabilizes ANN load/rebuild behavior under the current DB's mixed embedding dimensions.

@hurttlocker
hurttlocker merged commit b073b4a into main Mar 11, 2026
4 checks passed
@hurttlocker
hurttlocker deleted the feat/313-ann-hnsw-reliability branch March 21, 2026 01:27
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