fix(vfs): the VFS root never persists a zero-norm vector
Some checks failed
CI / Node 22 (push) Successful in 12m23s
CI / Node 24 (push) Successful in 12m12s
CI / Integration + conformance (Node 22) (push) Failing after 14m47s
CI / Bun (latest) (push) Successful in 12m38s

A zero-norm vector is lawful inside brainy (cosine distance scores it at
maximum, never a false top hit) but a false attractor for a downstream
engine serving squared-euclidean distance, which cannot tell a real
all-zero vector apart from a legitimate origin point.

- The VFS root now persists with vector [] (the existing "unvectored"
  shape) instead of a real all-zero 384-dim placeholder, and is never
  routed into the deferred-embed pipeline.
- A one-time migration in the root-init path detects a pre-fix store's
  all-zero placeholder root (by norm, not length) and rewrites it to []
  through a new sanctioned Brainy method that keeps the canonical
  vectored-noun ledger honest and removes the row from the vector index.
- The vector-index write seam (AddToVectorIndexOperation,
  ReplaceInVectorIndexOperation, and the generation materializer's direct
  insert) now refuses any real all-zero vector before it reaches a
  provider, loudly naming the entity, while the canonical write still
  lands.
- add()'s dimension-pinning and HNSW-insert gates, and the add-params
  validator, now treat any empty vector as carrying no dimension
  information, closing a latent trap where an explicit `vector: []`
  would have pinned dimensions to 0.
This commit is contained in:
David Snelling 2026-08-27 09:28:44 -07:00
parent aad9e2eeb1
commit c6cc0de955
10 changed files with 516 additions and 64 deletions

View file

@ -225,9 +225,12 @@ describe('canonical count ledger — the vectored-noun scalar (the vector leg\'s
}
/** Baseline vectored count right after a fresh open() init() creates a
* hidden system VFS-root noun that itself carries a real vector, so a
* brand-new store's `vectors.all` is 1, not 0. Tests assert DELTAS off
* this baseline rather than hardcoding it away. */
* hidden system VFS-root noun, but (the zero-norm root cure) it is
* deliberately UNVECTORED (`vector: []`, never a real all-zero
* placeholder a zero-norm vector never crosses an engine boundary), so
* a brand-new store's `vectors.all` is 0. Tests still assert DELTAS off
* this baseline rather than hardcoding it away, in case that ever
* changes again. */
let baseline: number
beforeEach(async () => {
@ -235,6 +238,7 @@ describe('canonical count ledger — the vectored-noun scalar (the vector leg\'s
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'brainy-vectored-ledger-'))
brain = await open()
baseline = (await brain.storage.getCanonicalCounts()).vectors.all
expect(baseline).toBe(0) // the unvectored VFS root contributes nothing
})
afterEach(async () => {
vi.restoreAllMocks()
@ -348,7 +352,7 @@ describe('canonical count ledger — the vectored-noun scalar (the vector leg\'s
brain = await open()
const ledger = await brain.storage.getCanonicalCounts()
expect(ledger.vectors.all).toBe(baseline + 1) // the root + the one non-deferred noun
expect(ledger.vectors.all).toBe(baseline + 1) // just the one non-deferred noun — the root is unvectored
expect(ledger.vectors.all).toBe(countVectoredNouns(dir))
const persisted = JSON.parse(fs.readFileSync(countsPath, 'utf-8'))
expect(persisted.totalVectoredNounCount).toBe(baseline + 1)