brainy/tests/unit/storage/fileSystemStorage-rootDirectory.test.ts
David Snelling 3f8e0971a2 fix: query-cap memory misread (MemAvailable + floor) + rootDirectory getter for native mmap fast-path
Two platform-wide production fixes for consumers on bare VMs / mmap-filesystem storage.

BUG A — auto maxQueryLimit collapsed to ~1000 on healthy VMs → 500s on legitimate
queries. getAvailableMemory() read os.freemem() (kernel MemFree, which excludes
reclaimable page cache and reads as tens of MB on a page-cache-heavy mmap box).
Now reads /proc/meminfo MemAvailable (the `free -h` figure), falling back to
os.freemem() only off-Linux; auto-detected caps (container/free branches) are floored
at 10k so a misread can't collapse them. Explicit maxQueryLimit/reservedQueryMemory
are honored as-is.

BUG B — native mmap vector fast-path never engaged (per-entity reads → 57s cold start
on a 283MB brain). FileSystemStorage now exposes a public `rootDirectory` getter; the
native vector provider feature-detects it to enable its memory-mapped graph path.
brainy stored it as the protected `rootDir`, so the gate silently failed. Self-heals
after the first post-upgrade flush writes the mmap file.

Regression tests: auto-cap floor (container/free/explicit-override) + the rootDirectory
getter. Build clean; full suite 1483 green.
2026-06-16 08:46:39 -07:00

28 lines
1.3 KiB
TypeScript

/**
* Regression: FileSystemStorage must expose a public `rootDirectory` getter.
*
* Native vector plugins (e.g. @soulcraft/cortex's NativeHNSWWrapper) feature-detect
* `storage.rootDirectory` to enable their memory-mapped vector fast path. Brainy
* stores the path as the protected `rootDir`; without this public alias the native
* mmap backend stayed un-wired and the index rebuilt via slow per-entity disk reads
* on cold start (BRAINY-MMAP-VECTOR-HOOK). The getter is the load-bearing line, so
* guard it explicitly.
*/
import { describe, it, expect } from 'vitest'
import { mkdtempSync, rmSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
import { FileSystemStorage } from '../../../src/storage/adapters/fileSystemStorage.js'
describe('FileSystemStorage.rootDirectory (native mmap-backend hook)', () => {
it('exposes the constructor root path via a public rootDirectory getter', () => {
const dir = mkdtempSync(join(tmpdir(), 'brainy-rootdir-'))
try {
const storage = new FileSystemStorage(dir)
// The exact property a native vector backend reads to engage its mmap path.
expect((storage as unknown as { rootDirectory: string }).rootDirectory).toBe(dir)
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
})