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:
parent
076c26f6fd
commit
8f87b35614
4 changed files with 172 additions and 12 deletions
|
|
@ -21,7 +21,7 @@ import type { CacheProvider } from '../plugin.js'
|
|||
|
||||
export interface CacheItem {
|
||||
key: string
|
||||
type: 'hnsw' | 'metadata' | 'embedding' | 'other'
|
||||
type: 'hnsw' | 'vectors' | 'metadata' | 'embedding' | 'other'
|
||||
data: any
|
||||
size: number
|
||||
rebuildCost: number // milliseconds to rebuild
|
||||
|
|
@ -69,7 +69,7 @@ export class UnifiedCache implements CacheProvider {
|
|||
private cache = new Map<string, CacheItem>()
|
||||
private access = new Map<string, number>() // Access counts
|
||||
private loadingPromises = new Map<string, Promise<any>>()
|
||||
private typeAccessCounts = { hnsw: 0, metadata: 0, embedding: 0, other: 0 }
|
||||
private typeAccessCounts = { hnsw: 0, vectors: 0, metadata: 0, embedding: 0, other: 0 }
|
||||
private totalAccessCount = 0
|
||||
private currentSize = 0
|
||||
private maxSize: number
|
||||
|
|
@ -209,7 +209,7 @@ export class UnifiedCache implements CacheProvider {
|
|||
set(
|
||||
key: string,
|
||||
data: any,
|
||||
type: 'hnsw' | 'metadata' | 'embedding' | 'other',
|
||||
type: 'hnsw' | 'vectors' | 'metadata' | 'embedding' | 'other',
|
||||
size: number,
|
||||
rebuildCost: number = 1
|
||||
): void {
|
||||
|
|
@ -323,8 +323,8 @@ export class UnifiedCache implements CacheProvider {
|
|||
|
||||
private checkFairness(): void {
|
||||
// Calculate type ratios in cache
|
||||
const typeSizes = { hnsw: 0, metadata: 0, embedding: 0, other: 0 }
|
||||
const typeCounts = { hnsw: 0, metadata: 0, embedding: 0, other: 0 }
|
||||
const typeSizes = { hnsw: 0, vectors: 0, metadata: 0, embedding: 0, other: 0 }
|
||||
const typeCounts = { hnsw: 0, vectors: 0, metadata: 0, embedding: 0, other: 0 }
|
||||
|
||||
for (const item of this.cache.values()) {
|
||||
typeSizes[item.type] += item.size
|
||||
|
|
@ -335,6 +335,7 @@ export class UnifiedCache implements CacheProvider {
|
|||
const totalAccess = this.totalAccessCount || 1
|
||||
const accessRatios = {
|
||||
hnsw: this.typeAccessCounts.hnsw / totalAccess,
|
||||
vectors: this.typeAccessCounts.vectors / totalAccess,
|
||||
metadata: this.typeAccessCounts.metadata / totalAccess,
|
||||
embedding: this.typeAccessCounts.embedding / totalAccess,
|
||||
other: this.typeAccessCounts.other / totalAccess
|
||||
|
|
@ -344,6 +345,7 @@ export class UnifiedCache implements CacheProvider {
|
|||
const totalSize = this.currentSize || 1
|
||||
const sizeRatios = {
|
||||
hnsw: typeSizes.hnsw / totalSize,
|
||||
vectors: typeSizes.vectors / totalSize,
|
||||
metadata: typeSizes.metadata / totalSize,
|
||||
embedding: typeSizes.embedding / totalSize,
|
||||
other: typeSizes.other / totalSize
|
||||
|
|
@ -351,7 +353,7 @@ export class UnifiedCache implements CacheProvider {
|
|||
|
||||
// Check for starvation (more aggressive - 70% cache with <15% accesses)
|
||||
// Previous: 90% cache, <10% access (too lenient, caused thrashing)
|
||||
for (const type of ['hnsw', 'metadata', 'embedding', 'other'] as const) {
|
||||
for (const type of ['hnsw', 'vectors', 'metadata', 'embedding', 'other'] as const) {
|
||||
if (sizeRatios[type] > 0.7 && accessRatios[type] < 0.15) {
|
||||
prodLog.warn(`Type ${type} is hogging cache (${(sizeRatios[type] * 100).toFixed(1)}% size, ${(accessRatios[type] * 100).toFixed(1)}% access)`)
|
||||
this.evictType(type)
|
||||
|
|
@ -364,7 +366,7 @@ export class UnifiedCache implements CacheProvider {
|
|||
* Called immediately when adding items to prevent imbalance formation
|
||||
* Uses same thresholds as periodic check but runs on-demand
|
||||
*/
|
||||
private checkProactiveFairness(addedType: 'hnsw' | 'metadata' | 'embedding' | 'other'): void {
|
||||
private checkProactiveFairness(addedType: 'hnsw' | 'vectors' | 'metadata' | 'embedding' | 'other'): void {
|
||||
// Quick check: only evaluate the type being added
|
||||
let typeSize = 0
|
||||
for (const item of this.cache.values()) {
|
||||
|
|
@ -386,7 +388,7 @@ export class UnifiedCache implements CacheProvider {
|
|||
/**
|
||||
* Force evict items of a specific type
|
||||
*/
|
||||
private evictType(type: 'hnsw' | 'metadata' | 'embedding' | 'other'): void {
|
||||
private evictType(type: 'hnsw' | 'vectors' | 'metadata' | 'embedding' | 'other'): void {
|
||||
const candidates: Array<[string, number, CacheItem]> = []
|
||||
|
||||
for (const [key, item] of this.cache) {
|
||||
|
|
@ -466,7 +468,7 @@ export class UnifiedCache implements CacheProvider {
|
|||
/**
|
||||
* Clear cache or specific type
|
||||
*/
|
||||
clear(type?: 'hnsw' | 'metadata' | 'embedding' | 'other'): void {
|
||||
clear(type?: 'hnsw' | 'vectors' | 'metadata' | 'embedding' | 'other'): void {
|
||||
if (!type) {
|
||||
this.cache.clear()
|
||||
this.currentSize = 0
|
||||
|
|
@ -533,8 +535,8 @@ export class UnifiedCache implements CacheProvider {
|
|||
* Get cache statistics with memory information
|
||||
*/
|
||||
getStats() {
|
||||
const typeSizes = { hnsw: 0, metadata: 0, embedding: 0, other: 0 }
|
||||
const typeCounts = { hnsw: 0, metadata: 0, embedding: 0, other: 0 }
|
||||
const typeSizes = { hnsw: 0, vectors: 0, metadata: 0, embedding: 0, other: 0 }
|
||||
const typeCounts = { hnsw: 0, vectors: 0, metadata: 0, embedding: 0, other: 0 }
|
||||
|
||||
for (const item of this.cache.values()) {
|
||||
typeSizes[item.type] += item.size
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue