refactor(8.0): add config.vector path + 'vectors' cache category (scaffold step 2)

Step 2 of the brainy 8.0 rename scaffolding. Adds the new algorithm-neutral
config surface alongside the legacy config.hnsw path (which becomes a compat
shim removed in the final cleanup commit).

CHANGES

src/utils/recallPreset.ts (NEW)
- DEFAULT_RECALL = 'balanced'
- HNSW_PRESETS table — fast/balanced/accurate → (M, efConstruction, efSearch).
  'balanced' equals brainy 7.x DEFAULT_CONFIG byte-for-byte so a 7.x → 8.0
  upgrade with no explicit recall value is a no-op.
- resolveRecallHnswKnobs(preset) — preset → knobs.
- resolveJsHnswConfig(vectorConfig) — preset first, then `advanced.hnsw` overrides
  win. Explicit knobs always beat the preset.

src/types/brainy.types.ts
- New optional `vector` field on BrainyConfig:
    vector?: {
      recall?: 'fast' | 'balanced' | 'accurate'
      quantization?: { enabled?, bits?: 8 | 4, rerankMultiplier? }
      vectorStorage?: 'memory' | 'lazy'
      advanced?: {
        hnsw?: { M?, efConstruction?, efSearch?, ml? }
        diskann?: { pqM?, pqKsub?, maxDegree?, searchListSize?, alpha?, ... }
      }
    }
- `hnsw` field marked @deprecated with note that it stays as a compat shim
  during the 8.0 rename and is removed in the final cleanup commit.

src/utils/unifiedCache.ts
- Cache-category union widened from
    'hnsw' | 'metadata' | 'embedding' | 'other'
  to
    'hnsw' | 'vectors' | 'metadata' | 'embedding' | 'other'
- 5 union sites + 4 typed-counter maps (typeAccessCounts, checkFairness
  typeSizes/typeCounts/accessRatios/sizeRatios, getStats typeSizes/typeCounts,
  evictType loop) all gained the 'vectors' key with initial value 0.
- The hard-coded fairness-check loop now iterates both keys.
- Final 8.0 cleanup will narrow back to 'vectors' | 'metadata' | 'embedding' | 'other'.

src/brainy.ts (normalizeConfig)
- Adds the new `vector` field to the Required<BrainyConfig> return shape so
  TS compiles. Reads config?.vector ?? undefined as any (same pattern as
  the other optional config fields).

NO-OP scope

No behavioural change. The new config.vector path is silently ignored at
runtime in this commit — wired up in step 4 (storage + index resolution).
Pure surface plumbing. Tests + build green.

VERIFICATION

- npx tsc --noEmit: clean
- npm test: 1468 / 1468 unit
This commit is contained in:
David Snelling 2026-06-09 13:06:10 -07:00
parent 076c26f6fd
commit 8f87b35614
4 changed files with 172 additions and 12 deletions

View file

@ -1117,6 +1117,9 @@ export interface BrainyConfig {
hnswPersistMode?: 'immediate' | 'deferred'
// HNSW optimization options (v7.11.0)
// @deprecated 8.0 — use `vector` instead. This field is kept as a compat
// shim during the 8.0 rename scaffolding; values are merged into the new
// `vector` block with explicit `vector.*` values winning on conflict.
hnsw?: {
quantization?: {
enabled?: boolean // default: false — current behavior exactly
@ -1126,6 +1129,67 @@ export interface BrainyConfig {
vectorStorage?: 'memory' | 'lazy' // default: 'memory' — 'lazy' evicts vectors after insert
}
/**
* Vector index configuration (Brainy 8.0).
*
* Algorithm-neutral surface. The `recall` preset means the same thing
* whether Brainy's open-core JS HNSW path is in play or a native
* acceleration provider has taken over the `'vector'` provider key:
* - `'fast'` minimum-latency search, accepts lower recall
* - `'balanced'` Brainy 7.x defaults, the right pick for almost everyone
* - `'accurate'` maximum recall, accepts higher latency
*
* Power users may override individual knobs via `advanced.hnsw` (the JS
* path) or `advanced.diskann` (a native DiskANN-style provider). The
* preset is the floor; explicit overrides win.
*
* **Closed-form contract** locked in handoff thread
* BRAINY-8.0-RENAME-COORDINATION § A.2 (cortex-confirmed 2026-06-09).
*/
vector?: {
/** Recall preset. Defaults to `'balanced'` when omitted. */
recall?: 'fast' | 'balanced' | 'accurate'
/**
* Vector quantization. Independent of `recall` it trades RAM for a
* small recall hit at any preset. `bits: 8` is SQ8 (4× memory reduction,
* ~0.4% recall loss). `bits: 4` is SQ4 (8× memory reduction, ~1-3% recall
* loss). Both ship in the open-core path; cortex's `distance:sq8` /
* `distance:sq4` SIMD providers accelerate them when available.
*/
quantization?: {
enabled?: boolean
bits?: 8 | 4
rerankMultiplier?: number
}
/** Vector storage mode. `'memory'` keeps vectors resident; `'lazy'` evicts after insert. */
vectorStorage?: 'memory' | 'lazy'
/**
* Power-user overrides. The recall preset works for 99% of users use
* these only if you've measured. `hnsw` overrides apply on the JS HNSW
* path; `diskann` overrides apply when a DiskANN-style native provider
* is in play. Both blocks are honored on their respective paths and
* silently ignored on the other (a one-shot warning fires when
* `strictConfig !== false`).
*/
advanced?: {
hnsw?: {
M?: number
efConstruction?: number
efSearch?: number
ml?: number
}
diskann?: {
pqM?: number
pqKsub?: number
maxDegree?: number
searchListSize?: number
alpha?: number
useMmapAdjacency?: boolean
mmapAdjacencyPath?: string
}
}
}
// Memory management options
maxQueryLimit?: number // Override auto-detected query result limit (max: 100000)
reservedQueryMemory?: number // Memory reserved for queries in bytes (e.g., 1073741824 = 1GB)