feat: brain.find() excludes VFS by default (Option 3C)

Added includeVFS parameter to FindParams:
- brain.find() excludes VFS entities by default (clean knowledge graph)
- Opt-in with brain.find({ includeVFS: true })
- Automatically excludes VFS in all query paths (empty, metadata, vector)
- Respects explicit where: { isVFS: ... } queries

Implementation:
- Empty query path: Apply VFS filtering even with no criteria
- Metadata query path: Filter out isVFS: true by default
- Vector search path: Apply VFS filter after search
- Skip auto-exclusion if where clause explicitly queries isVFS

Architecture (Option 3C):
- VFS entities are first-class graph entities
- Marked with isVFS: true flag
- Separated via filtering, not storage
- Enables VFS-knowledge relationships

Moved internal docs to .strategy/:
- README_STORAGE_EXPLORATION.md
- EXPLORATION_SUMMARY.md
- STORAGE_FILES_REFERENCE.md
- STORAGE_ADAPTER_QUICK_REFERENCE.md
- SECURITY.md
This commit is contained in:
David Snelling 2025-10-24 11:42:47 -07:00
parent 86f5956d59
commit 014b8104da
14 changed files with 1172 additions and 1807 deletions

View file

@ -0,0 +1,207 @@
/**
* VFS-Knowledge Separation Test (v4.3.3)
*
* Tests Option 3C Architecture:
* - VFS entities marked with isVFS: true
* - brain.find() excludes VFS by default
* - brain.find({ includeVFS: true }) includes VFS
* - Enables relationships between VFS and knowledge
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { Brainy } from '../../src/brainy.js'
import { NounType } from '../../src/types/graphTypes.js'
import * as fs from 'fs'
import * as path from 'path'
describe('VFS-Knowledge Separation (Option 3C)', () => {
const testDir = path.join(process.cwd(), 'test-vfs-knowledge-separation')
let brain: Brainy
beforeAll(async () => {
// Clean up
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true })
}
fs.mkdirSync(testDir, { recursive: true })
brain = new Brainy({
storage: {
type: 'filesystem',
options: { path: testDir }
}
})
await brain.init()
})
afterAll(() => {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true })
}
})
it('should exclude VFS entities from brain.find() by default', async () => {
// Create VFS file entity
const vfs = brain.vfs()
await vfs.init()
await vfs.mkdir('/docs', { recursive: true })
await vfs.writeFile('/docs/readme.md', '# Hello World')
// Create knowledge entity (no isVFS flag)
const knowledgeEntity = await brain.add({
data: 'This is a knowledge document about AI',
type: NounType.Document,
metadata: {
title: 'AI Research Paper',
category: 'research'
}
})
// Query for documents WITHOUT includeVFS
console.log('\n📋 Test 1: brain.find() excludes VFS by default')
const results = await brain.find({
type: NounType.Document,
limit: 100
})
console.log(` Total results: ${results.length}`)
const vfsResults = results.filter(r => r.metadata?.isVFS === true)
const knowledgeResults = results.filter(r => r.metadata?.isVFS !== true)
console.log(` VFS results: ${vfsResults.length}`)
console.log(` Knowledge results: ${knowledgeResults.length}`)
// Should only return knowledge entities (no VFS)
expect(vfsResults.length).toBe(0)
expect(knowledgeResults.length).toBeGreaterThan(0)
expect(results.some(r => r.id === knowledgeEntity.id)).toBe(true)
})
it('should include VFS entities when includeVFS: true', async () => {
console.log('\n📋 Test 2: brain.find({ includeVFS: true }) includes VFS')
// Query WITH includeVFS: true
const results = await brain.find({
type: NounType.Document,
includeVFS: true,
limit: 100
})
console.log(` Total results: ${results.length}`)
const vfsResults = results.filter(r => r.metadata?.isVFS === true)
const knowledgeResults = results.filter(r => r.metadata?.isVFS !== true)
console.log(` VFS results: ${vfsResults.length}`)
console.log(` Knowledge results: ${knowledgeResults.length}`)
// Should return BOTH VFS and knowledge entities
expect(vfsResults.length).toBeGreaterThan(0)
expect(knowledgeResults.length).toBeGreaterThan(0)
})
it('should allow relationships between VFS files and knowledge entities', async () => {
console.log('\n📋 Test 3: VFS-Knowledge relationships')
// Get VFS file entity (need includeVFS: true when querying VFS by path)
const vfsFile = await brain.find({
where: {
path: '/docs/readme.md'
},
includeVFS: true,
limit: 1
})
expect(vfsFile.length).toBe(1)
// Get knowledge entity
const knowledgeEntity = await brain.find({
type: NounType.Document,
where: {
title: 'AI Research Paper'
},
limit: 1
})
expect(knowledgeEntity.length).toBe(1)
// Create relationship: knowledge entity -> references -> VFS file
const relation = await brain.relate({
from: knowledgeEntity[0].id,
to: vfsFile[0].id,
type: 'references'
})
console.log(` Created relation: ${relation.id}`)
console.log(` From: ${knowledgeEntity[0].metadata?.title} (knowledge)`)
console.log(` To: ${vfsFile[0].metadata?.path} (VFS file)`)
// Verify relationship exists
const relations = await brain.getRelations({
from: knowledgeEntity[0].id,
to: vfsFile[0].id
})
expect(relations.length).toBe(1)
expect(relations[0].type).toBe('references')
console.log(` ✅ Relationship verified: knowledge can reference VFS files`)
})
it('should filter VFS entities using where clause', async () => {
console.log('\n📋 Test 4: Where clause filtering with isVFS')
// Query for VFS files explicitly
const vfsFiles = await brain.find({
where: {
isVFS: true,
vfsType: 'file'
},
limit: 100
})
console.log(` VFS files found: ${vfsFiles.length}`)
expect(vfsFiles.length).toBeGreaterThan(0)
expect(vfsFiles.every(f => f.metadata?.isVFS === true)).toBe(true)
expect(vfsFiles.every(f => f.metadata?.vfsType === 'file')).toBe(true)
// Query for non-VFS entities explicitly
const nonVFS = await brain.find({
where: {
category: 'research'
},
limit: 100
})
console.log(` Non-VFS entities found: ${nonVFS.length}`)
expect(nonVFS.length).toBeGreaterThan(0)
expect(nonVFS.every(e => e.metadata?.isVFS !== true)).toBe(true)
})
it('should handle semantic search with VFS filtering', async () => {
console.log('\n📋 Test 5: Semantic search excludes VFS by default')
// Semantic search WITHOUT includeVFS (should exclude VFS files)
const results = await brain.find({
query: 'research paper artificial intelligence',
limit: 10
})
console.log(` Semantic results: ${results.length}`)
const hasVFS = results.some(r => r.metadata?.isVFS === true)
console.log(` Contains VFS entities: ${hasVFS}`)
// Should NOT include VFS files in semantic search by default
expect(hasVFS).toBe(false)
// Semantic search WITH includeVFS: true
const resultsWithVFS = await brain.find({
query: 'hello world markdown',
includeVFS: true,
limit: 10
})
console.log(` Semantic results (with VFS): ${resultsWithVFS.length}`)
const hasVFSWithFlag = resultsWithVFS.some(r => r.metadata?.isVFS === true)
console.log(` Contains VFS entities: ${hasVFSWithFlag}`)
// Should include VFS files when explicitly requested
expect(hasVFSWithFlag).toBe(true)
})
})

View file

@ -0,0 +1,86 @@
/**
* Debug: Check metadata index for isVFS field
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { Brainy } from '../../src/brainy.js'
import { NounType } from '../../src/types/graphTypes.js'
import * as fs from 'fs'
import * as path from 'path'
describe('Metadata Index Debug', () => {
const testDir = path.join(process.cwd(), 'test-metadata-index-debug')
let brain: Brainy
beforeAll(async () => {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true })
}
fs.mkdirSync(testDir, { recursive: true })
brain = new Brainy({
storage: {
type: 'filesystem',
options: { path: testDir }
}
})
await brain.init()
})
afterAll(() => {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true })
}
})
it('should index custom metadata fields', async () => {
// Add entity with custom metadata field
const entity1 = await brain.add({
data: 'Test entity with custom field',
type: NounType.Document,
metadata: {
customFlag: true,
customString: 'hello',
customNumber: 42
}
})
console.log('\n📋 Added entity:', entity1.id)
console.log(' Metadata:', entity1.metadata)
// Try to find by custom fields
console.log('\n📋 Query 1: where: { customFlag: true }')
const result1 = await brain.find({
where: { customFlag: true },
limit: 10
})
console.log(` Results: ${result1.length}`)
if (result1.length > 0) {
console.log(` Found: ${result1[0].id}`)
console.log(` Metadata.customFlag: ${result1[0].metadata?.customFlag}`)
}
console.log('\n📋 Query 2: where: { customString: "hello" }')
const result2 = await brain.find({
where: { customString: 'hello' },
limit: 10
})
console.log(` Results: ${result2.length}`)
console.log('\n📋 Query 3: where: { customNumber: 42 }')
const result3 = await brain.find({
where: { customNumber: 42 },
limit: 10
})
console.log(` Results: ${result3.length}`)
// Check what fields are indexed
console.log('\n📋 Indexed fields:')
const fields = await brain.getFilterFields()
console.log(` ${fields.join(', ')}`)
expect(result1.length).toBeGreaterThan(0)
expect(result2.length).toBeGreaterThan(0)
expect(result3.length).toBeGreaterThan(0)
})
})

View file

@ -0,0 +1,90 @@
/**
* Diagnostic: Check if isVFS flag is actually being stored
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { Brainy } from '../../src/brainy.js'
import { NounType } from '../../src/types/graphTypes.js'
import * as fs from 'fs'
import * as path from 'path'
describe('isVFS Flag Diagnostic', () => {
const testDir = path.join(process.cwd(), 'test-isvfs-diagnostic')
let brain: Brainy
beforeAll(async () => {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true })
}
fs.mkdirSync(testDir, { recursive: true })
brain = new Brainy({
storage: {
type: 'filesystem',
options: { path: testDir }
}
})
await brain.init()
})
afterAll(() => {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true })
}
})
it('should show what entities are created', async () => {
// Create VFS file
const vfs = brain.vfs()
await vfs.init()
await vfs.writeFile('/test.txt', 'Hello World')
// Get ALL entities
console.log('\n📋 All entities in database:')
const allEntities = await brain.find({ limit: 100, includeVFS: true })
console.log(` Total: ${allEntities.length}`)
for (const entity of allEntities) {
console.log(`\n Entity: ${entity.id}`)
console.log(` Type: ${entity.type}`)
console.log(` Metadata keys: ${Object.keys(entity.metadata || {}).join(', ')}`)
if (entity.metadata?.path) {
console.log(` Path: ${entity.metadata.path}`)
}
if (entity.metadata?.vfsType) {
console.log(` VfsType: ${entity.metadata.vfsType}`)
}
if (entity.metadata?.isVFS !== undefined) {
console.log(` isVFS: ${entity.metadata.isVFS}`)
}
if (entity.metadata?.name) {
console.log(` Name: ${entity.metadata.name}`)
}
}
// Try different queries
console.log('\n\n📋 Query 1: brain.find({ limit: 100 }) [default, no includeVFS]')
const query1 = await brain.find({ limit: 100 })
console.log(` Results: ${query1.length}`)
console.log('\n📋 Query 2: brain.find({ limit: 100, includeVFS: true })')
const query2 = await brain.find({ limit: 100, includeVFS: true })
console.log(` Results: ${query2.length}`)
console.log('\n📋 Query 3: brain.find({ where: { isVFS: true } })')
const query3 = await brain.find({ where: { isVFS: true }, limit: 100 })
console.log(` Results: ${query3.length}`)
console.log('\n📋 Query 4: brain.find({ where: { path: "/test.txt" } })')
const query4 = await brain.find({ where: { path: '/test.txt' }, limit: 100 })
console.log(` Results: ${query4.length}`)
console.log('\n📋 Query 5: brain.find({ type: NounType.Document })')
const query5 = await brain.find({ type: NounType.Document, limit: 100 })
console.log(` Results: ${query5.length}`)
console.log('\n📋 Query 6: brain.find({ type: NounType.Document, includeVFS: true })')
const query6 = await brain.find({ type: NounType.Document, includeVFS: true, limit: 100 })
console.log(` Results: ${query6.length}`)
})
})

View file

@ -0,0 +1,207 @@
/**
* VFS Multiple Init() Diagnostic Test
*
* Tests Workshop team'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({
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 (Workshop scenario)', async () => {
// Simulate Workshop's 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 Workshop's getUserBrainy() pattern
const brain2 = new Brainy({
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({
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)
})
})

View file

@ -0,0 +1,171 @@
/**
* Manual test to reproduce Workshop team's type filtering issue
*
* This test verifies that brain.find({ type: NounType.Person }) actually works
* as documented in the API.
*/
import { Brainy, NounType } from '../../src/index.js'
async function testTypeFiltering() {
console.log('\n🔬 Testing Type Filtering Issue from Workshop Team\n')
console.log('=' .repeat(60))
// Create in-memory instance for testing
const brain = new Brainy({
storage: { type: 'memory' }
})
await brain.init()
console.log('\n1⃣ Adding test entities with different types...\n')
// Add 5 person entities
const people = []
for (let i = 0; i < 5; i++) {
const id = await brain.add({
data: `Person ${i + 1}`,
type: NounType.Person,
metadata: { name: `Person ${i + 1}` }
})
people.push(id)
}
console.log(`✅ Added ${people.length} person entities`)
// Add 3 location entities
const locations = []
for (let i = 0; i < 3; i++) {
const id = await brain.add({
data: `Location ${i + 1}`,
type: NounType.Location,
metadata: { name: `Location ${i + 1}` }
})
locations.push(id)
}
console.log(`✅ Added ${locations.length} location entities`)
// Add 2 concept entities
const concepts = []
for (let i = 0; i < 2; i++) {
const id = await brain.add({
data: `Concept ${i + 1}`,
type: NounType.Concept,
metadata: { name: `Concept ${i + 1}` }
})
concepts.push(id)
}
console.log(`✅ Added ${concepts.length} concept entities`)
console.log(`\n📊 Total: ${people.length + locations.length + concepts.length} entities`)
console.log('\n' + '='.repeat(60))
console.log('\n2⃣ Testing find() without type filter...\n')
const allResults = await brain.find({ limit: 100 })
console.log(`✅ brain.find({}) returned ${allResults.length} entities`)
// Check types in results
const typeCounts = allResults.reduce((acc, r) => {
acc[r.type || 'unknown'] = (acc[r.type || 'unknown'] || 0) + 1
return acc
}, {} as Record<string, number>)
console.log('\nTypes in results:')
for (const [type, count] of Object.entries(typeCounts)) {
console.log(` - ${type}: ${count}`)
}
console.log('\n' + '='.repeat(60))
console.log('\n3⃣ Testing find() WITH type filter (using enum)...\n')
// Test 1: Filter by NounType.Person (enum value)
console.log('Test 1: brain.find({ type: NounType.Person })')
const personResults = await brain.find({ type: NounType.Person, limit: 100 })
console.log(` Result: ${personResults.length} entities`)
console.log(` Expected: ${people.length} entities`)
if (personResults.length === people.length) {
console.log(' ✅ PASS')
} else {
console.log(' ❌ FAIL - Type filtering not working!')
}
// Test 2: Filter by NounType.Location
console.log('\nTest 2: brain.find({ type: NounType.Location })')
const locationResults = await brain.find({ type: NounType.Location, limit: 100 })
console.log(` Result: ${locationResults.length} entities`)
console.log(` Expected: ${locations.length} entities`)
if (locationResults.length === locations.length) {
console.log(' ✅ PASS')
} else {
console.log(' ❌ FAIL - Type filtering not working!')
}
// Test 3: Filter by NounType.Concept
console.log('\nTest 3: brain.find({ type: NounType.Concept })')
const conceptResults = await brain.find({ type: NounType.Concept, limit: 100 })
console.log(` Result: ${conceptResults.length} entities`)
console.log(` Expected: ${concepts.length} entities`)
if (conceptResults.length === concepts.length) {
console.log(' ✅ PASS')
} else {
console.log(' ❌ FAIL - Type filtering not working!')
}
console.log('\n' + '='.repeat(60))
console.log('\n4⃣ Testing find() WITH type filter (using string)...\n')
// Test 4: Filter by string 'person'
console.log('Test 4: brain.find({ type: "person" })')
const personResults2 = await brain.find({ type: 'person' as any, limit: 100 })
console.log(` Result: ${personResults2.length} entities`)
console.log(` Expected: ${people.length} entities`)
if (personResults2.length === people.length) {
console.log(' ✅ PASS')
} else {
console.log(' ❌ FAIL - String type filtering not working!')
}
console.log('\n' + '='.repeat(60))
console.log('\n5⃣ Checking entity storage (metadata.noun)...\n')
// Get a person entity and check its storage
const personEntity = await brain.get(people[0])
console.log('Person entity structure:')
console.log(' - id:', personEntity?.id)
console.log(' - type:', personEntity?.type)
console.log(' - metadata.noun:', (personEntity as any)?.metadata?.noun)
console.log(' - metadata.name:', personEntity?.metadata?.name)
console.log('\n' + '='.repeat(60))
console.log('\n📋 Summary\n')
const tests = [
{ name: 'Filter by NounType.Person', passed: personResults.length === people.length },
{ name: 'Filter by NounType.Location', passed: locationResults.length === locations.length },
{ name: 'Filter by NounType.Concept', passed: conceptResults.length === concepts.length },
{ name: 'Filter by string "person"', passed: personResults2.length === people.length }
]
const passedCount = tests.filter(t => t.passed).length
const totalCount = tests.length
console.log(`Tests passed: ${passedCount}/${totalCount}\n`)
for (const test of tests) {
console.log(`${test.passed ? '✅' : '❌'} ${test.name}`)
}
if (passedCount === totalCount) {
console.log('\n🎉 All tests passed! Type filtering works correctly.')
console.log('\n💡 The Workshop team might be experiencing a different issue.')
console.log(' Check: storage persistence, Brainy instance reuse, or data migration')
} else {
console.log('\n❌ Type filtering is BROKEN in Brainy!')
console.log('\n🐛 This is a bug that needs to be fixed.')
console.log(' Workshop team was right - it\'s not user error.')
}
console.log('\n' + '='.repeat(60) + '\n')
}
// Run the test
testTypeFiltering().catch(console.error)

View file

@ -0,0 +1,171 @@
/**
* Workshop VFS Diagnostic Test
*
* Tests to verify VFS import behavior and identify if VFS creates only wrappers or also graph entities
*/
import { describe, it, expect, beforeEach } from 'vitest'
import { Brainy, NounType } from '../../src/index.js'
describe('Workshop VFS Diagnostic', () => {
let brain: Brainy
beforeEach(async () => {
brain = new Brainy({
storage: { type: 'memory' }
})
await brain.init()
})
it('should verify VFS creates document wrappers AND allows entity filtering', async () => {
console.log('\n🔬 Workshop VFS Diagnostic Test\n')
console.log('='.repeat(70))
// Step 1: Add entities directly (control group)
console.log('\n1⃣ Adding entities directly (without VFS)...\n')
await brain.add({ data: 'Person 1', type: NounType.Person, metadata: { name: 'Person 1' } })
await brain.add({ data: 'Person 2', type: NounType.Person, metadata: { name: 'Person 2' } })
await brain.add({ data: 'Location 1', type: NounType.Location, metadata: { name: 'Location 1' } })
const beforeVfs = await brain.find({ limit: 100 })
console.log(` Total entities: ${beforeVfs.length}`)
const peopleBefore = await brain.find({ type: NounType.Person, limit: 100 })
console.log(` Person filter: ${peopleBefore.length} (expected: 2)`)
expect(peopleBefore.length).toBe(2)
console.log(' ✅ Type filtering works on direct entities\n')
// Step 2: Use VFS to create files
console.log('2⃣ Creating VFS files...\n')
const vfs = brain.vfs()
await vfs.init()
await vfs.mkdir('/test', { recursive: true })
// Create a VFS file with entity data
const personData = {
id: 'ent_person_test',
name: 'John Smith',
type: 'person',
metadata: { source: 'test' }
}
await vfs.writeFile('/test/john.json', Buffer.from(JSON.stringify(personData, null, 2)))
console.log(' Created VFS file: /test/john.json')
// Step 3: Check what entities exist now
console.log('\n3⃣ Analyzing entities after VFS...\n')
const afterVfs = await brain.find({ limit: 100 })
console.log(` Total entities: ${afterVfs.length}`)
// Count by type
const typeCounts: Record<string, number> = {}
for (const result of afterVfs) {
const type = result.type || 'unknown'
typeCounts[type] = (typeCounts[type] || 0) + 1
}
console.log('\n Entity type breakdown:')
for (const [type, count] of Object.entries(typeCounts)) {
console.log(` - ${type}: ${count}`)
}
// Count VFS wrappers vs regular entities
const vfsWrappers = afterVfs.filter(e => e.metadata?.vfsType === 'file')
const regularEntities = afterVfs.filter(e => !e.metadata?.vfsType)
console.log(`\n VFS wrappers: ${vfsWrappers.length}`)
console.log(` Regular entities: ${regularEntities.length}`)
// Step 4: Test type filtering after VFS
console.log('\n4⃣ Testing type filtering after VFS...\n')
const peopleAfter = await brain.find({ type: NounType.Person, limit: 100 })
console.log(` Person filter: ${peopleAfter.length} (expected: 2 - same as before)`)
const documents = await brain.find({ type: NounType.Document, limit: 100 })
console.log(` Document filter: ${documents.length} (expected: ${vfsWrappers.length})`)
// Step 5: Analyze VFS wrapper structure
console.log('\n5⃣ Analyzing VFS wrapper structure...\n')
const wrapper = vfsWrappers[0]
if (wrapper) {
console.log(' VFS Wrapper Entity:')
console.log(` - ID: ${wrapper.id}`)
console.log(` - Type: ${wrapper.type}`)
console.log(` - VFS Type: ${wrapper.metadata?.vfsType}`)
console.log(` - Path: ${wrapper.metadata?.path}`)
console.log(` - Has rawData: ${!!wrapper.metadata?.rawData}`)
if (wrapper.metadata?.rawData) {
const decoded = Buffer.from(wrapper.metadata.rawData, 'base64').toString()
const embedded = JSON.parse(decoded)
console.log(`\n Embedded Entity Data:`)
console.log(` - Name: ${embedded.name}`)
console.log(` - Type: ${embedded.type}`)
console.log(`\n 🔍 KEY FINDING:`)
console.log(` Wrapper type: "${wrapper.type}"`)
console.log(` Embedded type: "${embedded.type}"`)
console.log(` Filtering by type="${embedded.type}" searches wrapper type, not embedded!`)
}
}
// Step 6: Diagnosis
console.log('\n' + '='.repeat(70))
console.log('📋 DIAGNOSIS\n')
if (peopleAfter.length === peopleBefore.length) {
console.log('✅ VFS does NOT create duplicate graph entities')
console.log('✅ VFS only creates document wrappers')
console.log('✅ Type filtering works on original entities, ignores VFS wrappers')
console.log('\nThis means:')
console.log(' - VFS files are type="document" wrappers')
console.log(' - Original entities keep their types')
console.log(' - filter({ type: "person" }) returns original entities only')
} else {
console.log('❌ Unexpected behavior - VFS may have created additional entities')
}
console.log('\n' + '='.repeat(70) + '\n')
// Assertions
expect(peopleAfter.length).toBe(2) // Should still be 2, VFS doesn't create person entities
expect(documents.length).toBeGreaterThan(0) // VFS creates document wrappers
expect(vfsWrappers.length).toBeGreaterThan(0) // Should have VFS wrappers
})
it('should verify import creates BOTH VFS wrappers AND graph entities', async () => {
// This test would require creating a test Excel file and running import
// For now, we'll document the expected behavior based on code analysis
console.log('\n📚 Expected Import Behavior (from code analysis):\n')
console.log('When you run brain.import("file.xlsx", { vfsPath: "/imports" }):')
console.log('\n1. ImportCoordinator.execute() calls:')
console.log(' a) vfsGenerator.generate() - creates VFS file wrappers')
console.log(' - Each entity → JSON file in VFS')
console.log(' - Wrapper entity with type="document"')
console.log(' - Entity data stored in metadata.rawData (base64)')
console.log('')
console.log(' b) createGraphEntities() - creates graph entities')
console.log(' - Each entity → graph entity with proper type')
console.log(' - type="person", "location", "concept", etc.')
console.log(' - metadata.vfsPath points to VFS file')
console.log('')
console.log('2. Result: Database contains BOTH:')
console.log(' - VFS wrappers (type="document", vfsType="file")')
console.log(' - Graph entities (type="person", etc., vfsPath set)')
console.log('')
console.log('3. Type filtering:')
console.log(' - filter({ type: "person" }) → returns graph entities')
console.log(' - filter({ type: "document" }) → returns VFS wrappers')
console.log('')
console.log('If Workshop gets 0 results, likely causes:')
console.log(' ❌ Only VFS wrappers created (createEntities: false)')
console.log(' ❌ Import not completing before query')
console.log(' ❌ Querying different Brainy instance')
console.log('')
})
})