2025-09-26 15:45:13 -07:00
# VFS Troubleshooting Guide
## Common Issues and Solutions
### Issue: "VFSError: Not a directory: /"
**Symptoms:**
- `readdir('/')` throws "Not a directory" error
- Root directory exists but isn't recognized as directory
- Files can be written but not listed
**Root Cause:**
The root directory entity exists but doesn't have the proper metadata structure.
2026-01-27 15:38:21 -08:00
**Solution:**
This issue has been fixed. The VFS now:
2025-09-26 15:45:13 -07:00
1. Ensures root directory has `vfsType: 'directory'` metadata
2. Adds compatibility layer for entities with malformed metadata
3. Automatically repairs metadata on entity retrieval
**Manual Fix (if needed):**
```javascript
// Force re-initialization of root directory
2026-01-27 15:38:21 -08:00
await vfs.init() // Will repair root if needed
2025-09-26 15:45:13 -07:00
// Or manually update root entity
const rootId = vfs.rootEntityId
await brain.update({
2026-01-27 15:38:21 -08:00
id: rootId,
metadata: {
path: '/',
vfsType: 'directory',
// ... other metadata
}
2025-09-26 15:45:13 -07:00
})
```
---
### Issue: "readdir() returns empty array despite files existing"
**Symptoms:**
- Files are successfully written to VFS
- Files can be read individually
- `readdir()` returns `[]` for directories with files
**Root Cause:**
Contains relationships are missing between parent directories and files.
2026-01-27 15:38:21 -08:00
**Solution:**
2025-09-26 15:45:13 -07:00
This issue has been fixed. The VFS now:
1. Creates Contains relationships when writing new files
2. Ensures Contains relationships exist when updating files
3. Repairs missing relationships automatically
**Manual Fix (if needed):**
```javascript
// Repair missing Contains relationship
const parentId = await vfs.resolvePath('/directory')
const fileId = await vfs.resolvePath('/directory/file.txt')
await brain.relate({
2026-01-27 15:38:21 -08:00
from: parentId,
to: fileId,
type: VerbType.Contains
2025-09-26 15:45:13 -07:00
})
```
---
### Issue: "VFS not initialized" error
**Symptoms:**
- Any VFS operation throws "VFS not initialized"
- Operations fail even after creating VFS instance
**Root Cause:**
The VFS `init()` method wasn't called after getting the VFS instance.
**Solution:**
```javascript
// ✅ CORRECT
2026-01-27 15:38:21 -08:00
const vfs = brain.vfs() // Get instance
await vfs.init() // Initialize (REQUIRED!)
2025-09-26 15:45:13 -07:00
// ❌ WRONG
const vfs = brain.vfs()
// Missing: await vfs.init()
```
---
### Issue: Files disappear after process restart
**Symptoms:**
- Files exist during session
- All files gone after restart
- Fresh VFS each time
**Root Cause:**
Using in-memory storage instead of persistent storage.
**Solution:**
```javascript
// ✅ Use persistent storage
const brain = new Brainy({
2026-01-27 15:38:21 -08:00
storage: {
type: 'filesystem', // Persistent
path: './brainy-data'
}
2025-09-26 15:45:13 -07:00
})
// ❌ Don't use memory for production
const brain = new Brainy({
2026-01-27 15:38:21 -08:00
storage: { type: 'memory' } // Data lost on restart!
2025-09-26 15:45:13 -07:00
})
```
---
### Issue: Infinite recursion when listing directories
**Symptoms:**
- Directory appears as its own child
- Stack overflow errors
- UI freezes
**Root Cause:**
Using path-based filtering instead of graph relationships.
**Solution:**
```javascript
// ✅ CORRECT - Use graph relationships
const children = await vfs.getDirectChildren('/directory')
// ❌ WRONG - Path prefix matching causes recursion
const allNodes = await brain.find({})
const children = allNodes.filter(n =>
2026-01-27 15:38:21 -08:00
n.metadata.path.startsWith('/directory/')) // Directory matches itself!
2025-09-26 15:45:13 -07:00
```
---
### Issue: Can't find files by content
**Symptoms:**
- Semantic search returns no results
- Only exact filename matches work
**Root Cause:**
Files aren't being properly embedded or indexed.
**Solution:**
```javascript
// Ensure files have content for embedding
2026-01-27 15:38:21 -08:00
await vfs.writeFile('/doc.txt', 'Actual content here') // Not empty!
2025-09-26 15:45:13 -07:00
// Use semantic search correctly
const results = await vfs.search('machine learning', {
2026-01-27 15:38:21 -08:00
path: '/documents', // Search within path
limit: 10,
type: 'file'
2025-09-26 15:45:13 -07:00
})
```
---
## Debugging Tips
### 1. Check VFS Initialization
```javascript
console.log('VFS initialized:', vfs.initialized)
console.log('Root entity ID:', vfs.rootEntityId)
```
### 2. Verify Entity Metadata
```javascript
const entity = await vfs.getEntity('/path/to/file')
console.log('Entity metadata:', entity.metadata)
console.log('VFS type:', entity.metadata.vfsType)
```
### 3. Check Relationships
```javascript
const parentId = await vfs.resolvePath('/directory')
const relations = await brain.getRelations({
2026-01-27 15:38:21 -08:00
from: parentId,
type: VerbType.Contains
2025-09-26 15:45:13 -07:00
})
console.log('Child count:', relations.length)
```
### 4. Enable Debug Logging
```javascript
const brain = new Brainy({
2026-01-27 15:38:21 -08:00
storage: { type: 'filesystem' },
logger: {
level: 'debug',
enabled: true
}
2025-09-26 15:45:13 -07:00
})
```
---
## Performance Tips
### 1. Use Caching
```javascript
// Enable caching in VFS config
const vfs = brain.vfs({
2026-01-27 15:38:21 -08:00
cache: {
enabled: true,
ttl: 300000, // 5 minutes
maxSize: 1000
}
2025-09-26 15:45:13 -07:00
})
```
### 2. Batch Operations
```javascript
// Write multiple files efficiently
const files = [
2026-01-27 15:38:21 -08:00
{ path: '/file1.txt', content: 'content1' },
{ path: '/file2.txt', content: 'content2' }
2025-09-26 15:45:13 -07:00
]
await Promise.all(
2026-01-27 15:38:21 -08:00
files.map(f => vfs.writeFile(f.path, f.content))
2025-09-26 15:45:13 -07:00
)
```
### 3. Limit Directory Depth
```javascript
// Don't traverse too deep
const tree = await vfs.getTreeStructure('/', {
2026-01-27 15:38:21 -08:00
maxDepth: 3, // Limit recursion
includeHidden: false
2025-09-26 15:45:13 -07:00
})
```
---
## Getting Help
If you encounter issues not covered here:
1. Check the [VFS API Guide ](./VFS_API_GUIDE.md )
2. Review [VFS Examples ](./VFS_EXAMPLES_SCENARIOS.md )
3. Look at [test files ](../../tests/vfs/ ) for working examples
4. Report issues at [GitHub Issues ](https://github.com/soulcraftlabs/brainy/issues )
Remember: Most VFS issues are related to:
- Missing initialization (`await vfs.init()` )
- Using memory storage instead of filesystem
- Missing Contains relationships
- Incorrect path handling