brainy/tests/unit/utils
David Snelling 73e7e39b52 feat: SQ4 (4-bit) scalar quantization + native distance hook (2.5.0 #30)
Brainy now has a complete reference SQ4 (4-bit per dimension, 8× compression
vs float32) quantization layer in src/utils/vectorQuantization.ts: quantizeSQ4,
dequantizeSQ4, distanceSQ4Js, serializeSQ4, deserializeSQ4 — all byte-for-byte
identical to cortex's Rust quantize_sq4 / dequantize_sq4 / cosine_distance_sq4.

The pack format mirrors cortex exactly:
- Two nibbles per byte. High nibble = vector[2i], low nibble = vector[2i+1].
- Odd-dim vectors place the final value in the high nibble and pad the low
  nibble with 0. Packed length = ceil(dim / 2).
- Zero-range vectors (all values identical) map every nibble to 8 (midpoint
  of [0, 15]) — both reference impls produce 0x88 bytes.

The active-fn dispatch pattern matches SQ8: distanceSQ4Js is the pure-JS
reference; distanceSQ4 routes through a swappable activeSQ4Distance slot;
setSQ4DistanceImplementation(fn) swaps in cortex's SIMD Rust distance when the
distance:sq4 provider is registered. brainy.ts wires the provider in init,
same shape as the existing SQ8 wiring (~14 LOC). The dispatch path is the
single point of integration so HNSW search code never needs to know whether
it's running on JS or native.

Serialization adds a uint32 dim field after min/max (12-byte header total) —
SQ8 doesn't need it because the packed length equals dim, but SQ4's packed
length rounds up so dim must be recorded explicitly to disambiguate the
trailing pad nibble.

Tests (1462 total, +15):
- Pack format: high-nibble-first, odd-dim trailing pad = 0, ceil(dim/2) length
- Round-trip error envelope: every reconstructed value within half a quantum
  step of the original (verified across dims 1, 2, 3, 4, 16, 17, 384, 385, 1000)
- Edge cases: empty input throws, zero-range maps to 0x88, out-of-range
  clamping
- distanceSQ4Js agreement with dequantize-then-cosine baseline within 1e-9
- dispatch swap (setSQ4DistanceImplementation): swap in a sentinel fn, observe
  the active fn changes, restore the JS default, observe the revert
- serialize/deserialize round-trip preserves all four fields byte-for-byte for
  both even and odd dim

The HNSW SQ4 reranking path (bits === 4 routing in HNSWIndex's quantization
config) is wired to use distanceSQ4 — when cortex's distance:sq4 provider
is registered, that hot path immediately becomes native SIMD with zero brainy
change required. Cross-language byte-format parity tests run in the cortex
test suite (paired release brainy 7.28.0 + cortex 2.5.0).
2026-05-28 12:27:10 -07:00
..
collation.test.ts fix: deterministic code-point string collation for column store + aggregation 2026-05-26 17:35:18 -07:00
contentExtractor.test.ts feat: expand ContentCategory to universal 6-category set for highlight() 2026-01-27 13:44:58 -08:00
entity-id-mapper-stability.test.ts feat: mmap-vector backend wiring — HNSWIndex consumes vectorStore:mmap (2.4.0 #2) 2026-05-28 10:37:47 -07:00
memoryLimits.test.ts feat: COW always-on architecture + cloud storage clear() fix (v5.11.0) 2025-11-18 13:44:02 -08:00
metadataIndex-text-indexing.test.ts feat: add structured content extraction and batch embedding optimization to highlight() 2026-01-27 10:27:22 -08:00
metadataIndex-type-aware.test.ts fix: eliminate flaky test timeouts and add storage adapters guide 2026-01-31 09:11:20 -08:00
mutex.test.ts feat: add distributed architecture with sharding and coordination 2025-09-22 15:45:35 -07:00
paramValidation.test.ts fix: resolve 10 test failures across clustering, metadata, and deletion 2025-10-09 16:33:08 -07:00
resultRanking.test.ts feat: hook native sort:topK provider into search result ranking 2026-05-27 13:13:47 -07:00
roaring-bitmap-integration.test.ts fix: migrate from roaring (native C++) to roaring-wasm for universal compatibility 2025-10-14 10:24:59 -07:00
unifiedCache-eviction.test.ts fix: correct cache eviction formula to prioritize high-value items 2025-10-13 11:21:19 -07:00
vectorQuantization-sq4.test.ts feat: SQ4 (4-bit) scalar quantization + native distance hook (2.5.0 #30) 2026-05-28 12:27:10 -07:00