@soulcraft/brainy 4.11.2 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CHANGELOG.md +271 -0
  2. package/README.md +38 -1
  3. package/dist/augmentations/brainyAugmentation.d.ts +76 -0
  4. package/dist/augmentations/brainyAugmentation.js +126 -0
  5. package/dist/augmentations/cacheAugmentation.js +9 -4
  6. package/dist/brainy.d.ts +248 -15
  7. package/dist/brainy.js +707 -17
  8. package/dist/cli/commands/cow.d.ts +60 -0
  9. package/dist/cli/commands/cow.js +444 -0
  10. package/dist/cli/commands/import.js +1 -1
  11. package/dist/cli/commands/vfs.js +24 -40
  12. package/dist/cli/index.js +50 -0
  13. package/dist/hnsw/hnswIndex.d.ts +41 -0
  14. package/dist/hnsw/hnswIndex.js +96 -1
  15. package/dist/hnsw/typeAwareHNSWIndex.d.ts +9 -0
  16. package/dist/hnsw/typeAwareHNSWIndex.js +22 -0
  17. package/dist/import/ImportHistory.js +3 -3
  18. package/dist/importers/VFSStructureGenerator.d.ts +1 -1
  19. package/dist/importers/VFSStructureGenerator.js +3 -3
  20. package/dist/index.d.ts +6 -0
  21. package/dist/index.js +10 -0
  22. package/dist/storage/adapters/memoryStorage.d.ts +6 -0
  23. package/dist/storage/adapters/memoryStorage.js +39 -14
  24. package/dist/storage/adapters/typeAwareStorageAdapter.d.ts +31 -1
  25. package/dist/storage/adapters/typeAwareStorageAdapter.js +272 -43
  26. package/dist/storage/baseStorage.d.ts +64 -0
  27. package/dist/storage/baseStorage.js +252 -12
  28. package/dist/storage/cow/BlobStorage.d.ts +232 -0
  29. package/dist/storage/cow/BlobStorage.js +437 -0
  30. package/dist/storage/cow/CommitLog.d.ts +199 -0
  31. package/dist/storage/cow/CommitLog.js +363 -0
  32. package/dist/storage/cow/CommitObject.d.ts +276 -0
  33. package/dist/storage/cow/CommitObject.js +431 -0
  34. package/dist/storage/cow/RefManager.d.ts +213 -0
  35. package/dist/storage/cow/RefManager.js +409 -0
  36. package/dist/storage/cow/TreeObject.d.ts +177 -0
  37. package/dist/storage/cow/TreeObject.js +293 -0
  38. package/dist/storage/storageFactory.d.ts +6 -0
  39. package/dist/storage/storageFactory.js +92 -74
  40. package/dist/types/brainy.types.d.ts +1 -0
  41. package/dist/vfs/FSCompat.d.ts +1 -1
  42. package/dist/vfs/FSCompat.js +1 -1
  43. package/dist/vfs/VirtualFileSystem.js +5 -6
  44. package/package.json +1 -1
package/dist/brainy.d.ts CHANGED
@@ -717,6 +717,231 @@ export declare class Brainy<T = any> implements BrainyInterface<T> {
717
717
  * Clear all data from the database
718
718
  */
719
719
  clear(): Promise<void>;
720
+ /**
721
+ * Fork the brain (instant clone via Snowflake-style COW)
722
+ *
723
+ * Creates a shallow copy in <100ms using copy-on-write (COW) technology.
724
+ * Fork shares storage and HNSW data structures with parent, copying only
725
+ * when modified (lazy deep copy).
726
+ *
727
+ * **How It Works (v5.0.0)**:
728
+ * 1. HNSW Index: Shallow copy via `enableCOW()` (~10ms for 1M+ nodes)
729
+ * 2. Metadata Index: Fast rebuild from shared storage (<100ms)
730
+ * 3. Graph Index: Fast rebuild from shared storage (<500ms)
731
+ *
732
+ * **Performance**:
733
+ * - Fork time: <100ms @ 10K entities (MEASURED)
734
+ * - Memory overhead: 10-20% (shared HNSW nodes)
735
+ * - Storage overhead: 10-20% (shared blobs)
736
+ *
737
+ * **Write Isolation**: Changes in fork don't affect parent, and vice versa.
738
+ *
739
+ * @param branch - Optional branch name (auto-generated if not provided)
740
+ * @param options - Optional fork metadata (author, message)
741
+ * @returns New Brainy instance (forked, fully independent)
742
+ *
743
+ * @example
744
+ * ```typescript
745
+ * const brain = new Brainy()
746
+ * await brain.init()
747
+ *
748
+ * // Add data to parent
749
+ * await brain.add({ type: 'user', data: { name: 'Alice' } })
750
+ *
751
+ * // Fork instantly (<100ms)
752
+ * const experiment = await brain.fork('test-migration')
753
+ *
754
+ * // Make changes safely in fork
755
+ * await experiment.add({ type: 'user', data: { name: 'Bob' } })
756
+ *
757
+ * // Original untouched
758
+ * console.log((await brain.find({})).length) // 1 (Alice)
759
+ * console.log((await experiment.find({})).length) // 2 (Alice + Bob)
760
+ * ```
761
+ *
762
+ * @since v5.0.0
763
+ */
764
+ fork(branch?: string, options?: {
765
+ author?: string;
766
+ message?: string;
767
+ metadata?: Record<string, any>;
768
+ }): Promise<Brainy<T>>;
769
+ /**
770
+ * List all branches/forks
771
+ * @returns Array of branch names
772
+ *
773
+ * @example
774
+ * ```typescript
775
+ * const branches = await brain.listBranches()
776
+ * console.log(branches) // ['main', 'experiment', 'backup']
777
+ * ```
778
+ */
779
+ listBranches(): Promise<string[]>;
780
+ /**
781
+ * Get current branch name
782
+ * @returns Current branch name
783
+ *
784
+ * @example
785
+ * ```typescript
786
+ * const current = await brain.getCurrentBranch()
787
+ * console.log(current) // 'main'
788
+ * ```
789
+ */
790
+ getCurrentBranch(): Promise<string>;
791
+ /**
792
+ * Switch to a different branch
793
+ * @param branch - Branch name to switch to
794
+ *
795
+ * @example
796
+ * ```typescript
797
+ * await brain.checkout('experiment')
798
+ * console.log(await brain.getCurrentBranch()) // 'experiment'
799
+ * ```
800
+ */
801
+ checkout(branch: string): Promise<void>;
802
+ /**
803
+ * Create a commit with current state
804
+ * @param options - Commit options (message, author, metadata)
805
+ * @returns Commit hash
806
+ *
807
+ * @example
808
+ * ```typescript
809
+ * await brain.add({ noun: 'user', data: { name: 'Alice' } })
810
+ * const commitHash = await brain.commit({
811
+ * message: 'Add Alice user',
812
+ * author: 'dev@example.com'
813
+ * })
814
+ * ```
815
+ */
816
+ commit(options?: {
817
+ message?: string;
818
+ author?: string;
819
+ metadata?: Record<string, any>;
820
+ }): Promise<string>;
821
+ /**
822
+ * Merge a source branch into target branch
823
+ * @param sourceBranch - Branch to merge from
824
+ * @param targetBranch - Branch to merge into
825
+ * @param options - Merge options (strategy, author, onConflict)
826
+ * @returns Merge result with statistics
827
+ *
828
+ * @example
829
+ * ```typescript
830
+ * const result = await brain.merge('experiment', 'main', {
831
+ * strategy: 'last-write-wins',
832
+ * author: 'dev@example.com'
833
+ * })
834
+ * console.log(result) // { added: 5, modified: 3, deleted: 1, conflicts: 0 }
835
+ * ```
836
+ */
837
+ merge(sourceBranch: string, targetBranch: string, options?: {
838
+ strategy?: 'last-write-wins' | 'first-write-wins' | 'custom';
839
+ author?: string;
840
+ onConflict?: (entityA: any, entityB: any) => Promise<any>;
841
+ }): Promise<{
842
+ added: number;
843
+ modified: number;
844
+ deleted: number;
845
+ conflicts: number;
846
+ }>;
847
+ /**
848
+ * Compare differences between two branches (like git diff)
849
+ * @param sourceBranch - Branch to compare from (defaults to current branch)
850
+ * @param targetBranch - Branch to compare to (defaults to 'main')
851
+ * @returns Diff result showing added, modified, and deleted entities/relationships
852
+ *
853
+ * @example
854
+ * ```typescript
855
+ * // Compare current branch with main
856
+ * const diff = await brain.diff()
857
+ *
858
+ * // Compare two specific branches
859
+ * const diff = await brain.diff('experiment', 'main')
860
+ * console.log(diff)
861
+ * // {
862
+ * // entities: { added: 5, modified: 3, deleted: 1 },
863
+ * // relationships: { added: 10, modified: 2, deleted: 0 }
864
+ * // }
865
+ * ```
866
+ */
867
+ diff(sourceBranch?: string, targetBranch?: string): Promise<{
868
+ entities: {
869
+ added: Array<{
870
+ id: string;
871
+ type: string;
872
+ data?: any;
873
+ }>;
874
+ modified: Array<{
875
+ id: string;
876
+ type: string;
877
+ changes: string[];
878
+ }>;
879
+ deleted: Array<{
880
+ id: string;
881
+ type: string;
882
+ }>;
883
+ };
884
+ relationships: {
885
+ added: Array<{
886
+ from: string;
887
+ to: string;
888
+ type: string;
889
+ }>;
890
+ modified: Array<{
891
+ from: string;
892
+ to: string;
893
+ type: string;
894
+ changes: string[];
895
+ }>;
896
+ deleted: Array<{
897
+ from: string;
898
+ to: string;
899
+ type: string;
900
+ }>;
901
+ };
902
+ summary: {
903
+ entitiesAdded: number;
904
+ entitiesModified: number;
905
+ entitiesDeleted: number;
906
+ relationshipsAdded: number;
907
+ relationshipsModified: number;
908
+ relationshipsDeleted: number;
909
+ };
910
+ }>;
911
+ /**
912
+ * Delete a branch/fork
913
+ * @param branch - Branch name to delete
914
+ *
915
+ * @example
916
+ * ```typescript
917
+ * await brain.deleteBranch('old-experiment')
918
+ * ```
919
+ */
920
+ deleteBranch(branch: string): Promise<void>;
921
+ /**
922
+ * Get commit history for current branch
923
+ * @param options - History options (limit, offset, author)
924
+ * @returns Array of commits
925
+ *
926
+ * @example
927
+ * ```typescript
928
+ * const history = await brain.getHistory({ limit: 10 })
929
+ * history.forEach(commit => {
930
+ * console.log(`${commit.hash}: ${commit.message}`)
931
+ * })
932
+ * ```
933
+ */
934
+ getHistory(options?: {
935
+ limit?: number;
936
+ offset?: number;
937
+ author?: string;
938
+ }): Promise<Array<{
939
+ hash: string;
940
+ message: string;
941
+ author: string;
942
+ timestamp: number;
943
+ metadata?: Record<string, any>;
944
+ }>>;
720
945
  /**
721
946
  * Get total count of nouns - O(1) operation
722
947
  * @returns Promise that resolves to the total number of nouns
@@ -911,35 +1136,43 @@ export declare class Brainy<T = any> implements BrainyInterface<T> {
911
1136
  }) => void;
912
1137
  }): Promise<import("./import/ImportCoordinator.js").ImportResult>;
913
1138
  /**
914
- * Virtual File System API - Knowledge Operating System
1139
+ * Virtual File System API - Knowledge Operating System (v5.0.1+)
915
1140
  *
916
- * Returns a cached VFS instance. You must call vfs.init() before use:
1141
+ * Returns a cached VFS instance that is auto-initialized during brain.init().
1142
+ * No separate initialization needed!
917
1143
  *
918
1144
  * @example After import
919
1145
  * ```typescript
920
1146
  * await brain.import('./data.xlsx', { vfsPath: '/imports/data' })
921
- *
922
- * const vfs = brain.vfs()
923
- * await vfs.init() // Required! (safe to call multiple times)
924
- * const files = await vfs.readdir('/imports/data')
1147
+ * // VFS ready immediately - no init() call needed!
1148
+ * const files = await brain.vfs.readdir('/imports/data')
925
1149
  * ```
926
1150
  *
927
1151
  * @example Direct VFS usage
928
1152
  * ```typescript
929
- * const vfs = brain.vfs()
930
- * await vfs.init() // Always required before first use
931
- * await vfs.writeFile('/docs/readme.md', 'Hello World')
932
- * const content = await vfs.readFile('/docs/readme.md')
1153
+ * await brain.init() // VFS auto-initialized here!
1154
+ * await brain.vfs.writeFile('/docs/readme.md', 'Hello World')
1155
+ * const content = await brain.vfs.readFile('/docs/readme.md')
933
1156
  * ```
934
1157
  *
935
- * **Note:** brain.import() automatically initializes the VFS, so after
936
- * an import you can call vfs.init() again (it's idempotent) and immediately
937
- * query the imported files.
1158
+ * @example With fork (COW isolation)
1159
+ * ```typescript
1160
+ * await brain.init()
1161
+ * await brain.vfs.writeFile('/config.json', '{"v": 1}')
1162
+ *
1163
+ * const fork = await brain.fork('experiment')
1164
+ * // Fork inherits parent's files
1165
+ * const config = await fork.vfs.readFile('/config.json')
1166
+ * // Fork modifications are isolated
1167
+ * await fork.vfs.writeFile('/test.txt', 'Fork only')
1168
+ * ```
938
1169
  *
939
- * **Pattern:** The VFS instance is cached, so multiple calls to brain.vfs()
1170
+ * **Pattern:** The VFS instance is cached, so multiple calls to brain.vfs
940
1171
  * return the same instance. This ensures import and user code share state.
1172
+ *
1173
+ * @since v5.0.1 - Auto-initialization during brain.init()
941
1174
  */
942
- vfs(): VirtualFileSystem;
1175
+ get vfs(): VirtualFileSystem;
943
1176
  /**
944
1177
  * Data Management API - backup, restore, import, export
945
1178
  */