Brainy 8.0 makes subtype required by default on every public write path
(`add`, `addMany`, `update`, `relate`, `relateMany`, `updateRelation`,
import). Per the locked C-1 contract, every entity and relation gets a
non-empty subtype string by the time the storage layer sees it.
OPT-OUT REMAINS FULLY SUPPORTED
The runtime flag is still consumer-controlled. Three opt-out paths
cover migration / legacy fixtures / typed escape:
- `new Brainy({ requireSubtype: false })` — last-resort: turn off the
contract entirely. Recommended only for migration windows or test
fixtures that legitimately can't supply a subtype.
- `new Brainy({ requireSubtype: { except: [NounType.Thing, ...] } })` —
per-type allowlist: strict everywhere except the listed types.
- `brain.requireSubtype(type, options)` — per-type registration with
optional vocabulary. Composes with the brain-wide flag.
Default is now `true`. Opt-out is explicit and documented; nothing
silently degrades.
TEST SWEEP
Bulk-applied `requireSubtype: false` to every `new Brainy({...})` call
site across 120 test files. Three sed patterns covered the shapes:
- `new Brainy({` → `new Brainy({ requireSubtype: false,`
- `new Brainy<T>({` → `new Brainy<T>({ requireSubtype: false,`
- `new Brainy()` → `new Brainy({ requireSubtype: false })`
tests/helpers/test-factory.ts → createTestConfig() defaults
`requireSubtype: false` so test files using the helper inherit the
opt-out without per-site edits.
The test sites that DO exercise subtype semantics (the
subtype-and-facets suite, the strict-mode-self-test suite, the verb-
subtype-and-enforcement suite, etc.) already pass real subtypes — they
were the 7.30.x acceptance tests for this contract. Those tests
continue to pass unchanged.
CHANGES
src/brainy.ts
- normalizeConfig() — `requireSubtype` default `false` → `true`.
Comment refreshed to document the three opt-out paths.
tests/* (120 files)
- Bulk-edited brain construction sites. No functional test changes; the
opt-out preserves the test author's original intent.
tests/helpers/test-factory.ts
- createTestConfig() base config gains `requireSubtype: false`.
NO-OP for consumers who were already passing subtype on every write.
For consumers who weren't, the upgrade path is one of the three opt-out
forms above. Migration recipe documented in 8.0 release notes (next
commit).
VERIFICATION
- npx tsc --noEmit: clean
- npm test: 1408 / 1409 (same pre-existing race-condition outstanding;
no other regressions from the flip)
207 lines
6 KiB
TypeScript
207 lines
6 KiB
TypeScript
/**
|
|
* VFS Multiple Init() Diagnostic Test
|
|
*
|
|
* Tests a consumer's issue: Does calling vfs.init() multiple times
|
|
* create duplicate root entities?
|
|
*
|
|
* Scenario:
|
|
* - Create brain instance
|
|
* - Call vfs.init() multiple times (simulating multiple requests)
|
|
* - Check if multiple root entities are created
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
|
import { Brainy } from '../../src/brainy.js'
|
|
import * as fs from 'fs'
|
|
import * as path from 'path'
|
|
|
|
describe('VFS Multiple Init Diagnostic', () => {
|
|
const testDir = path.join(process.cwd(), 'test-vfs-multi-init')
|
|
let brain: Brainy
|
|
|
|
beforeAll(async () => {
|
|
// Clean up test directory
|
|
if (fs.existsSync(testDir)) {
|
|
fs.rmSync(testDir, { recursive: true, force: true })
|
|
}
|
|
fs.mkdirSync(testDir, { recursive: true })
|
|
|
|
// Create brain with filesystem storage
|
|
brain = new Brainy({ requireSubtype: false,
|
|
storage: {
|
|
type: 'filesystem',
|
|
path: testDir
|
|
}
|
|
})
|
|
await brain.init()
|
|
})
|
|
|
|
afterAll(() => {
|
|
// Clean up
|
|
if (fs.existsSync(testDir)) {
|
|
fs.rmSync(testDir, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('should not create duplicate roots when calling vfs.init() multiple times on SAME instance', async () => {
|
|
const vfs = brain.vfs
|
|
|
|
// Call init multiple times
|
|
await vfs.init()
|
|
await vfs.init()
|
|
await vfs.init()
|
|
|
|
// Query for root entities using workaround (where clause is broken)
|
|
const collections = await brain.find({
|
|
type: 'collection',
|
|
limit: 100
|
|
})
|
|
|
|
const roots = collections.filter(e =>
|
|
e.metadata?.path === '/' &&
|
|
e.metadata?.vfsType === 'directory'
|
|
)
|
|
|
|
console.log(`\n✅ Test 1: Same VFS instance`)
|
|
console.log(` Total collections: ${collections.length}`)
|
|
console.log(` Roots found: ${roots.length}`)
|
|
roots.forEach((root, i) => {
|
|
console.log(` Root ${i + 1}: ${root.id}`)
|
|
})
|
|
|
|
expect(roots.length).toBe(1)
|
|
})
|
|
|
|
it('should not create duplicate roots when creating MULTIPLE VFS instances (the reported scenario)', async () => {
|
|
// Simulate the reported scenario: Getting VFS on multiple requests
|
|
// brain.vfs returns cached instance, so this should be safe
|
|
const vfs1 = brain.vfs
|
|
const vfs2 = brain.vfs
|
|
const vfs3 = brain.vfs
|
|
|
|
await vfs1.init()
|
|
await vfs2.init()
|
|
await vfs3.init()
|
|
|
|
// Query for root entities using workaround
|
|
const collections = await brain.find({
|
|
type: 'collection',
|
|
limit: 100
|
|
})
|
|
|
|
const roots = collections.filter(e =>
|
|
e.metadata?.path === '/' &&
|
|
e.metadata?.vfsType === 'directory'
|
|
)
|
|
|
|
console.log(`\n✅ Test 2: Multiple VFS references (cached)`)
|
|
console.log(` VFS instances are same? ${vfs1 === vfs2 && vfs2 === vfs3}`)
|
|
console.log(` Roots found: ${roots.length}`)
|
|
roots.forEach((root, i) => {
|
|
console.log(` Root ${i + 1}: ${root.id}`)
|
|
})
|
|
|
|
expect(vfs1).toBe(vfs2) // Should be same instance (cached)
|
|
expect(roots.length).toBe(1) // Should still be 1 root
|
|
})
|
|
|
|
it('should handle NEW brain instances gracefully (per-user scenario)', async () => {
|
|
// Simulate creating separate brain instances per user
|
|
// This is closer to a consumer's getUserBrainy() pattern
|
|
const brain2 = new Brainy({ requireSubtype: false,
|
|
storage: {
|
|
type: 'filesystem',
|
|
path: testDir // Same storage!
|
|
}
|
|
})
|
|
await brain2.init()
|
|
|
|
const vfs2 = brain2.vfs()
|
|
await vfs2.init()
|
|
|
|
// Query for roots using workaround
|
|
const collections = await brain.find({
|
|
type: 'collection',
|
|
limit: 100
|
|
})
|
|
|
|
const roots = collections.filter(e =>
|
|
e.metadata?.path === '/' &&
|
|
e.metadata?.vfsType === 'directory'
|
|
)
|
|
|
|
console.log(`\n✅ Test 3: New Brainy instance (same storage)`)
|
|
console.log(` Roots found: ${roots.length}`)
|
|
roots.forEach((root, i) => {
|
|
console.log(` Root ${i + 1}: ${root.id} (created: ${root.metadata?.createdAt})`)
|
|
})
|
|
|
|
// This is the CRITICAL test - should find existing root, not create new one
|
|
expect(roots.length).toBe(1)
|
|
})
|
|
|
|
it('should verify readdir works after multiple inits', async () => {
|
|
// Create a test directory
|
|
const vfs = brain.vfs
|
|
await vfs.mkdir('/test-dir', { recursive: true })
|
|
await vfs.writeFile('/test-dir/test.txt', 'Hello')
|
|
|
|
// Call init again (simulating new request)
|
|
const brain3 = new Brainy({ requireSubtype: false,
|
|
storage: {
|
|
type: 'filesystem',
|
|
path: testDir
|
|
}
|
|
})
|
|
await brain3.init()
|
|
const vfs3 = brain3.vfs()
|
|
await vfs3.init()
|
|
|
|
// Try to read root directory
|
|
const entries = await vfs3.readdir('/', { withFileTypes: true })
|
|
|
|
console.log(`\n✅ Test 4: readdir after new brain instance`)
|
|
console.log(` Entries found: ${entries.length}`)
|
|
entries.forEach((entry: any) => {
|
|
console.log(` - ${entry.name} (${entry.type})`)
|
|
})
|
|
|
|
expect(entries.length).toBeGreaterThan(0)
|
|
})
|
|
|
|
it('should show if Contains relationships are created', async () => {
|
|
const vfs = brain.vfs
|
|
|
|
// Get root entity ID using workaround
|
|
const collections = await brain.find({
|
|
type: 'collection',
|
|
limit: 100
|
|
})
|
|
|
|
const roots = collections.filter(e =>
|
|
e.metadata?.path === '/' &&
|
|
e.metadata?.vfsType === 'directory'
|
|
)
|
|
|
|
expect(roots.length).toBeGreaterThanOrEqual(1)
|
|
const rootId = roots[0].id
|
|
|
|
// Check for Contains relationships FROM root
|
|
const relations = await brain.getRelations({
|
|
from: rootId
|
|
})
|
|
|
|
console.log(`\n✅ Test 5: Contains relationships`)
|
|
console.log(` Root ID: ${rootId}`)
|
|
console.log(` Relationships from root: ${relations.length}`)
|
|
relations.forEach((rel) => {
|
|
console.log(` - ${rel.from} -> ${rel.to} (${rel.type})`)
|
|
})
|
|
|
|
// Root should have at least one Contains relationship (to /test-dir)
|
|
const containsRels = relations.filter(r => r.type === 'contains')
|
|
console.log(` Contains relationships: ${containsRels.length}`)
|
|
|
|
expect(containsRels.length).toBeGreaterThan(0)
|
|
})
|
|
})
|