feat: v5.1.0 - VFS auto-initialization and complete API documentation

BREAKING CHANGES:
- VFS API changed from brain.vfs() (method) to brain.vfs (property)
- VFS now auto-initializes during brain.init() - no separate vfs.init() needed

Features:
- VFS auto-initialization with property access pattern
- Complete TypeAware COW support verification (all 20 methods)
- Comprehensive API documentation (docs/api/README.md)
- All 7 storage adapters verified with COW support

Bug Fixes:
- CLI now properly initializes brain before VFS operations
- Fixed infinite recursion in VFS initialization
- All VFS CLI commands updated to modern API

Documentation:
- Created comprehensive, verified API reference
- Consolidated documentation structure (deleted redundant quick starts)
- Updated all VFS docs to v5.1.0 patterns
- Fixed all internal documentation links

Verification:
- Memory Storage: 23/24 tests (95.8%)
- FileSystem Storage: 9/9 tests (100%)
- VFS Auto-Init: 7/7 tests (100%)
- Zero fake code confirmed
This commit is contained in:
David Snelling 2025-11-02 10:58:52 -08:00
parent 5e16f9e5e8
commit d4c9f71345
20 changed files with 2306 additions and 1343 deletions

View file

@ -28,11 +28,7 @@ const brain = new Brainy({
}
})
await brain.init()
// ✅ CORRECT: Initialize VFS
const vfs = brain.vfs()
await vfs.init()
await brain.init() // VFS auto-initialized!
console.log('🎉 VFS ready!')
```
@ -50,9 +46,9 @@ const badItems = allNodes.filter(node => node.path.startsWith(dirPath))
**✅ CORRECT - Use tree-aware methods:**
```typescript
// ✅ Method 1: Get direct children (recommended for UI)
async function loadDirectoryContents(path: string) {
async function loadDirectoryContents(brain: Brainy, path: string) {
try {
const children = await vfs.getDirectChildren(path)
const children = await brain.vfs.getDirectChildren(path)
// Sort directories first, then files
return children.sort((a, b) => {
@ -67,8 +63,8 @@ async function loadDirectoryContents(path: string) {
}
// ✅ Method 2: Get complete tree structure (for full trees)
async function loadFullTree(path: string) {
const tree = await vfs.getTreeStructure(path, {
async function loadFullTree(brain: Brainy, path: string) {
const tree = await brain.vfs.getTreeStructure(path, {
maxDepth: 3, // Prevent deep recursion
includeHidden: false, // Skip hidden files
sort: 'name'
@ -77,8 +73,8 @@ async function loadFullTree(path: string) {
}
// ✅ Method 3: Get detailed path info
async function inspectPath(path: string) {
const info = await vfs.inspect(path)
async function inspectPath(brain: Brainy, path: string) {
const info = await brain.vfs.inspect(path)
return {
isDirectory: info.node.metadata.vfsType === 'directory',
children: info.children,
@ -92,8 +88,8 @@ async function inspectPath(path: string) {
```typescript
// ✅ Find files by content, not just filename
async function searchFiles(query: string, basePath: string = '/') {
const results = await vfs.search(query, {
async function searchFiles(brain: Brainy, query: string, basePath: string = '/') {
const results = await brain.vfs.search(query, {
path: basePath, // Limit search to specific directory
limit: 50, // Reasonable limit
type: 'file' // Only search files, not directories
@ -122,37 +118,34 @@ import React, { useState, useEffect } from 'react'
import { Brainy } from '@soulcraft/brainy'
export function FileExplorer() {
const [vfs, setVfs] = useState(null)
const [brain, setBrain] = useState(null)
const [currentPath, setCurrentPath] = useState('/')
const [items, setItems] = useState([])
const [loading, setLoading] = useState(true)
const [searchQuery, setSearchQuery] = useState('')
// Initialize VFS
// Initialize Brainy (VFS auto-initialized!)
useEffect(() => {
async function initVFS() {
const brain = new Brainy({
async function initBrainy() {
const brainInstance = new Brainy({
storage: { type: 'filesystem', path: './brainy-data' }
})
await brain.init()
await brainInstance.init() // VFS ready after this!
const vfsInstance = brain.vfs()
await vfsInstance.init()
setVfs(vfsInstance)
setBrain(brainInstance)
setLoading(false)
}
initVFS()
initBrainy()
}, [])
// Load directory contents
const loadDirectory = async (path: string) => {
if (!vfs) return
if (!brain) return
setLoading(true)
try {
// ✅ CORRECT: Use getDirectChildren to prevent recursion
const children = await vfs.getDirectChildren(path)
const children = await brain.vfs.getDirectChildren(path)
// Sort directories first
const sorted = children.sort((a, b) => {
@ -173,14 +166,14 @@ export function FileExplorer() {
// Search files
const handleSearch = async () => {
if (!vfs || !searchQuery.trim()) {
if (!brain || !searchQuery.trim()) {
loadDirectory(currentPath)
return
}
setLoading(true)
try {
const results = await vfs.search(searchQuery, {
const results = await brain.vfs.search(searchQuery, {
path: currentPath,
limit: 100
})
@ -194,13 +187,13 @@ export function FileExplorer() {
// Initial load
useEffect(() => {
if (vfs) {
if (brain) {
loadDirectory('/')
}
}, [vfs])
}, [brain])
if (loading && !vfs) {
return <div>Initializing VFS...</div>
if (loading && !brain) {
return <div>Initializing Brainy...</div>
}
return (
@ -301,16 +294,16 @@ npm install @soulcraft/brainy@latest # Update if needed
### "VFS not initialized" errors
```typescript
// Always await both init() calls
// v5.1.0+: Just await brain.init() - VFS is auto-initialized!
await brain.init()
const vfs = brain.vfs()
await vfs.init() // Don't forget this!
// VFS ready - use brain.vfs directly
await brain.vfs.writeFile('/test.txt', 'data')
```
### Slow directory loading
```typescript
// Add pagination for large directories
const children = await vfs.getDirectChildren(path, {
const children = await brain.vfs.getDirectChildren(path, {
limit: 100, // Load only first 100 items
offset: 0 // Start from beginning
})
@ -319,7 +312,7 @@ const children = await vfs.getDirectChildren(path, {
### Search not finding files
```typescript
// Make sure files are imported into VFS first
await vfs.importDirectory('./my-files', {
await brain.vfs.importDirectory('./my-files', {
recursive: true,
extractMetadata: true // Enable content understanding
})

View file

@ -1,69 +1,79 @@
# VFS Initialization Guide
# VFS Initialization Guide (v5.1.0+)
## Quick Start
The Brainy VFS requires proper initialization before use. Here's the correct pattern:
The Brainy VFS is automatically initialized during `brain.init()`. No separate initialization needed!
```javascript
import { Brainy } from '@soulcraft/brainy'
// Step 1: Create and initialize Brainy
// Create and initialize Brainy
const brain = new Brainy({
storage: { type: 'filesystem', path: './data' }
})
await brain.init() // VFS is auto-initialized here!
// Use VFS immediately - it's a property, not a method!
await brain.vfs.writeFile('/test.txt', 'Hello World')
const files = await brain.vfs.readdir('/')
```
## What Changed in v5.1.0?
### Before (v4.x and early v5.0.0):
```javascript
const brain = new Brainy(...)
await brain.init()
// Step 2: Get VFS instance (it's a METHOD, not a property!)
const vfs = brain.vfs() // ✅ Correct: method call with ()
// Step 3: Initialize VFS (THIS IS REQUIRED!)
await vfs.init() // Creates the root directory
// Now you can use VFS
await vfs.writeFile('/test.txt', 'Hello World')
const files = await vfs.readdir('/')
const vfs = brain.vfs() // ❌ Method call
await vfs.init() // ❌ Separate initialization
await vfs.writeFile(...)
```
## Common Mistakes
### ❌ Mistake 1: Accessing VFS as Property
### After (v5.1.0+):
```javascript
// WRONG - vfs is a method, not a property
const vfs = brain.vfs // Missing parentheses!
const brain = new Brainy(...)
await brain.init() // VFS auto-initialized!
await brain.vfs.writeFile(...) // ✅ Property access, just works!
```
### ❌ Mistake 2: Forgetting to Initialize VFS
### Key Changes:
1. **`vfs()``vfs`**: Method call becomes property access
2. **Auto-initialization**: VFS initialized during `brain.init()`
3. **Zero complexity**: No separate `vfs.init()` call needed
4. **Consistent pattern**: VFS treated like any other brain API
## Migration from v4.x/v5.0.0
### Old Pattern (DEPRECATED):
```javascript
const vfs = brain.vfs()
// Missing: await vfs.init()
await vfs.writeFile('/test.txt', 'data') // Error: VFS not initialized
await vfs.init()
await vfs.writeFile('/test.txt', 'data')
```
### ❌ Mistake 3: Not Waiting for Initialization
### New Pattern (v5.1.0+):
```javascript
const vfs = brain.vfs()
vfs.init() // Missing await!
await vfs.readdir('/') // Error: VFS not initialized (init still running)
// Just remove the () and init() call
await brain.vfs.writeFile('/test.txt', 'data')
```
## Why Initialization is Required
## Why Auto-Initialization?
The VFS `init()` method performs critical setup:
VFS stores files as entities and relationships in the same graph as everything else. There's no reason to treat it differently! Auto-initialization:
1. **Creates the root directory entity** in Brainy's graph database
2. **Initializes the PathResolver** for efficient path lookups
3. **Sets up caching layers** for performance
4. **Starts background tasks** for maintenance
5. **Configures storage adapters** based on your settings
Without initialization, the root directory (`/`) doesn't exist, which is why operations fail with "Not a directory: /" errors.
- ✅ **Simpler API**: One less step to remember
- ✅ **Fewer errors**: Can't forget to initialize
- ✅ **More intuitive**: Property access feels natural
- ✅ **Consistent**: Matches how other brain APIs work
## Complete Example
```javascript
import { Brainy } from '@soulcraft/brainy'
async function setupVFS() {
async function useVFS() {
// Initialize Brainy
const brain = new Brainy({
storage: {
@ -71,44 +81,22 @@ async function setupVFS() {
path: './brainy-data'
}
})
await brain.init()
await brain.init() // VFS ready after this!
// Get and initialize VFS
const vfs = brain.vfs()
await vfs.init()
// Use VFS immediately
await brain.vfs.writeFile('/readme.txt', 'Welcome to VFS!')
await brain.vfs.mkdir('/documents')
// Verify initialization
const rootExists = await vfs.exists('/')
console.log('Root directory exists:', rootExists) // true
const files = await brain.vfs.readdir('/')
console.log('Files in root:', files.map(f => f.name))
const stats = await vfs.stat('/')
console.log('Root is directory:', stats.isDirectory()) // true
// Now use VFS normally
await vfs.writeFile('/readme.txt', 'Welcome to VFS!')
await vfs.mkdir('/documents')
const files = await vfs.readdir('/')
console.log('Files in root:', files) // ['readme.txt', 'documents']
return vfs
const content = await brain.vfs.readFile('/readme.txt')
console.log('File content:', content.toString())
}
setupVFS().catch(console.error)
useVFS().catch(console.error)
```
## Error Messages
If you see this error:
```
VFS not initialized. You must call await vfs.init() after getting the VFS instance.
Example:
const vfs = brain.vfs() // Note: vfs() is a method, not a property
await vfs.init() // This creates the root directory
```
It means you forgot to initialize VFS. Follow the example in the error message.
## TypeScript Usage
```typescript
@ -116,87 +104,87 @@ import { Brainy, VirtualFileSystem } from '@soulcraft/brainy'
class FileManager {
private brain: Brainy
private vfs: VirtualFileSystem | null = null
async initialize(): Promise<void> {
// Initialize Brainy
this.brain = new Brainy({
storage: { type: 'filesystem' }
})
await this.brain.init()
// Initialize VFS
this.vfs = this.brain.vfs()
await this.vfs.init()
// VFS is ready! No separate initialization needed
}
async writeFile(path: string, content: string): Promise<void> {
if (!this.vfs) {
if (!this.brain) {
throw new Error('FileManager not initialized. Call initialize() first.')
}
await this.vfs.writeFile(path, content)
// Use VFS as property
await this.brain.vfs.writeFile(path, content)
}
async listFiles(path: string): Promise<string[]> {
const entries = await this.brain.vfs.readdir(path)
return entries.map(e => e.name)
}
}
```
## Auto-Initialization Pattern (Optional)
## Error Messages
If you want VFS to auto-initialize on first use:
If you see this error:
```
Brainy not initialized. Call init() first.
```
It means you tried to use VFS before calling `brain.init()`. Always initialize Brainy first:
```javascript
class AutoInitVFS {
constructor(config) {
this.brain = new Brainy(config)
this.vfs = null
this.initPromise = null
}
await brain.init() // Required!
await brain.vfs.writeFile(...) // Now this works
```
async ensureInit() {
if (!this.initPromise) {
this.initPromise = this._initialize()
}
await this.initPromise
}
## Fork Support (v5.0.0+)
async _initialize() {
await this.brain.init()
this.vfs = this.brain.vfs()
await this.vfs.init()
}
VFS works seamlessly with Brainy's Copy-on-Write (COW) fork feature:
// Wrap all VFS methods
async writeFile(path, data) {
await this.ensureInit()
return this.vfs.writeFile(path, data)
}
```javascript
const brain = new Brainy({ storage: { type: 'memory' } })
await brain.init()
async readdir(path) {
await this.ensureInit()
return this.vfs.readdir(path)
}
}
// Create files in parent
await brain.vfs.writeFile('/config.json', '{"version": 1}')
// Usage - no explicit init needed
const vfs = new AutoInitVFS({ storage: { type: 'memory' } })
await vfs.writeFile('/test.txt', 'Auto-init works!')
// Fork inherits parent's files
const fork = await brain.fork('experiment')
const files = await fork.vfs.readdir('/') // Sees parent's config.json!
// Fork modifications are isolated
await fork.vfs.writeFile('/test.txt', 'Fork only')
await brain.vfs.readdir('/') // Parent doesn't see test.txt
```
## FAQ
### Q: Why doesn't VFS auto-initialize?
A: Explicit initialization gives you control over when the root directory is created and when background tasks start. This prevents unexpected side effects and makes the initialization cost visible.
### Q: Do I need to call `vfs.init()` anymore?
**A:** No! VFS is automatically initialized during `brain.init()` in v5.1.0+.
### Q: Can I reinitialize VFS?
A: No, VFS can only be initialized once per instance. If you need to reset, create a new Brainy instance.
### Q: Why did the API change from `vfs()` to `vfs`?
**A:** VFS uses the same entity/relationship graph as everything else. There's no reason to treat it differently from other brain APIs.
### Q: What happens if Brainy isn't initialized?
A: VFS initialization will fail. Always initialize Brainy first with `await brain.init()`.
### Q: Will my old code break?
**A:** If you're using `brain.vfs()` or `await vfs.init()`, you'll need to update to the new pattern. The migration is simple - just remove the `()` and `init()` calls.
### Q: Is the initialization pattern the same for all storage types?
A: Yes, whether using memory, filesystem, S3, or R2 storage, the initialization pattern is identical.
### Q: Can I still configure VFS?
**A:** Yes, VFS configuration is passed through `brain.init()` config. The VFS-specific options are applied during auto-initialization.
### Q: Does this work with all storage adapters?
**A:** Yes! VFS auto-initialization works with all storage adapters: Memory, FileSystem, OPFS, S3, R2, Azure Blob, and Google Cloud Storage.
### Q: What if I need multiple VFS instances?
**A:** Each Brainy instance has its own VFS. Create multiple Brainy instances if you need multiple VFS instances.
## Related Documentation
- [VFS Quick Start](./QUICK_START.md) - 5-minute setup guide
- [VFS API Guide](./VFS_API_GUIDE.md) - Complete API reference
- [Common Patterns](./COMMON_PATTERNS.md) - Best practices and patterns
- [Common Patterns](./COMMON_PATTERNS.md) - Best practices and patterns
- [Instant Fork](../features/instant-fork.md) - VFS + Copy-on-Write