root b8c072cf0b G1: vectord — HNSW vector search via coder/hnsw · 6 scrum fixes applied
First G1+ piece. Standalone vectord service with in-memory HNSW
indexes keyed by string IDs and optional opaque JSON metadata.
Wraps github.com/coder/hnsw v0.6.1 (pure Go, no cgo). New port
:3215 with /v1/vectors/* routed through gateway.

API:
  POST   /v1/vectors/index            create
  GET    /v1/vectors/index            list
  GET    /v1/vectors/index/{name}     get info
  DELETE /v1/vectors/index/{name}
  POST   /v1/vectors/index/{name}/add (batch)
  POST   /v1/vectors/index/{name}/search

Acceptance smoke 7/7 PASS — including recall=1 on inserted vector
w-042 (cosine distance 5.96e-8, float32 precision noise), 200-
vector batch round-trip, dim mismatch → 400, missing index → 404,
duplicate create → 409.

Two upstream library quirks worked around in the wrapper:
  1. coder/hnsw.Add panics with "node not added" on re-adding an
     existing key (length-invariant fires because internal
     delete+re-add doesn't change Len). Pre-Delete fixes for n>1.
  2. Delete of the LAST node leaves layers[0] non-empty but
     entryless; next Add SIGSEGVs in Dims(). Workaround: when
     re-adding to a 1-node graph, recreate the underlying graph
     fresh via resetGraphLocked().

Cross-lineage scrum on shipped code:
  - Opus 4.7 (opencode):                 0 BLOCK + 4 WARN + 3 INFO
  - Kimi K2-0905 (openrouter):           2 BLOCK + 2 WARN + 1 INFO
  - Qwen3-coder (openrouter):            "No BLOCKs" (4 tokens)

Fixed (4 real + 2 cleanup):
  O-W1: Lookup returned the raw []float32 from coder/hnsw — caller
    mutation would corrupt index. Now copies before return.
  O-W3: NaN/Inf vectors poison HNSW (distance comparisons return
    false for both < and >, breaking heap invariants). Zero-norm
    under cosine produces NaN. Now validated at Add time.
  K-B1: Re-adding with nil metadata silently cleared the existing
    entry — JSON-omitted "metadata" field deserializes as nil,
    making upsert non-idempotent. Now nil = "leave alone"; explicit
    {} or Delete to clear.
  O-W4: Batch Add with mid-batch failure left items 0..N-1
    committed and item N rejected. Now pre-validates all IDs+dims
    before any Add.
  O-I1: jsonItoa hand-roll replaced with strconv.Itoa — no
    measured allocation win.
  O-I2: distanceFn re-resolved per Search → use stored i.g.Distance.

Dismissed (2 false positives):
  K-B2 "MaxBytesReader applied after full read" — false, applied
    BEFORE Decode in decodeJSON
  K-W1 "Search distances under read lock might see invalidated
    slices from concurrent Add" — false, RWMutex serializes
    write-lock during Add against read-lock during Search

Deferred (3): HTTP server timeouts (consistent G0 punt),
Content-Type validation (internal service behind gateway), Lookup
dim assertion (in-memory state can't drift).

The K-B1 finding is worth pausing on: nil metadata on re-add is
the kind of API ergonomics bug only a code-reading reviewer
catches — smoke would never detect it because the smoke always
sends explicit metadata. Three lines changed in Add; the resulting
API matches what callers actually expect.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 00:50:28 -05:00

233 lines
7.0 KiB
Go

package vectord
import (
"encoding/json"
"errors"
"fmt"
"math"
"sync"
"testing"
)
func TestNewIndex_DefaultsAndValidation(t *testing.T) {
idx, err := NewIndex(IndexParams{Name: "x", Dimension: 4})
if err != nil {
t.Fatal(err)
}
p := idx.Params()
if p.M != DefaultM || p.EfSearch != DefaultEfSearch || p.Distance != DistanceCosine {
t.Errorf("defaults not applied: %+v", p)
}
if _, err := NewIndex(IndexParams{Dimension: 4}); err == nil {
t.Error("expected error on empty name")
}
if _, err := NewIndex(IndexParams{Name: "y", Dimension: 0}); err == nil {
t.Error("expected error on zero dimension")
}
if _, err := NewIndex(IndexParams{Name: "z", Dimension: 4, Distance: "bogus"}); !errors.Is(err, ErrUnknownDistance) {
t.Errorf("expected ErrUnknownDistance, got %v", err)
}
}
func TestIndex_AddAndSearch_Recall(t *testing.T) {
idx, err := NewIndex(IndexParams{Name: "x", Dimension: 3, Distance: DistanceCosine})
if err != nil {
t.Fatal(err)
}
target := []float32{1, 0, 0}
if err := idx.Add("alice", target, json.RawMessage(`{"role":"warehouse"}`)); err != nil {
t.Fatal(err)
}
if err := idx.Add("bob", []float32{0, 1, 0}, nil); err != nil {
t.Fatal(err)
}
if err := idx.Add("carol", []float32{0, 0, 1}, nil); err != nil {
t.Fatal(err)
}
if idx.Len() != 3 {
t.Errorf("Len: got %d, want 3", idx.Len())
}
hits, err := idx.Search(target, 2)
if err != nil {
t.Fatal(err)
}
if len(hits) < 1 {
t.Fatal("no hits")
}
if hits[0].ID != "alice" {
t.Errorf("nearest: got %q, want alice", hits[0].ID)
}
if hits[0].Distance > 0.001 {
t.Errorf("nearest distance: got %v, want ~0", hits[0].Distance)
}
// Cosine distance of identical unit vectors is 0; metadata round-trips.
if string(hits[0].Metadata) != `{"role":"warehouse"}` {
t.Errorf("metadata: got %q", hits[0].Metadata)
}
}
func TestIndex_DimensionMismatch(t *testing.T) {
idx, _ := NewIndex(IndexParams{Name: "x", Dimension: 4})
err := idx.Add("a", []float32{1, 2, 3}, nil)
if !errors.Is(err, ErrDimensionMismatch) {
t.Errorf("Add: expected ErrDimensionMismatch, got %v", err)
}
_, err = idx.Search([]float32{1, 2, 3}, 1)
if !errors.Is(err, ErrDimensionMismatch) {
t.Errorf("Search: expected ErrDimensionMismatch, got %v", err)
}
}
func TestIndex_DeleteAndLookup(t *testing.T) {
idx, _ := NewIndex(IndexParams{Name: "x", Dimension: 2})
_ = idx.Add("a", []float32{1, 0}, nil)
if !idx.Delete("a") {
t.Error("Delete returned false on existing key")
}
if _, _, ok := idx.Lookup("a"); ok {
t.Error("Lookup found deleted key")
}
if idx.Delete("a") {
t.Error("Delete should return false on missing key")
}
}
// TestIndex_ConcurrentSearchAdd exercises the RWMutex — many
// concurrent searches alongside a writer adding distinct keys
// shouldn't deadlock, panic, or interleave incorrectly. Each
// writer goroutine gets its own key namespace so we don't
// stress-test the library's re-add path (which has known issues
// under high churn — the wrapper exposes idempotent semantics
// via single-threaded Delete+Add but isn't a fix-everything for
// upstream).
func TestIndex_ConcurrentSearchAdd(t *testing.T) {
idx, _ := NewIndex(IndexParams{Name: "x", Dimension: 4})
for i := 0; i < 50; i++ {
_ = idx.Add(fmt.Sprintf("seed-%d", i), []float32{float32(i), 0, 0, 0}, nil)
}
var wg sync.WaitGroup
// One writer goroutine, eight readers — realistic ratio for the
// staffing co-pilot use case where ingestion is occasional and
// queries are common.
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 50; j++ {
_ = idx.Add(fmt.Sprintf("hot-%d", j), []float32{float32(j), 1, 0, 0}, nil)
}
}()
for r := 0; r < 8; r++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 50; j++ {
_, _ = idx.Search([]float32{float32(j), 0, 0, 0}, 5)
}
}()
}
wg.Wait()
}
func TestRegistry_CreateGetDelete(t *testing.T) {
r := NewRegistry()
idx, err := r.Create(IndexParams{Name: "workers", Dimension: 4})
if err != nil {
t.Fatal(err)
}
if idx.Params().Name != "workers" {
t.Errorf("name: got %q", idx.Params().Name)
}
got, err := r.Get("workers")
if err != nil || got != idx {
t.Errorf("Get returned different / err: %v", err)
}
if _, err := r.Create(IndexParams{Name: "workers", Dimension: 4}); !errors.Is(err, ErrIndexAlreadyExists) {
t.Errorf("dup create: expected ErrIndexAlreadyExists, got %v", err)
}
if err := r.Delete("workers"); err != nil {
t.Fatal(err)
}
if _, err := r.Get("workers"); !errors.Is(err, ErrIndexNotFound) {
t.Errorf("Get after Delete: expected ErrIndexNotFound, got %v", err)
}
if err := r.Delete("workers"); !errors.Is(err, ErrIndexNotFound) {
t.Errorf("idempotent Delete: expected ErrIndexNotFound, got %v", err)
}
}
func TestAdd_RejectsNonFinite(t *testing.T) {
idx, _ := NewIndex(IndexParams{Name: "x", Dimension: 3, Distance: DistanceEuclidean})
cases := [][]float32{
{float32(math.NaN()), 0, 0},
{float32(math.Inf(1)), 0, 0},
{0, float32(math.Inf(-1)), 0},
}
for _, vec := range cases {
if err := idx.Add("a", vec, nil); err == nil {
t.Errorf("expected error for non-finite vec %v", vec)
}
}
}
func TestAdd_RejectsZeroNormUnderCosine(t *testing.T) {
idx, _ := NewIndex(IndexParams{Name: "x", Dimension: 3, Distance: DistanceCosine})
if err := idx.Add("a", []float32{0, 0, 0}, nil); err == nil {
t.Error("expected error for zero-norm under cosine")
}
// Same vec is OK under euclidean (origin is a valid point).
idxE, _ := NewIndex(IndexParams{Name: "y", Dimension: 3, Distance: DistanceEuclidean})
if err := idxE.Add("a", []float32{0, 0, 0}, nil); err != nil {
t.Errorf("zero vec under euclidean should be valid: %v", err)
}
}
func TestAdd_PreservesMetaOnNilReAdd(t *testing.T) {
// Per scrum K-B1: re-adding with nil meta must NOT clear existing.
idx, _ := NewIndex(IndexParams{Name: "x", Dimension: 2})
_ = idx.Add("alice", []float32{1, 0}, json.RawMessage(`{"role":"warehouse"}`))
if err := idx.Add("alice", []float32{0.9, 0.1}, nil); err != nil {
t.Fatal(err)
}
_, meta, ok := idx.Lookup("alice")
if !ok {
t.Fatal("Lookup not found")
}
if string(meta) != `{"role":"warehouse"}` {
t.Errorf("metadata cleared on nil re-add: got %q", meta)
}
// Explicit empty {} replaces.
_ = idx.Add("alice", []float32{1, 0}, json.RawMessage(`{}`))
_, meta, _ = idx.Lookup("alice")
if string(meta) != `{}` {
t.Errorf("explicit {} should replace: got %q", meta)
}
}
func TestLookup_ReturnsCopy(t *testing.T) {
// Per scrum O-W1: caller mutation must not corrupt index state.
idx, _ := NewIndex(IndexParams{Name: "x", Dimension: 3})
orig := []float32{1, 2, 3}
_ = idx.Add("a", orig, nil)
got, _, _ := idx.Lookup("a")
got[0] = 99 // mutate the returned copy
again, _, _ := idx.Lookup("a")
if again[0] != 1 {
t.Errorf("Lookup didn't copy: index now sees %v", again)
}
}
func TestRegistry_Names_Sorted(t *testing.T) {
r := NewRegistry()
for _, n := range []string{"zoo", "alpha", "midway"} {
_, _ = r.Create(IndexParams{Name: n, Dimension: 4})
}
got := r.Names()
want := []string{"alpha", "midway", "zoo"}
for i, w := range want {
if got[i] != w {
t.Errorf("Names[%d]: got %q, want %q", i, got[i], w)
}
}
}