# Brainy Virtual Filesystem (VFS) 🗂️🧠

> Transform your filesystem into an intelligent knowledge graph where every file is a living entity with semantic understanding, relationships, and AI-powered organization.

## 📚 Complete VFS Documentation

**Essential guides to get started:**
- **[VFS Core](VFS_CORE.md)** - Complete filesystem architecture and API
- **[Semantic VFS](SEMANTIC_VFS.md)** - Multi-dimensional file access (6 semantic dimensions)
- **[Neural Extraction](NEURAL_EXTRACTION.md)** - AI-powered concept and entity extraction
- **[Examples & Scenarios](VFS_EXAMPLES_SCENARIOS.md)** - Real-world use cases and code
- **[VFS API Guide](VFS_API_GUIDE.md)** - Complete API reference

## What is Brainy VFS?

Brainy VFS is a revolutionary virtual filesystem that runs on top of Brainy's neural database. Unlike traditional filesystems that treat files as isolated bytes on disk, Brainy VFS treats every file as an intelligent entity that:

- **Understands its content** through AI-powered semantic analysis
- **Maintains relationships** with other files, concepts, and entities
- **Self-organizes** based on meaning and usage patterns
- **Enables semantic search** beyond simple filename matching
- **Connects to everything** - todos, concepts, people, projects, and more

## Quick Start

```javascript
import { VirtualFileSystem } from '@soulcraft/brainy/vfs'

// Initialize the VFS
const vfs = new VirtualFileSystem({
  root: '/my-brain',
  intelligent: true  // Enable AI features
})

await vfs.init()

// Write a file - it automatically becomes intelligent
await vfs.writeFile('/projects/my-app/index.js',
  'console.log("Hello, World!")')

// Find similar files using semantic search
const similar = await vfs.findSimilar('/projects/my-app/index.js')

// Search with natural language
const results = await vfs.search('files about authentication')

// Connect files to other entities
await vfs.addRelationship('/docs/spec.md', '/projects/my-app/', 'implements')
```

## Core Features

### 🆕 Tree Operations (Prevents Recursion Issues)

**NEW: Safe tree operations for building file explorers:**
- **`getDirectChildren(path)`** - Returns only immediate children, never the parent
- **`getTreeStructure(path, options)`** - Builds complete tree with recursion protection
- **`getDescendants(path, options)`** - Gets all descendants efficiently
- **`inspect(path)`** - Comprehensive info with parent, children, and stats

See [Building File Explorers Guide](building-file-explorers.md) for complete documentation on avoiding common recursion pitfalls.

## Core Features

### 📁 Full Filesystem API

All the operations you expect from a filesystem:

```javascript
// Basic file operations
await vfs.writeFile('/notes/idea.md', 'My brilliant idea')
const content = await vfs.readFile('/notes/idea.md')
await vfs.unlink('/temp/old.txt')

// Directory operations
await vfs.mkdir('/projects/new-project')
const files = await vfs.readdir('/projects')
await vfs.rmdir('/temp')

// File metadata
const stats = await vfs.stat('/photos/sunset.jpg')
await vfs.chmod('/scripts/deploy.sh', 0o755)

// Moving and copying
await vfs.rename('/draft.md', '/published.md')
await vfs.copy('/template.html', '/new-page.html')
```

### 🧠 Semantic Intelligence

Every file has a neural understanding:

```javascript
// Find files by meaning, not just name
const docs = await vfs.search('technical documentation for API endpoints')

// Find similar files
const similar = await vfs.findSimilar('/code/auth.js', {
  limit: 5,
  threshold: 0.8  // 80% similarity
})

// Get related files through the knowledge graph
const related = await vfs.getRelated('/proposal.pdf', {
  depth: 2  // Include relationships of relationships
})

// Auto-organization suggestions
const suggestions = await vfs.suggestOrganization([
  '/downloads/doc1.pdf',
  '/downloads/image.jpg',
  '/downloads/code.py'
])
// Returns: suggested folders and categorization
```

### 🔗 Rich Relationships

Files aren't isolated - they're connected:

```javascript
// Connect files with semantic relationships
await vfs.addRelationship('/spec.md', '/code/impl.js', 'implements')
await vfs.addRelationship('/test.js', '/code/impl.js', 'tests')
await vfs.addRelationship('/paper.pdf', '/notes/summary.md', 'summarizes')

// Query relationships
const connections = await vfs.getConnections('/code/impl.js')
// Returns: [{from: '/spec.md', type: 'implements'}, {from: '/test.js', type: 'tests'}]

// Traverse the graph
const implementations = await vfs.search('', {
  connected: {
    to: '/spec.md',
    via: 'implements'
  }
})
```

### 📝 Extended Metadata

Store anything alongside your files:

```javascript
// Add todos to files
await vfs.setTodos('/projects/app/index.js', [
  { task: 'Add error handling', priority: 'high', due: '2024-01-20' },
  { task: 'Optimize performance', priority: 'medium' }
])

// Set custom attributes
await vfs.setxattr('/report.pdf', 'project', 'Q4-Planning')
await vfs.setxattr('/photo.jpg', 'location', 'Paris, France')
await vfs.setxattr('/video.mp4', 'tags', ['tutorial', 'react', 'hooks'])

// Query by metadata
const urgent = await vfs.search('', {
  where: { 'todos.priority': 'high' }
})

const parisPhotos = await vfs.search('', {
  where: { location: 'Paris, France' }
})
```

### 🎯 Smart Collections

Virtual directories based on queries:

```javascript
// Create a smart folder that auto-updates
await vfs.createVirtualDirectory('/smart/recent-docs', {
  query: 'type:document modified:last-7-days'
})

// Create a collection based on similarity
await vfs.createVirtualDirectory('/smart/like-this', {
  similar: '/examples/good-code.js',
  threshold: 0.7
})

// Tag-based collections
await vfs.createVirtualDirectory('/smart/important', {
  where: { tags: 'important' }
})
```

## Real-World Examples

### 📚 Knowledge Management

```javascript
// Store a research paper with automatic analysis
await vfs.writeFile('/research/quantum-computing.pdf', pdfBuffer, {
  metadata: {
    authors: ['Dr. Alice Smith', 'Dr. Bob Jones'],
    year: 2024,
    topics: ['quantum', 'computing', 'algorithms'],
    citations: 42
  }
})

// Find all papers on similar topics
const related = await vfs.search('quantum algorithms', {
  type: 'document',
  where: { year: { $gte: 2020 } }
})

// Find papers that cite this one
const citations = await vfs.getConnections('/research/quantum-computing.pdf', {
  type: 'cites',
  direction: 'incoming'
})
```

### 💻 Code Intelligence

```javascript
// Write code that understands itself
await vfs.writeFile('/src/utils/auth.js', authCode)

// Automatically detects:
// - Programming language
// - Imported dependencies
// - Exported functions
// - Design patterns used

// Find all files that import this module
const importers = await vfs.search('', {
  where: { dependencies: 'utils/auth.js' }
})

// Find test files for this code
const tests = await vfs.getRelated('/src/utils/auth.js', {
  type: 'tests'
})

// Find similar implementations
const similar = await vfs.findSimilar('/src/utils/auth.js')
```

### 🎨 Digital Asset Management

```javascript
// Store media with rich metadata
await vfs.writeFile('/photos/sunset.jpg', imageBuffer, {
  metadata: {
    camera: 'Canon R5',
    location: { lat: 37.7749, lng: -122.4194 },
    tags: ['sunset', 'golden-gate', 'landscape'],
    album: 'San Francisco 2024'
  }
})

// Find similar images
const similar = await vfs.findSimilar('/photos/sunset.jpg')

// Find photos by location
const nearby = await vfs.search('', {
  type: 'image',
  where: {
    'location.lat': { $between: [37.7, 37.8] },
    'location.lng': { $between: [-122.5, -122.3] }
  }
})

// Smart albums
await vfs.createVirtualDirectory('/albums/best-sunsets', {
  query: 'sunset',
  type: 'image',
  where: { rating: { $gte: 4 } }
})
```

### 📋 Project Management

```javascript
// Connect everything in a project
const projectPath = '/projects/new-website'

// Add project files
await vfs.writeFile(`${projectPath}/README.md`, readmeContent)
await vfs.writeFile(`${projectPath}/src/index.js`, jsCode)
await vfs.writeFile(`${projectPath}/design.fig`, designFile)

// Add project metadata
await vfs.setxattr(projectPath, 'team', ['Alice', 'Bob', 'Charlie'])
await vfs.setxattr(projectPath, 'deadline', '2024-03-01')
await vfs.setxattr(projectPath, 'status', 'in-progress')

// Add todos to specific files
await vfs.setTodos(`${projectPath}/src/index.js`, [
  { task: 'Implement user authentication', assignee: 'Alice' },
  { task: 'Add error handling', assignee: 'Bob' }
])

// Find all files with pending todos
const pending = await vfs.search('', {
  where: {
    path: { $startsWith: projectPath },
    'todos.status': 'pending'
  }
})

// Find projects nearing deadline
const urgent = await vfs.search('', {
  where: {
    type: 'directory',
    'deadline': { $lte: '2024-02-01' },
    'status': 'in-progress'
  }
})
```

## Advanced Features

### 🔄 Version History

```javascript
// Enable versioning for a file
await vfs.enableVersioning('/important/contract.pdf')

// Write updates - automatically creates versions
await vfs.writeFile('/important/contract.pdf', newVersion)

// Get version history
const versions = await vfs.getVersions('/important/contract.pdf')
// Returns: [{version: 1, date: ..., size: ...}, {version: 2, ...}]

// Restore a previous version
await vfs.restoreVersion('/important/contract.pdf', 1)

// Compare versions
const diff = await vfs.diffVersions('/important/contract.pdf', 1, 2)
```

### 🌐 Distributed Filesystem

```javascript
// Mount remote Brainy instances
await vfs.mount('/remote/server2', {
  host: 'brainy.server2.com',
  credentials: { ... }
})

// Federated search across all mounted systems
const results = await vfs.search('project documentation', {
  distributed: true
})

// Sync directories across instances
await vfs.sync('/projects', '/remote/server2/backup/projects')
```

### 🤖 AI-Powered Automation

```javascript
// Auto-organize downloads folder
await vfs.autoOrganize('/downloads', {
  rules: [
    { pattern: '*.pdf', destination: '/documents' },
    { pattern: '*.{jpg,png}', destination: '/images' },
    { semantic: 'code files', destination: '/code' }
  ]
})

// Smart deduplication
const duplicates = await vfs.detectDuplicates('/photos')
await vfs.deduplicateFiles(duplicates, {
  strategy: 'keep-highest-quality'
})

// Content-aware compression
await vfs.optimizeStorage('/archives', {
  compress: true,
  deduplicate: true,
  indexContent: true
})
```

### 🔐 Security & Permissions

```javascript
// Set access control
await vfs.setACL('/private', {
  owner: 'user123',
  permissions: {
    owner: 'rwx',
    group: 'r-x',
    others: '---'
  }
})

// Encryption at rest
await vfs.encrypt('/sensitive', {
  algorithm: 'AES-256',
  key: encryptionKey
})

// Audit trail
const audit = await vfs.getAuditLog('/financial/reports')
// Returns: who accessed what and when
```

## Integration Examples

### Node.js fs Compatibility

```javascript
// Drop-in replacement for fs module
import { promises as fs } from '@soulcraft/brainy/vfs/fs'

// Works with existing code!
const data = await fs.readFile('/config.json', 'utf8')
const config = JSON.parse(data)

await fs.writeFile('/output.txt', 'Hello VFS!')
const stats = await fs.stat('/output.txt')
```

### Express.js Static Files

```javascript
import express from 'express'
import { createStaticMiddleware } from '@soulcraft/brainy/vfs/express'

const app = express()

// Serve files from VFS
app.use('/static', createStaticMiddleware('/public', {
  intelligentCaching: true,  // Cache based on access patterns
  autoCompress: true          // Compress on the fly
}))
```

### VSCode Extension

```javascript
// Open VFS in VSCode
import { workspace } from 'vscode'
import { VFSProvider } from '@soulcraft/brainy/vfs/vscode'

// Register VFS as a filesystem provider
workspace.registerFileSystemProvider('brainy', new VFSProvider(), {
  isCaseSensitive: true,
  isReadonly: false
})

// Now you can open: brainy:///projects/my-app
```

## Performance

Brainy VFS is designed for speed and scale:

- **Sub-10ms latency** for basic operations
- **Intelligent caching** reduces repeated reads to <1ms
- **Vector search** returns results in <100ms for millions of files
- **Streaming support** for files of any size
- **Distributed sharding** for billions of files

## Triple Intelligence Power 🧠⚡

Brainy VFS fully leverages Brainy's revolutionary Triple Intelligence system:

- **📊 Vector Intelligence**: Semantic understanding of file content
- **🗃️ Field Intelligence**: Rich metadata filtering and queries
- **🕸️ Graph Intelligence**: Relationship-based navigation and traversal
- **🔀 Adaptive Fusion**: Automatically combines all three for optimal results

**[Learn how VFS exploits Triple Intelligence →](./TRIPLE_INTELLIGENCE.md)**

## Why Brainy VFS?

| Traditional Filesystem | Brainy VFS |
|------------------------|------------|
| Files are isolated bytes | Files are connected knowledge |
| Rigid folder hierarchy | Fluid, semantic organization |
| String-based search | AI-powered semantic search with Triple Intelligence |
| No content understanding | Deep content comprehension via vectors |
| Manual organization | Self-organizing with intelligent fusion |
| No relationships | Rich knowledge graph with traversal |
| Static metadata | Dynamic, queryable metadata with field intelligence |
| Single server | Distributed & federated |

## Installation

```bash
npm install @soulcraft/brainy
```

## Requirements

- Node.js 18+ (for server/desktop)
- Modern browser (for web apps)
- Brainy 3.0+

## API Reference

See the [full API documentation](./API.md) for detailed method signatures and options.

## Examples

Check out the [examples directory](../../examples/vfs) for:
- Building a file explorer
- Creating a note-taking app
- Implementing a photo organizer
- Building a code intelligence system

## Architecture & Implementation

### Production-Ready Design

The VFS is built with production scalability in mind:

#### **Path Resolution System**
- **4-Layer Cache Hierarchy**: L1 Hot Paths (<1ms) → L2 Path Cache (<5ms) → L3 Parent Cache (<10ms) → L4 Graph Traversal (<50ms)
- **Intelligent Cache Eviction**: LRU with usage tracking and TTL
- **Path Compression**: Frequently accessed deep paths get shortcut edges

#### **Storage Strategy**
```typescript
// Adaptive storage based on file size
< 100KB:  Inline storage (entity.data)
< 10MB:   External reference (S3/R2 key)
> 10MB:   Chunked storage (parallel chunks)
```

#### **Performance Metrics**
- **Path Resolution**: <1ms for cached, <50ms for cold paths
- **File Operations**: 100-1000 ops/sec depending on size
- **Directory Listing**: 200K entries/sec with pagination
- **Search**: <100ms across millions of files
- **Concurrent Access**: Lock-free reads, optimistic writes

### Scaling to Millions

#### **How It Handles Scale**

1. **Hierarchical Caching**
   - 100K+ path cache entries
   - Parent-child relationship caching
   - Hot path detection and optimization

2. **Distributed Architecture**
   - Sharding by path prefix
   - Read replicas for hot directories
   - CDN integration for static files

3. **Intelligent Indexing**
   - Compound indexes on (parent, name)
   - Vector indexes for semantic search
   - Graph indexes for relationships

4. **Streaming Everything**
   - Large files never fully in memory
   - Progressive loading
   - Chunked transfers

### Real Production Scenarios

#### **1. CI/CD Pipeline Storage**

Store build artifacts with automatic relationships:

```javascript
// Store build output with metadata
await vfs.writeFile('/builds/v1.2.3/app.js', buildOutput, {
  metadata: {
    commit: 'abc123',
    branch: 'main',
    timestamp: Date.now(),
    tests: 'passing',
    coverage: 0.92
  }
})

// Find all builds for a commit
const builds = await vfs.search('', {
  where: { commit: 'abc123' }
})

// Get latest passing build
const latest = await vfs.search('', {
  where: {
    branch: 'main',
    tests: 'passing'
  },
  sort: 'modified',
  order: 'desc',
  limit: 1
})
```

#### **2. Multi-Tenant SaaS Platform**

Isolate customer data with semantic understanding:

```javascript
// Each tenant gets their own root
const tenantVfs = new VirtualFileSystem({
  root: `/tenants/${tenantId}`,
  service: tenantId  // Isolate at Brainy level too
})

// Tenant uploads document
await tenantVfs.writeFile('/documents/contract.pdf', pdfBuffer)

// Cross-tenant analytics (admin only)
const adminVfs = new VirtualFileSystem({ root: '/tenants' })
const stats = await adminVfs.search('contract', {
  recursive: true,
  aggregations: {
    byTenant: { field: 'service' },
    byType: { field: 'mimeType' }
  }
})
```

#### **3. Machine Learning Pipeline**

Connect datasets, models, and results:

```javascript
// Store training data
await vfs.writeFile('/datasets/train.csv', csvData, {
  metadata: {
    samples: 100000,
    features: 50,
    labels: 10
  }
})

// Store trained model
await vfs.writeFile('/models/v1/model.pkl', modelBuffer, {
  metadata: {
    algorithm: 'random-forest',
    accuracy: 0.95,
    trainedOn: '/datasets/train.csv',
    hyperparameters: { trees: 100, depth: 10 }
  }
})

// Connect model to its training data
await vfs.addRelationship(
  '/models/v1/model.pkl',
  '/datasets/train.csv',
  'trained-on'
)

// Find best model for a dataset
const models = await vfs.search('', {
  connected: {
    to: '/datasets/train.csv',
    via: 'trained-on'
  },
  sort: 'accuracy',
  order: 'desc'
})
```

#### **4. Content Management System**

Intelligent content organization:

```javascript
// Auto-organize uploads
vfs.on('file:added', async (path) => {
  if (path.startsWith('/uploads/')) {
    const file = await vfs.getEntity(path)

    // Auto-categorize by AI
    const category = await detectCategory(file)
    const newPath = `/content/${category}/${file.metadata.name}`

    await vfs.move(path, newPath)

    // Auto-tag
    const tags = await extractTags(file)
    await vfs.setxattr(newPath, 'tags', tags)

    // Find related content
    const related = await vfs.findSimilar(newPath, {
      limit: 5,
      threshold: 0.8
    })

    // Create relationships
    for (const rel of related) {
      await vfs.addRelationship(newPath, rel.path, 'related-to')
    }
  }
})
```

#### **5. Distributed Team Workspace**

Collaborative file management:

```javascript
// Track file ownership and access
await vfs.writeFile('/projects/alpha/spec.md', content, {
  metadata: {
    owner: userId,
    team: 'engineering',
    permissions: {
      [userId]: 'rw',
      'team:engineering': 'r',
      'others': '-'
    }
  }
})

// Add collaborative features
await vfs.addTodo('/projects/alpha/spec.md', {
  task: 'Review security section',
  assignee: 'alice@company.com',
  due: '2024-02-01',
  priority: 'high'
})

// Track who's working on what
await vfs.setxattr('/projects/alpha/spec.md', 'locks', {
  section3: {
    user: 'bob@company.com',
    since: Date.now()
  }
})

// Find all files assigned to a user
const assigned = await vfs.search('', {
  where: {
    'todos.assignee': 'alice@company.com',
    'todos.status': 'pending'
  }
})
```

### Monitoring & Operations

#### **Production Metrics**

```javascript
// Get VFS statistics
const stats = vfs.getStatistics()
console.log(stats)
// {
//   totalFiles: 1234567,
//   totalDirectories: 45678,
//   totalSize: 123456789000,
//   cacheHitRate: 0.95,
//   avgResponseTime: 12,
//   activeConnections: 234
// }

// Monitor hot paths
const hotPaths = vfs.getHotPaths()
// Paths accessed >100 times/minute

// Check health
const health = await vfs.healthCheck()
// {
//   status: 'healthy',
//   latency: { p50: 10, p99: 100 },
//   errors: { rate: 0.001 }
// }
```

#### **Backup & Recovery**

```javascript
// Incremental backup
const changes = await vfs.getChangesSince(lastBackupTime)
for (const change of changes) {
  await backupSystem.store(change)
}

// Point-in-time recovery
await vfs.restoreToTime(timestamp)

// Verify integrity
const corrupted = await vfs.verifyIntegrity()
if (corrupted.length > 0) {
  await vfs.repair(corrupted)
}
```

### Deployment Options

#### **Standalone Mode**
```javascript
const vfs = new VirtualFileSystem()
await vfs.init()  // Uses in-memory storage
```

#### **Local Persistence**
```javascript
const vfs = new VirtualFileSystem()
await vfs.init({
  storage: 'filesystem',
  dataDir: '/var/lib/brainy-vfs'
})
```

#### **Cloud Native**
```javascript
const vfs = new VirtualFileSystem()
await vfs.init({
  storage: 's3',
  bucket: 'my-vfs-data',
  region: 'us-west-2'
})
```

#### **Distributed Cluster**
```javascript
const vfs = new VirtualFileSystem()
await vfs.init({
  distributed: true,
  nodes: [
    'vfs1.internal:8080',
    'vfs2.internal:8080',
    'vfs3.internal:8080'
  ],
  replication: 3,
  consistency: 'eventual'
})
```

## Coming Soon

- **Version 1.1**: Full streaming support, FUSE driver
- **Version 1.2**: Distributed transactions, global replication
- **Version 1.3**: Time-travel queries, branching
- **Version 2.0**: Quantum-resistant encryption, neural interfaces

## Contributing

We welcome contributions! See [CONTRIBUTING.md](../../CONTRIBUTING.md)

## License

MIT - Part of the Brainy project

---

*Transform your filesystem into a brain. Production-ready, infinitely scalable, impossibly intelligent.* 🧠🚀