feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
/**
|
|
|
|
|
* VFS Structure Generator
|
|
|
|
|
*
|
|
|
|
|
* Organizes imported entities into structured VFS directories
|
|
|
|
|
* - Type-based grouping (Place/, Character/, Concept/)
|
|
|
|
|
* - Metadata files (_metadata.json, _relationships.json)
|
|
|
|
|
* - Source file preservation
|
|
|
|
|
*
|
|
|
|
|
* NO MOCKS - Production-ready implementation
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { Brainy } from '../brainy.js'
|
|
|
|
|
import { VirtualFileSystem } from '../vfs/VirtualFileSystem.js'
|
|
|
|
|
import { NounType, VerbType } from '../types/graphTypes.js'
|
|
|
|
|
import type { SmartExcelResult } from './SmartExcelImporter.js'
|
fix: resolve HNSW concurrency race condition across all storage adapters
Fixes critical P0 bug causing data corruption during bulk imports with 50+ concurrent operations. The non-atomic read-modify-write pattern in saveHNSWData() combined with fire-and-forget neighbor updates was causing 16-32 concurrent writes per entity, resulting in lost HNSW connections and corrupted graph structure.
**Root Cause:**
- saveHNSWData() used non-atomic read-modify-write
- HNSW neighbor updates fired without await (16-32 concurrent writes/entity)
- Popular nodes became hotspots (100 concurrent imports = 3,400 concurrent saveHNSWData calls)
- Result: Lost neighbor connections, 0 search results
**Atomic Write Strategies by Adapter:**
FileSystemStorage:
- Atomic rename with temp files
- Write to {file}.tmp.{timestamp}.{random}
- POSIX-guaranteed atomic rename(temp, final)
GCSStorage:
- Optimistic locking with generation numbers
- preconditionOpts: { ifGenerationMatch }
- 5 retries with exponential backoff (50ms→800ms)
S3/R2/AzureStorage:
- ETag-based optimistic locking
- IfMatch/conditions preconditions
- 5 retries with exponential backoff
MemoryStorage + OPFSStorage:
- Mutex locks per entity path
- Serializes async operations even in single-threaded environments
HNSW Index:
- Changed fire-and-forget .catch() to await
- Serializes 16-32 neighbor updates per entity
- Trade-off: 20-30% slower bulk import vs 100% data integrity
**Sharding Compatibility:**
- ✅ Works with deterministic UUID sharding (256 shards, always on)
- ✅ Works with distributed multi-node sharding (optional)
- ✅ All atomic strategies work in both single-node and distributed deployments
**Index Impact:**
- Only HNSW index modified (saveHNSWData, saveHNSWSystem)
- Other 4 indexes unaffected (Metadata, Graph Adjacency, Deleted Items, Entity ID Mapper)
- No regression risk - isolated code paths
**Testing:**
- 8/8 unit tests passing (real concurrent operations, no mocks)
- Tests verify data integrity after 20 concurrent updates
- Tests verify temp file cleanup and mutex serialization
**Files Modified:**
- All 8 storage adapters (FileSystem, GCS, S3, R2, Azure, Memory, OPFS)
- HNSW Index (neighbor update serialization)
- New test: tests/unit/storage/hnswConcurrency.test.ts (8 passing tests)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 15:24:20 -07:00
|
|
|
import type { TrackingContext } from '../import/ImportCoordinator.js'
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
|
|
|
|
|
export interface VFSStructureOptions {
|
|
|
|
|
/** Root path in VFS for import */
|
|
|
|
|
rootPath: string
|
|
|
|
|
|
|
|
|
|
/** Grouping strategy */
|
|
|
|
|
groupBy: 'type' | 'sheet' | 'flat' | 'custom'
|
|
|
|
|
|
|
|
|
|
/** Custom grouping function */
|
|
|
|
|
customGrouping?: (entity: any) => string
|
|
|
|
|
|
|
|
|
|
/** Preserve source file */
|
|
|
|
|
preserveSource?: boolean
|
|
|
|
|
|
|
|
|
|
/** Source file buffer (if preserving) */
|
|
|
|
|
sourceBuffer?: Buffer
|
|
|
|
|
|
|
|
|
|
/** Source filename */
|
|
|
|
|
sourceFilename?: string
|
|
|
|
|
|
|
|
|
|
/** Create relationship file */
|
|
|
|
|
createRelationshipFile?: boolean
|
|
|
|
|
|
|
|
|
|
/** Create metadata file */
|
|
|
|
|
createMetadataFile?: boolean
|
fix: resolve HNSW concurrency race condition across all storage adapters
Fixes critical P0 bug causing data corruption during bulk imports with 50+ concurrent operations. The non-atomic read-modify-write pattern in saveHNSWData() combined with fire-and-forget neighbor updates was causing 16-32 concurrent writes per entity, resulting in lost HNSW connections and corrupted graph structure.
**Root Cause:**
- saveHNSWData() used non-atomic read-modify-write
- HNSW neighbor updates fired without await (16-32 concurrent writes/entity)
- Popular nodes became hotspots (100 concurrent imports = 3,400 concurrent saveHNSWData calls)
- Result: Lost neighbor connections, 0 search results
**Atomic Write Strategies by Adapter:**
FileSystemStorage:
- Atomic rename with temp files
- Write to {file}.tmp.{timestamp}.{random}
- POSIX-guaranteed atomic rename(temp, final)
GCSStorage:
- Optimistic locking with generation numbers
- preconditionOpts: { ifGenerationMatch }
- 5 retries with exponential backoff (50ms→800ms)
S3/R2/AzureStorage:
- ETag-based optimistic locking
- IfMatch/conditions preconditions
- 5 retries with exponential backoff
MemoryStorage + OPFSStorage:
- Mutex locks per entity path
- Serializes async operations even in single-threaded environments
HNSW Index:
- Changed fire-and-forget .catch() to await
- Serializes 16-32 neighbor updates per entity
- Trade-off: 20-30% slower bulk import vs 100% data integrity
**Sharding Compatibility:**
- ✅ Works with deterministic UUID sharding (256 shards, always on)
- ✅ Works with distributed multi-node sharding (optional)
- ✅ All atomic strategies work in both single-node and distributed deployments
**Index Impact:**
- Only HNSW index modified (saveHNSWData, saveHNSWSystem)
- Other 4 indexes unaffected (Metadata, Graph Adjacency, Deleted Items, Entity ID Mapper)
- No regression risk - isolated code paths
**Testing:**
- 8/8 unit tests passing (real concurrent operations, no mocks)
- Tests verify data integrity after 20 concurrent updates
- Tests verify temp file cleanup and mutex serialization
**Files Modified:**
- All 8 storage adapters (FileSystem, GCS, S3, R2, Azure, Memory, OPFS)
- HNSW Index (neighbor update serialization)
- New test: tests/unit/storage/hnswConcurrency.test.ts (8 passing tests)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 15:24:20 -07:00
|
|
|
|
|
|
|
|
/** Import tracking context (v4.10.0) */
|
|
|
|
|
trackingContext?: TrackingContext
|
2025-10-30 13:19:08 -07:00
|
|
|
|
|
|
|
|
/** Progress callback (v4.11.1) - Reports VFS creation progress */
|
|
|
|
|
onProgress?: (progress: {
|
|
|
|
|
stage: 'directories' | 'entities' | 'metadata'
|
|
|
|
|
message: string
|
|
|
|
|
processed: number
|
|
|
|
|
total: number
|
|
|
|
|
}) => void
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface VFSStructureResult {
|
|
|
|
|
/** Root path created */
|
|
|
|
|
rootPath: string
|
|
|
|
|
|
|
|
|
|
/** Directories created */
|
|
|
|
|
directories: string[]
|
|
|
|
|
|
|
|
|
|
/** Files created */
|
|
|
|
|
files: Array<{
|
|
|
|
|
path: string
|
|
|
|
|
entityId?: string
|
|
|
|
|
type: 'entity' | 'metadata' | 'source' | 'relationships'
|
|
|
|
|
}>
|
|
|
|
|
|
|
|
|
|
/** Total operations */
|
|
|
|
|
operations: number
|
|
|
|
|
|
|
|
|
|
/** Time taken in ms */
|
|
|
|
|
duration: number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* VFSStructureGenerator - Organizes imported data into VFS
|
|
|
|
|
*/
|
|
|
|
|
export class VFSStructureGenerator {
|
|
|
|
|
private brain: Brainy
|
feat: add confidence/weight to Entity and flatten Result fields for convenient access
Add confidence and weight properties to Entity interface and flatten Result fields to top level for improved developer experience and API consistency.
Breaking Changes: None (all changes are backward compatible)
Phase 2 - Entity Confidence & Weight:
- Add confidence (type classification certainty) and weight (entity importance) to Entity interface
- Add confidence/weight parameters to AddParams and UpdateParams
- Update convertNounToEntity() to extract confidence/weight from storage
- Update add() and update() methods to preserve confidence/weight in metadata
- Enable developers to specify and access entity confidence/weight scores
Phase 3 - Result Field Flattening:
- Flatten commonly-used entity fields (type, metadata, data, confidence, weight) to Result top level
- Add createResult() helper for consistent Result construction
- Update all find() code paths to use createResult()
- Enable direct access: result.metadata instead of result.entity.metadata
- Preserve full entity in result.entity for backward compatibility
VFS Fix (from previous work):
- Fix VFSStructureGenerator to use brain.vfs() cached instance instead of creating separate instance
- Improve VFS error messages with step-by-step guidance
- Update examples to show correct vfs.init() usage
- Add comprehensive VFS import verification tests
Documentation Updates:
- Update API_REFERENCE.md with confidence/weight examples and flattened Result documentation
- Enhance JSDoc for add(), get(), find(), similar() with v4.3.0 examples
- Document Result structure changes and backward compatibility
- Add migration examples showing both old and new access patterns
Tests:
- Add 16 comprehensive tests for Entity confidence/weight exposure
- Add tests for Result field flattening
- Add tests for backward compatibility
- All tests passing (16/16)
API Consistency:
- Entity: direct access to confidence/weight
- Result: flattened fields + nested entity (both work)
- Relation: already had confidence/weight (consistent)
- VFS: inherits from Entity (automatic)
Files Changed:
- src/types/brainy.types.ts - Updated Entity, AddParams, UpdateParams, Result interfaces
- src/brainy.ts - Updated implementation and JSDoc for all affected methods
- tests/integration/entity-confidence-weight.test.ts - 16 comprehensive tests
- docs/API_REFERENCE.md - Updated with v4.3.0 examples
- src/importers/VFSStructureGenerator.ts - VFS fix
- src/vfs/VirtualFileSystem.ts - Improved error messages
- examples/unified-import-example.ts - Added vfs.init() example
- tests/integration/vfs-*-verification.test.ts - VFS verification tests
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-23 12:19:50 -07:00
|
|
|
private vfs!: VirtualFileSystem // Non-null assertion - will be set in init()
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
|
|
|
|
|
constructor(brain: Brainy) {
|
|
|
|
|
this.brain = brain
|
feat: add confidence/weight to Entity and flatten Result fields for convenient access
Add confidence and weight properties to Entity interface and flatten Result fields to top level for improved developer experience and API consistency.
Breaking Changes: None (all changes are backward compatible)
Phase 2 - Entity Confidence & Weight:
- Add confidence (type classification certainty) and weight (entity importance) to Entity interface
- Add confidence/weight parameters to AddParams and UpdateParams
- Update convertNounToEntity() to extract confidence/weight from storage
- Update add() and update() methods to preserve confidence/weight in metadata
- Enable developers to specify and access entity confidence/weight scores
Phase 3 - Result Field Flattening:
- Flatten commonly-used entity fields (type, metadata, data, confidence, weight) to Result top level
- Add createResult() helper for consistent Result construction
- Update all find() code paths to use createResult()
- Enable direct access: result.metadata instead of result.entity.metadata
- Preserve full entity in result.entity for backward compatibility
VFS Fix (from previous work):
- Fix VFSStructureGenerator to use brain.vfs() cached instance instead of creating separate instance
- Improve VFS error messages with step-by-step guidance
- Update examples to show correct vfs.init() usage
- Add comprehensive VFS import verification tests
Documentation Updates:
- Update API_REFERENCE.md with confidence/weight examples and flattened Result documentation
- Enhance JSDoc for add(), get(), find(), similar() with v4.3.0 examples
- Document Result structure changes and backward compatibility
- Add migration examples showing both old and new access patterns
Tests:
- Add 16 comprehensive tests for Entity confidence/weight exposure
- Add tests for Result field flattening
- Add tests for backward compatibility
- All tests passing (16/16)
API Consistency:
- Entity: direct access to confidence/weight
- Result: flattened fields + nested entity (both work)
- Relation: already had confidence/weight (consistent)
- VFS: inherits from Entity (automatic)
Files Changed:
- src/types/brainy.types.ts - Updated Entity, AddParams, UpdateParams, Result interfaces
- src/brainy.ts - Updated implementation and JSDoc for all affected methods
- tests/integration/entity-confidence-weight.test.ts - 16 comprehensive tests
- docs/API_REFERENCE.md - Updated with v4.3.0 examples
- src/importers/VFSStructureGenerator.ts - VFS fix
- src/vfs/VirtualFileSystem.ts - Improved error messages
- examples/unified-import-example.ts - Added vfs.init() example
- tests/integration/vfs-*-verification.test.ts - VFS verification tests
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-23 12:19:50 -07:00
|
|
|
// CRITICAL FIX: Use brain.vfs() instead of creating separate instance
|
|
|
|
|
// This ensures VFSStructureGenerator and user code share the same VFS instance
|
|
|
|
|
// Before: Created separate instance that wasn't accessible to users
|
|
|
|
|
// After: Uses brain's cached instance, making VFS queryable after import
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the generator
|
feat: add confidence/weight to Entity and flatten Result fields for convenient access
Add confidence and weight properties to Entity interface and flatten Result fields to top level for improved developer experience and API consistency.
Breaking Changes: None (all changes are backward compatible)
Phase 2 - Entity Confidence & Weight:
- Add confidence (type classification certainty) and weight (entity importance) to Entity interface
- Add confidence/weight parameters to AddParams and UpdateParams
- Update convertNounToEntity() to extract confidence/weight from storage
- Update add() and update() methods to preserve confidence/weight in metadata
- Enable developers to specify and access entity confidence/weight scores
Phase 3 - Result Field Flattening:
- Flatten commonly-used entity fields (type, metadata, data, confidence, weight) to Result top level
- Add createResult() helper for consistent Result construction
- Update all find() code paths to use createResult()
- Enable direct access: result.metadata instead of result.entity.metadata
- Preserve full entity in result.entity for backward compatibility
VFS Fix (from previous work):
- Fix VFSStructureGenerator to use brain.vfs() cached instance instead of creating separate instance
- Improve VFS error messages with step-by-step guidance
- Update examples to show correct vfs.init() usage
- Add comprehensive VFS import verification tests
Documentation Updates:
- Update API_REFERENCE.md with confidence/weight examples and flattened Result documentation
- Enhance JSDoc for add(), get(), find(), similar() with v4.3.0 examples
- Document Result structure changes and backward compatibility
- Add migration examples showing both old and new access patterns
Tests:
- Add 16 comprehensive tests for Entity confidence/weight exposure
- Add tests for Result field flattening
- Add tests for backward compatibility
- All tests passing (16/16)
API Consistency:
- Entity: direct access to confidence/weight
- Result: flattened fields + nested entity (both work)
- Relation: already had confidence/weight (consistent)
- VFS: inherits from Entity (automatic)
Files Changed:
- src/types/brainy.types.ts - Updated Entity, AddParams, UpdateParams, Result interfaces
- src/brainy.ts - Updated implementation and JSDoc for all affected methods
- tests/integration/entity-confidence-weight.test.ts - 16 comprehensive tests
- docs/API_REFERENCE.md - Updated with v4.3.0 examples
- src/importers/VFSStructureGenerator.ts - VFS fix
- src/vfs/VirtualFileSystem.ts - Improved error messages
- examples/unified-import-example.ts - Added vfs.init() example
- tests/integration/vfs-*-verification.test.ts - VFS verification tests
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-23 12:19:50 -07:00
|
|
|
*
|
|
|
|
|
* CRITICAL: Gets brain's VFS instance and initializes it if needed.
|
|
|
|
|
* This ensures that after import, brain.vfs() returns an initialized instance.
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
*/
|
|
|
|
|
async init(): Promise<void> {
|
feat: add confidence/weight to Entity and flatten Result fields for convenient access
Add confidence and weight properties to Entity interface and flatten Result fields to top level for improved developer experience and API consistency.
Breaking Changes: None (all changes are backward compatible)
Phase 2 - Entity Confidence & Weight:
- Add confidence (type classification certainty) and weight (entity importance) to Entity interface
- Add confidence/weight parameters to AddParams and UpdateParams
- Update convertNounToEntity() to extract confidence/weight from storage
- Update add() and update() methods to preserve confidence/weight in metadata
- Enable developers to specify and access entity confidence/weight scores
Phase 3 - Result Field Flattening:
- Flatten commonly-used entity fields (type, metadata, data, confidence, weight) to Result top level
- Add createResult() helper for consistent Result construction
- Update all find() code paths to use createResult()
- Enable direct access: result.metadata instead of result.entity.metadata
- Preserve full entity in result.entity for backward compatibility
VFS Fix (from previous work):
- Fix VFSStructureGenerator to use brain.vfs() cached instance instead of creating separate instance
- Improve VFS error messages with step-by-step guidance
- Update examples to show correct vfs.init() usage
- Add comprehensive VFS import verification tests
Documentation Updates:
- Update API_REFERENCE.md with confidence/weight examples and flattened Result documentation
- Enhance JSDoc for add(), get(), find(), similar() with v4.3.0 examples
- Document Result structure changes and backward compatibility
- Add migration examples showing both old and new access patterns
Tests:
- Add 16 comprehensive tests for Entity confidence/weight exposure
- Add tests for Result field flattening
- Add tests for backward compatibility
- All tests passing (16/16)
API Consistency:
- Entity: direct access to confidence/weight
- Result: flattened fields + nested entity (both work)
- Relation: already had confidence/weight (consistent)
- VFS: inherits from Entity (automatic)
Files Changed:
- src/types/brainy.types.ts - Updated Entity, AddParams, UpdateParams, Result interfaces
- src/brainy.ts - Updated implementation and JSDoc for all affected methods
- tests/integration/entity-confidence-weight.test.ts - 16 comprehensive tests
- docs/API_REFERENCE.md - Updated with v4.3.0 examples
- src/importers/VFSStructureGenerator.ts - VFS fix
- src/vfs/VirtualFileSystem.ts - Improved error messages
- examples/unified-import-example.ts - Added vfs.init() example
- tests/integration/vfs-*-verification.test.ts - VFS verification tests
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-23 12:19:50 -07:00
|
|
|
// Get brain's cached VFS instance (creates if doesn't exist)
|
|
|
|
|
this.vfs = this.brain.vfs()
|
|
|
|
|
|
2025-10-29 19:13:04 -07:00
|
|
|
// CRITICAL FIX (v4.10.2): Always call vfs.init() explicitly
|
|
|
|
|
// The previous code tried to check if initialized via stat('/') but this was unreliable
|
|
|
|
|
// vfs.init() is idempotent, so calling it multiple times is safe
|
|
|
|
|
await this.vfs.init()
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate VFS structure from import result
|
|
|
|
|
*/
|
|
|
|
|
async generate(
|
|
|
|
|
importResult: SmartExcelResult,
|
|
|
|
|
options: VFSStructureOptions
|
|
|
|
|
): Promise<VFSStructureResult> {
|
|
|
|
|
const startTime = Date.now()
|
|
|
|
|
const result: VFSStructureResult = {
|
|
|
|
|
rootPath: options.rootPath,
|
|
|
|
|
directories: [],
|
|
|
|
|
files: [],
|
|
|
|
|
operations: 0,
|
|
|
|
|
duration: 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure VFS is initialized
|
|
|
|
|
await this.init()
|
|
|
|
|
|
2025-10-30 13:19:08 -07:00
|
|
|
// v4.11.1: Calculate total operations for progress tracking
|
|
|
|
|
const groups = this.groupEntities(importResult, options)
|
|
|
|
|
const totalEntities = Array.from(groups.values()).reduce((sum, entities) => sum + entities.length, 0)
|
|
|
|
|
const totalOperations =
|
|
|
|
|
1 + // root directory
|
|
|
|
|
(options.preserveSource ? 1 : 0) + // source file
|
|
|
|
|
groups.size + // group directories
|
|
|
|
|
totalEntities + // entity files
|
|
|
|
|
(options.createRelationshipFile !== false ? 1 : 0) + // relationships file
|
|
|
|
|
(options.createMetadataFile !== false ? 1 : 0) // metadata file
|
|
|
|
|
|
|
|
|
|
let completedOperations = 0
|
|
|
|
|
|
|
|
|
|
// Helper to report progress
|
|
|
|
|
const reportProgress = (stage: 'directories' | 'entities' | 'metadata', message: string) => {
|
|
|
|
|
completedOperations++
|
|
|
|
|
options.onProgress?.({
|
|
|
|
|
stage,
|
|
|
|
|
message,
|
|
|
|
|
processed: completedOperations,
|
|
|
|
|
total: totalOperations
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
fix: resolve HNSW concurrency race condition across all storage adapters
Fixes critical P0 bug causing data corruption during bulk imports with 50+ concurrent operations. The non-atomic read-modify-write pattern in saveHNSWData() combined with fire-and-forget neighbor updates was causing 16-32 concurrent writes per entity, resulting in lost HNSW connections and corrupted graph structure.
**Root Cause:**
- saveHNSWData() used non-atomic read-modify-write
- HNSW neighbor updates fired without await (16-32 concurrent writes/entity)
- Popular nodes became hotspots (100 concurrent imports = 3,400 concurrent saveHNSWData calls)
- Result: Lost neighbor connections, 0 search results
**Atomic Write Strategies by Adapter:**
FileSystemStorage:
- Atomic rename with temp files
- Write to {file}.tmp.{timestamp}.{random}
- POSIX-guaranteed atomic rename(temp, final)
GCSStorage:
- Optimistic locking with generation numbers
- preconditionOpts: { ifGenerationMatch }
- 5 retries with exponential backoff (50ms→800ms)
S3/R2/AzureStorage:
- ETag-based optimistic locking
- IfMatch/conditions preconditions
- 5 retries with exponential backoff
MemoryStorage + OPFSStorage:
- Mutex locks per entity path
- Serializes async operations even in single-threaded environments
HNSW Index:
- Changed fire-and-forget .catch() to await
- Serializes 16-32 neighbor updates per entity
- Trade-off: 20-30% slower bulk import vs 100% data integrity
**Sharding Compatibility:**
- ✅ Works with deterministic UUID sharding (256 shards, always on)
- ✅ Works with distributed multi-node sharding (optional)
- ✅ All atomic strategies work in both single-node and distributed deployments
**Index Impact:**
- Only HNSW index modified (saveHNSWData, saveHNSWSystem)
- Other 4 indexes unaffected (Metadata, Graph Adjacency, Deleted Items, Entity ID Mapper)
- No regression risk - isolated code paths
**Testing:**
- 8/8 unit tests passing (real concurrent operations, no mocks)
- Tests verify data integrity after 20 concurrent updates
- Tests verify temp file cleanup and mutex serialization
**Files Modified:**
- All 8 storage adapters (FileSystem, GCS, S3, R2, Azure, Memory, OPFS)
- HNSW Index (neighbor update serialization)
- New test: tests/unit/storage/hnswConcurrency.test.ts (8 passing tests)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 15:24:20 -07:00
|
|
|
// Extract tracking metadata if provided
|
|
|
|
|
const trackingMetadata = options.trackingContext ? {
|
|
|
|
|
importIds: [options.trackingContext.importId],
|
|
|
|
|
projectId: options.trackingContext.projectId,
|
|
|
|
|
importedAt: options.trackingContext.importedAt,
|
|
|
|
|
importFormat: options.trackingContext.importFormat,
|
|
|
|
|
importSource: options.trackingContext.importSource,
|
|
|
|
|
...options.trackingContext.customMetadata
|
|
|
|
|
} : {}
|
|
|
|
|
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
// Create root directory
|
|
|
|
|
try {
|
fix: resolve HNSW concurrency race condition across all storage adapters
Fixes critical P0 bug causing data corruption during bulk imports with 50+ concurrent operations. The non-atomic read-modify-write pattern in saveHNSWData() combined with fire-and-forget neighbor updates was causing 16-32 concurrent writes per entity, resulting in lost HNSW connections and corrupted graph structure.
**Root Cause:**
- saveHNSWData() used non-atomic read-modify-write
- HNSW neighbor updates fired without await (16-32 concurrent writes/entity)
- Popular nodes became hotspots (100 concurrent imports = 3,400 concurrent saveHNSWData calls)
- Result: Lost neighbor connections, 0 search results
**Atomic Write Strategies by Adapter:**
FileSystemStorage:
- Atomic rename with temp files
- Write to {file}.tmp.{timestamp}.{random}
- POSIX-guaranteed atomic rename(temp, final)
GCSStorage:
- Optimistic locking with generation numbers
- preconditionOpts: { ifGenerationMatch }
- 5 retries with exponential backoff (50ms→800ms)
S3/R2/AzureStorage:
- ETag-based optimistic locking
- IfMatch/conditions preconditions
- 5 retries with exponential backoff
MemoryStorage + OPFSStorage:
- Mutex locks per entity path
- Serializes async operations even in single-threaded environments
HNSW Index:
- Changed fire-and-forget .catch() to await
- Serializes 16-32 neighbor updates per entity
- Trade-off: 20-30% slower bulk import vs 100% data integrity
**Sharding Compatibility:**
- ✅ Works with deterministic UUID sharding (256 shards, always on)
- ✅ Works with distributed multi-node sharding (optional)
- ✅ All atomic strategies work in both single-node and distributed deployments
**Index Impact:**
- Only HNSW index modified (saveHNSWData, saveHNSWSystem)
- Other 4 indexes unaffected (Metadata, Graph Adjacency, Deleted Items, Entity ID Mapper)
- No regression risk - isolated code paths
**Testing:**
- 8/8 unit tests passing (real concurrent operations, no mocks)
- Tests verify data integrity after 20 concurrent updates
- Tests verify temp file cleanup and mutex serialization
**Files Modified:**
- All 8 storage adapters (FileSystem, GCS, S3, R2, Azure, Memory, OPFS)
- HNSW Index (neighbor update serialization)
- New test: tests/unit/storage/hnswConcurrency.test.ts (8 passing tests)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 15:24:20 -07:00
|
|
|
await this.vfs.mkdir(options.rootPath, {
|
|
|
|
|
recursive: true,
|
|
|
|
|
metadata: trackingMetadata // v4.10.0: Add tracking metadata
|
|
|
|
|
})
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
result.directories.push(options.rootPath)
|
|
|
|
|
result.operations++
|
2025-10-30 13:19:08 -07:00
|
|
|
reportProgress('directories', `Created root directory: ${options.rootPath}`)
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
} catch (error: any) {
|
|
|
|
|
// Directory might already exist, that's fine
|
|
|
|
|
if (error.code !== 'EEXIST') {
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
result.directories.push(options.rootPath)
|
2025-10-30 13:19:08 -07:00
|
|
|
reportProgress('directories', `Root directory exists: ${options.rootPath}`)
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Preserve source file if requested
|
|
|
|
|
if (options.preserveSource && options.sourceBuffer && options.sourceFilename) {
|
|
|
|
|
const sourcePath = `${options.rootPath}/_source${this.getExtension(options.sourceFilename)}`
|
fix: resolve HNSW concurrency race condition across all storage adapters
Fixes critical P0 bug causing data corruption during bulk imports with 50+ concurrent operations. The non-atomic read-modify-write pattern in saveHNSWData() combined with fire-and-forget neighbor updates was causing 16-32 concurrent writes per entity, resulting in lost HNSW connections and corrupted graph structure.
**Root Cause:**
- saveHNSWData() used non-atomic read-modify-write
- HNSW neighbor updates fired without await (16-32 concurrent writes/entity)
- Popular nodes became hotspots (100 concurrent imports = 3,400 concurrent saveHNSWData calls)
- Result: Lost neighbor connections, 0 search results
**Atomic Write Strategies by Adapter:**
FileSystemStorage:
- Atomic rename with temp files
- Write to {file}.tmp.{timestamp}.{random}
- POSIX-guaranteed atomic rename(temp, final)
GCSStorage:
- Optimistic locking with generation numbers
- preconditionOpts: { ifGenerationMatch }
- 5 retries with exponential backoff (50ms→800ms)
S3/R2/AzureStorage:
- ETag-based optimistic locking
- IfMatch/conditions preconditions
- 5 retries with exponential backoff
MemoryStorage + OPFSStorage:
- Mutex locks per entity path
- Serializes async operations even in single-threaded environments
HNSW Index:
- Changed fire-and-forget .catch() to await
- Serializes 16-32 neighbor updates per entity
- Trade-off: 20-30% slower bulk import vs 100% data integrity
**Sharding Compatibility:**
- ✅ Works with deterministic UUID sharding (256 shards, always on)
- ✅ Works with distributed multi-node sharding (optional)
- ✅ All atomic strategies work in both single-node and distributed deployments
**Index Impact:**
- Only HNSW index modified (saveHNSWData, saveHNSWSystem)
- Other 4 indexes unaffected (Metadata, Graph Adjacency, Deleted Items, Entity ID Mapper)
- No regression risk - isolated code paths
**Testing:**
- 8/8 unit tests passing (real concurrent operations, no mocks)
- Tests verify data integrity after 20 concurrent updates
- Tests verify temp file cleanup and mutex serialization
**Files Modified:**
- All 8 storage adapters (FileSystem, GCS, S3, R2, Azure, Memory, OPFS)
- HNSW Index (neighbor update serialization)
- New test: tests/unit/storage/hnswConcurrency.test.ts (8 passing tests)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 15:24:20 -07:00
|
|
|
await this.vfs.writeFile(sourcePath, options.sourceBuffer, {
|
|
|
|
|
metadata: trackingMetadata // v4.10.0: Add tracking metadata
|
|
|
|
|
})
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
result.files.push({
|
|
|
|
|
path: sourcePath,
|
|
|
|
|
type: 'source'
|
|
|
|
|
})
|
|
|
|
|
result.operations++
|
2025-10-30 13:19:08 -07:00
|
|
|
reportProgress('metadata', `Preserved source file: ${options.sourceFilename}`)
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-30 13:19:08 -07:00
|
|
|
// Note: groups already calculated above for progress tracking
|
|
|
|
|
// const groups = this.groupEntities(importResult, options)
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
|
|
|
|
|
// Create directories and files for each group
|
|
|
|
|
for (const [groupName, entities] of groups.entries()) {
|
|
|
|
|
const groupPath = `${options.rootPath}/${groupName}`
|
|
|
|
|
|
|
|
|
|
// Create group directory
|
|
|
|
|
try {
|
fix: resolve HNSW concurrency race condition across all storage adapters
Fixes critical P0 bug causing data corruption during bulk imports with 50+ concurrent operations. The non-atomic read-modify-write pattern in saveHNSWData() combined with fire-and-forget neighbor updates was causing 16-32 concurrent writes per entity, resulting in lost HNSW connections and corrupted graph structure.
**Root Cause:**
- saveHNSWData() used non-atomic read-modify-write
- HNSW neighbor updates fired without await (16-32 concurrent writes/entity)
- Popular nodes became hotspots (100 concurrent imports = 3,400 concurrent saveHNSWData calls)
- Result: Lost neighbor connections, 0 search results
**Atomic Write Strategies by Adapter:**
FileSystemStorage:
- Atomic rename with temp files
- Write to {file}.tmp.{timestamp}.{random}
- POSIX-guaranteed atomic rename(temp, final)
GCSStorage:
- Optimistic locking with generation numbers
- preconditionOpts: { ifGenerationMatch }
- 5 retries with exponential backoff (50ms→800ms)
S3/R2/AzureStorage:
- ETag-based optimistic locking
- IfMatch/conditions preconditions
- 5 retries with exponential backoff
MemoryStorage + OPFSStorage:
- Mutex locks per entity path
- Serializes async operations even in single-threaded environments
HNSW Index:
- Changed fire-and-forget .catch() to await
- Serializes 16-32 neighbor updates per entity
- Trade-off: 20-30% slower bulk import vs 100% data integrity
**Sharding Compatibility:**
- ✅ Works with deterministic UUID sharding (256 shards, always on)
- ✅ Works with distributed multi-node sharding (optional)
- ✅ All atomic strategies work in both single-node and distributed deployments
**Index Impact:**
- Only HNSW index modified (saveHNSWData, saveHNSWSystem)
- Other 4 indexes unaffected (Metadata, Graph Adjacency, Deleted Items, Entity ID Mapper)
- No regression risk - isolated code paths
**Testing:**
- 8/8 unit tests passing (real concurrent operations, no mocks)
- Tests verify data integrity after 20 concurrent updates
- Tests verify temp file cleanup and mutex serialization
**Files Modified:**
- All 8 storage adapters (FileSystem, GCS, S3, R2, Azure, Memory, OPFS)
- HNSW Index (neighbor update serialization)
- New test: tests/unit/storage/hnswConcurrency.test.ts (8 passing tests)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 15:24:20 -07:00
|
|
|
await this.vfs.mkdir(groupPath, {
|
|
|
|
|
recursive: true,
|
|
|
|
|
metadata: trackingMetadata // v4.10.0: Add tracking metadata
|
|
|
|
|
})
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
result.directories.push(groupPath)
|
|
|
|
|
result.operations++
|
2025-10-30 13:19:08 -07:00
|
|
|
reportProgress('directories', `Created directory: ${groupName} (${entities.length} entities)`)
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
} catch (error: any) {
|
|
|
|
|
// Directory might already exist
|
|
|
|
|
if (error.code !== 'EEXIST') {
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
result.directories.push(groupPath)
|
2025-10-30 13:19:08 -07:00
|
|
|
reportProgress('directories', `Directory exists: ${groupName}`)
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create entity files
|
2025-10-30 13:19:08 -07:00
|
|
|
let entityCount = 0
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
for (const extracted of entities) {
|
2025-10-30 13:19:08 -07:00
|
|
|
entityCount++
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
const sanitizedName = this.sanitizeFilename(extracted.entity.name)
|
|
|
|
|
const entityPath = `${groupPath}/${sanitizedName}.json`
|
|
|
|
|
|
|
|
|
|
// Create entity JSON
|
|
|
|
|
const entityJson = {
|
|
|
|
|
id: extracted.entity.id,
|
|
|
|
|
name: extracted.entity.name,
|
|
|
|
|
type: extracted.entity.type,
|
|
|
|
|
description: extracted.entity.description,
|
|
|
|
|
confidence: extracted.entity.confidence,
|
|
|
|
|
metadata: extracted.entity.metadata,
|
|
|
|
|
concepts: extracted.concepts || [],
|
|
|
|
|
relatedEntities: extracted.relatedEntities,
|
|
|
|
|
relationships: extracted.relationships.map(rel => ({
|
|
|
|
|
from: rel.from,
|
|
|
|
|
to: rel.to,
|
|
|
|
|
type: rel.type,
|
|
|
|
|
confidence: rel.confidence,
|
|
|
|
|
evidence: rel.evidence
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
fix: resolve HNSW concurrency race condition across all storage adapters
Fixes critical P0 bug causing data corruption during bulk imports with 50+ concurrent operations. The non-atomic read-modify-write pattern in saveHNSWData() combined with fire-and-forget neighbor updates was causing 16-32 concurrent writes per entity, resulting in lost HNSW connections and corrupted graph structure.
**Root Cause:**
- saveHNSWData() used non-atomic read-modify-write
- HNSW neighbor updates fired without await (16-32 concurrent writes/entity)
- Popular nodes became hotspots (100 concurrent imports = 3,400 concurrent saveHNSWData calls)
- Result: Lost neighbor connections, 0 search results
**Atomic Write Strategies by Adapter:**
FileSystemStorage:
- Atomic rename with temp files
- Write to {file}.tmp.{timestamp}.{random}
- POSIX-guaranteed atomic rename(temp, final)
GCSStorage:
- Optimistic locking with generation numbers
- preconditionOpts: { ifGenerationMatch }
- 5 retries with exponential backoff (50ms→800ms)
S3/R2/AzureStorage:
- ETag-based optimistic locking
- IfMatch/conditions preconditions
- 5 retries with exponential backoff
MemoryStorage + OPFSStorage:
- Mutex locks per entity path
- Serializes async operations even in single-threaded environments
HNSW Index:
- Changed fire-and-forget .catch() to await
- Serializes 16-32 neighbor updates per entity
- Trade-off: 20-30% slower bulk import vs 100% data integrity
**Sharding Compatibility:**
- ✅ Works with deterministic UUID sharding (256 shards, always on)
- ✅ Works with distributed multi-node sharding (optional)
- ✅ All atomic strategies work in both single-node and distributed deployments
**Index Impact:**
- Only HNSW index modified (saveHNSWData, saveHNSWSystem)
- Other 4 indexes unaffected (Metadata, Graph Adjacency, Deleted Items, Entity ID Mapper)
- No regression risk - isolated code paths
**Testing:**
- 8/8 unit tests passing (real concurrent operations, no mocks)
- Tests verify data integrity after 20 concurrent updates
- Tests verify temp file cleanup and mutex serialization
**Files Modified:**
- All 8 storage adapters (FileSystem, GCS, S3, R2, Azure, Memory, OPFS)
- HNSW Index (neighbor update serialization)
- New test: tests/unit/storage/hnswConcurrency.test.ts (8 passing tests)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 15:24:20 -07:00
|
|
|
await this.vfs.writeFile(entityPath, JSON.stringify(entityJson, null, 2), {
|
|
|
|
|
metadata: {
|
|
|
|
|
...trackingMetadata, // v4.10.0: Add tracking metadata
|
|
|
|
|
entityId: extracted.entity.id
|
|
|
|
|
}
|
|
|
|
|
})
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
result.files.push({
|
|
|
|
|
path: entityPath,
|
|
|
|
|
entityId: extracted.entity.id,
|
|
|
|
|
type: 'entity'
|
|
|
|
|
})
|
|
|
|
|
result.operations++
|
2025-10-30 13:19:08 -07:00
|
|
|
|
|
|
|
|
// v4.11.1: Report progress every 10 entities (or on last entity)
|
|
|
|
|
if (entityCount % 10 === 0 || entityCount === entities.length) {
|
|
|
|
|
reportProgress('entities', `Created ${entityCount}/${entities.length} ${groupName} files`)
|
|
|
|
|
}
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create relationships file
|
|
|
|
|
if (options.createRelationshipFile !== false) {
|
|
|
|
|
const relationshipsPath = `${options.rootPath}/_relationships.json`
|
|
|
|
|
const allRelationships = importResult.rows.flatMap(row => row.relationships)
|
|
|
|
|
|
|
|
|
|
const relationshipsJson = {
|
|
|
|
|
source: options.sourceFilename || 'unknown',
|
|
|
|
|
count: allRelationships.length,
|
|
|
|
|
relationships: allRelationships,
|
|
|
|
|
stats: {
|
|
|
|
|
byType: this.groupByType(allRelationships, 'type'),
|
|
|
|
|
byConfidence: {
|
|
|
|
|
high: allRelationships.filter(r => r.confidence > 0.8).length,
|
|
|
|
|
medium: allRelationships.filter(r => r.confidence >= 0.6 && r.confidence <= 0.8).length,
|
|
|
|
|
low: allRelationships.filter(r => r.confidence < 0.6).length
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
fix: resolve HNSW concurrency race condition across all storage adapters
Fixes critical P0 bug causing data corruption during bulk imports with 50+ concurrent operations. The non-atomic read-modify-write pattern in saveHNSWData() combined with fire-and-forget neighbor updates was causing 16-32 concurrent writes per entity, resulting in lost HNSW connections and corrupted graph structure.
**Root Cause:**
- saveHNSWData() used non-atomic read-modify-write
- HNSW neighbor updates fired without await (16-32 concurrent writes/entity)
- Popular nodes became hotspots (100 concurrent imports = 3,400 concurrent saveHNSWData calls)
- Result: Lost neighbor connections, 0 search results
**Atomic Write Strategies by Adapter:**
FileSystemStorage:
- Atomic rename with temp files
- Write to {file}.tmp.{timestamp}.{random}
- POSIX-guaranteed atomic rename(temp, final)
GCSStorage:
- Optimistic locking with generation numbers
- preconditionOpts: { ifGenerationMatch }
- 5 retries with exponential backoff (50ms→800ms)
S3/R2/AzureStorage:
- ETag-based optimistic locking
- IfMatch/conditions preconditions
- 5 retries with exponential backoff
MemoryStorage + OPFSStorage:
- Mutex locks per entity path
- Serializes async operations even in single-threaded environments
HNSW Index:
- Changed fire-and-forget .catch() to await
- Serializes 16-32 neighbor updates per entity
- Trade-off: 20-30% slower bulk import vs 100% data integrity
**Sharding Compatibility:**
- ✅ Works with deterministic UUID sharding (256 shards, always on)
- ✅ Works with distributed multi-node sharding (optional)
- ✅ All atomic strategies work in both single-node and distributed deployments
**Index Impact:**
- Only HNSW index modified (saveHNSWData, saveHNSWSystem)
- Other 4 indexes unaffected (Metadata, Graph Adjacency, Deleted Items, Entity ID Mapper)
- No regression risk - isolated code paths
**Testing:**
- 8/8 unit tests passing (real concurrent operations, no mocks)
- Tests verify data integrity after 20 concurrent updates
- Tests verify temp file cleanup and mutex serialization
**Files Modified:**
- All 8 storage adapters (FileSystem, GCS, S3, R2, Azure, Memory, OPFS)
- HNSW Index (neighbor update serialization)
- New test: tests/unit/storage/hnswConcurrency.test.ts (8 passing tests)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 15:24:20 -07:00
|
|
|
await this.vfs.writeFile(relationshipsPath, JSON.stringify(relationshipsJson, null, 2), {
|
|
|
|
|
metadata: trackingMetadata // v4.10.0: Add tracking metadata
|
|
|
|
|
})
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
result.files.push({
|
|
|
|
|
path: relationshipsPath,
|
|
|
|
|
type: 'relationships'
|
|
|
|
|
})
|
|
|
|
|
result.operations++
|
2025-10-30 13:19:08 -07:00
|
|
|
reportProgress('metadata', `Created relationships file (${allRelationships.length} relationships)`)
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create metadata file
|
|
|
|
|
if (options.createMetadataFile !== false) {
|
|
|
|
|
const metadataPath = `${options.rootPath}/_metadata.json`
|
|
|
|
|
const metadataJson = {
|
|
|
|
|
import: {
|
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
source: {
|
|
|
|
|
filename: options.sourceFilename || 'unknown',
|
|
|
|
|
format: 'excel'
|
|
|
|
|
},
|
|
|
|
|
options: {
|
|
|
|
|
groupBy: options.groupBy,
|
|
|
|
|
preserveSource: options.preserveSource
|
|
|
|
|
},
|
|
|
|
|
stats: {
|
|
|
|
|
rowsProcessed: importResult.rowsProcessed,
|
|
|
|
|
entitiesExtracted: importResult.entitiesExtracted,
|
|
|
|
|
relationshipsInferred: importResult.relationshipsInferred,
|
|
|
|
|
processingTime: importResult.processingTime,
|
|
|
|
|
byType: importResult.stats.byType,
|
|
|
|
|
byConfidence: importResult.stats.byConfidence
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
structure: {
|
|
|
|
|
rootPath: options.rootPath,
|
|
|
|
|
groupingStrategy: options.groupBy,
|
|
|
|
|
directories: result.directories,
|
|
|
|
|
fileCount: result.files.length
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
fix: resolve HNSW concurrency race condition across all storage adapters
Fixes critical P0 bug causing data corruption during bulk imports with 50+ concurrent operations. The non-atomic read-modify-write pattern in saveHNSWData() combined with fire-and-forget neighbor updates was causing 16-32 concurrent writes per entity, resulting in lost HNSW connections and corrupted graph structure.
**Root Cause:**
- saveHNSWData() used non-atomic read-modify-write
- HNSW neighbor updates fired without await (16-32 concurrent writes/entity)
- Popular nodes became hotspots (100 concurrent imports = 3,400 concurrent saveHNSWData calls)
- Result: Lost neighbor connections, 0 search results
**Atomic Write Strategies by Adapter:**
FileSystemStorage:
- Atomic rename with temp files
- Write to {file}.tmp.{timestamp}.{random}
- POSIX-guaranteed atomic rename(temp, final)
GCSStorage:
- Optimistic locking with generation numbers
- preconditionOpts: { ifGenerationMatch }
- 5 retries with exponential backoff (50ms→800ms)
S3/R2/AzureStorage:
- ETag-based optimistic locking
- IfMatch/conditions preconditions
- 5 retries with exponential backoff
MemoryStorage + OPFSStorage:
- Mutex locks per entity path
- Serializes async operations even in single-threaded environments
HNSW Index:
- Changed fire-and-forget .catch() to await
- Serializes 16-32 neighbor updates per entity
- Trade-off: 20-30% slower bulk import vs 100% data integrity
**Sharding Compatibility:**
- ✅ Works with deterministic UUID sharding (256 shards, always on)
- ✅ Works with distributed multi-node sharding (optional)
- ✅ All atomic strategies work in both single-node and distributed deployments
**Index Impact:**
- Only HNSW index modified (saveHNSWData, saveHNSWSystem)
- Other 4 indexes unaffected (Metadata, Graph Adjacency, Deleted Items, Entity ID Mapper)
- No regression risk - isolated code paths
**Testing:**
- 8/8 unit tests passing (real concurrent operations, no mocks)
- Tests verify data integrity after 20 concurrent updates
- Tests verify temp file cleanup and mutex serialization
**Files Modified:**
- All 8 storage adapters (FileSystem, GCS, S3, R2, Azure, Memory, OPFS)
- HNSW Index (neighbor update serialization)
- New test: tests/unit/storage/hnswConcurrency.test.ts (8 passing tests)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 15:24:20 -07:00
|
|
|
await this.vfs.writeFile(metadataPath, JSON.stringify(metadataJson, null, 2), {
|
|
|
|
|
metadata: trackingMetadata // v4.10.0: Add tracking metadata
|
|
|
|
|
})
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
result.files.push({
|
|
|
|
|
path: metadataPath,
|
|
|
|
|
type: 'metadata'
|
|
|
|
|
})
|
|
|
|
|
result.operations++
|
2025-10-30 13:19:08 -07:00
|
|
|
reportProgress('metadata', 'Created metadata file')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// v4.11.1: Final progress update
|
|
|
|
|
if (options.onProgress) {
|
|
|
|
|
options.onProgress({
|
|
|
|
|
stage: 'metadata',
|
|
|
|
|
message: `VFS structure created successfully (${result.files.length} files, ${result.directories.length} directories)`,
|
|
|
|
|
processed: totalOperations,
|
|
|
|
|
total: totalOperations
|
|
|
|
|
})
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.duration = Date.now() - startTime
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Group entities by strategy
|
|
|
|
|
*/
|
|
|
|
|
private groupEntities(
|
|
|
|
|
importResult: SmartExcelResult,
|
|
|
|
|
options: VFSStructureOptions
|
|
|
|
|
): Map<string, typeof importResult.rows> {
|
|
|
|
|
const groups = new Map<string, typeof importResult.rows>()
|
|
|
|
|
|
2025-10-22 17:36:27 -07:00
|
|
|
// Handle sheet-based grouping (v4.2.0)
|
|
|
|
|
if (options.groupBy === 'sheet' && importResult.sheets && importResult.sheets.length > 0) {
|
|
|
|
|
for (const sheet of importResult.sheets) {
|
|
|
|
|
groups.set(sheet.name, sheet.rows)
|
|
|
|
|
}
|
|
|
|
|
return groups
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle other grouping strategies
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
for (const extracted of importResult.rows) {
|
|
|
|
|
let groupName: string
|
|
|
|
|
|
|
|
|
|
switch (options.groupBy) {
|
|
|
|
|
case 'type':
|
|
|
|
|
groupName = this.getTypeGroupName(extracted.entity.type)
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case 'flat':
|
|
|
|
|
groupName = 'entities'
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case 'custom':
|
|
|
|
|
groupName = options.customGrouping ?
|
|
|
|
|
options.customGrouping(extracted.entity) :
|
|
|
|
|
'entities'
|
|
|
|
|
break
|
|
|
|
|
|
2025-10-22 17:36:27 -07:00
|
|
|
case 'sheet':
|
|
|
|
|
// Fallback if sheets data not available
|
|
|
|
|
groupName = 'entities'
|
|
|
|
|
break
|
|
|
|
|
|
feat: add unified import system with auto-detection and dual storage
Implemented a comprehensive unified import system that revolutionizes how data flows into Brainy:
## Core Features (Phase 1)
- Auto-detection of file formats (Excel, PDF, CSV, JSON, Markdown) via magic bytes and content analysis
- Dual storage architecture: creates both VFS files AND knowledge graph entities
- Single unified API: brain.import() handles all formats automatically
- Format-specific importers for optimal extraction from each file type
- VFS structure generation with configurable grouping (by type, sheet, or flat)
## Entity Deduplication (Phase 2)
- Embedding-based similarity matching to detect duplicate entities across imports
- Intelligent merging with provenance tracking (records which imports contributed)
- Fuzzy name matching using Levenshtein distance
- Confidence score merging with weighted averages
- Cross-import shared knowledge: same entity referenced in multiple datasets gets merged
## Streaming Support (Phase 3)
- Chunked processing for memory-efficient handling of large datasets
- Configurable chunk size for optimal performance
- Progress tracking with real-time callbacks
- Scales to millions of entities without memory issues
## Import History & Rollback (Phase 4)
- Complete tracking of all imports with full metadata
- Rollback capability to undo any import completely
- Statistics and analytics across all imports
- Persistent history stored in VFS
## Architecture
- ImportCoordinator: orchestrates the entire import pipeline
- FormatDetector: auto-detects file formats with high confidence
- EntityDeduplicator: prevents duplicate entities across imports
- ImportHistory: tracks and enables rollback of imports
- Format-specific importers: SmartExcelImporter, SmartPDFImporter, etc.
- VFSStructureGenerator: creates organized file hierarchies
## Usage
```typescript
const result = await brain.import('/path/to/file.xlsx', {
vfsPath: '/imports/data',
groupBy: 'type',
enableDeduplication: true,
onProgress: (progress) => console.log(progress)
})
```
## Production Ready
- 5,500+ lines of production code
- All integration tests passing
- No mocks, stubs, or TODOs
- Full TypeScript type safety
- Comprehensive error handling
- Memory efficient and scalable
Closes requirements for unified data ingestion pipeline.
2025-10-08 16:55:30 -07:00
|
|
|
default:
|
|
|
|
|
groupName = 'entities'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!groups.has(groupName)) {
|
|
|
|
|
groups.set(groupName, [])
|
|
|
|
|
}
|
|
|
|
|
groups.get(groupName)!.push(extracted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groups
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get directory name for entity type
|
|
|
|
|
*/
|
|
|
|
|
private getTypeGroupName(type: NounType): string {
|
|
|
|
|
const typeMap: Record<string, string> = {
|
|
|
|
|
[NounType.Person]: 'Characters',
|
|
|
|
|
[NounType.Location]: 'Places',
|
|
|
|
|
[NounType.Organization]: 'Organizations',
|
|
|
|
|
[NounType.Concept]: 'Concepts',
|
|
|
|
|
[NounType.Event]: 'Events',
|
|
|
|
|
[NounType.Product]: 'Items',
|
|
|
|
|
[NounType.Document]: 'Documents',
|
|
|
|
|
[NounType.Project]: 'Projects',
|
|
|
|
|
[NounType.Thing]: 'Other'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return typeMap[type as string] || 'Other'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sanitize filename
|
|
|
|
|
*/
|
|
|
|
|
private sanitizeFilename(name: string): string {
|
|
|
|
|
return name
|
|
|
|
|
.replace(/[<>:"/\\|?*]/g, '_') // Replace invalid chars
|
|
|
|
|
.replace(/\s+/g, '_') // Replace spaces
|
|
|
|
|
.replace(/_{2,}/g, '_') // Collapse multiple underscores
|
|
|
|
|
.substring(0, 200) // Limit length
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get file extension
|
|
|
|
|
*/
|
|
|
|
|
private getExtension(filename: string): string {
|
|
|
|
|
const lastDot = filename.lastIndexOf('.')
|
|
|
|
|
return lastDot !== -1 ? filename.substring(lastDot) : '.bin'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Group items by property
|
|
|
|
|
*/
|
|
|
|
|
private groupByType<T extends Record<string, any>>(
|
|
|
|
|
items: T[],
|
|
|
|
|
property: keyof T
|
|
|
|
|
): Record<string, number> {
|
|
|
|
|
const groups: Record<string, number> = {}
|
|
|
|
|
|
|
|
|
|
for (const item of items) {
|
|
|
|
|
const key = String(item[property])
|
|
|
|
|
groups[key] = (groups[key] || 0) + 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groups
|
|
|
|
|
}
|
|
|
|
|
}
|