@soulcraft/brainy 5.10.4 → 5.11.1
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.
- package/CHANGELOG.md +101 -0
- package/dist/brainy.d.ts +160 -2
- package/dist/brainy.js +276 -11
- package/dist/storage/adapters/azureBlobStorage.d.ts +2 -5
- package/dist/storage/adapters/azureBlobStorage.js +4 -40
- package/dist/storage/adapters/fileSystemStorage.d.ts +2 -5
- package/dist/storage/adapters/fileSystemStorage.js +4 -42
- package/dist/storage/adapters/gcsStorage.d.ts +2 -5
- package/dist/storage/adapters/gcsStorage.js +10 -47
- package/dist/storage/adapters/historicalStorageAdapter.d.ts +2 -5
- package/dist/storage/adapters/historicalStorageAdapter.js +2 -9
- package/dist/storage/adapters/memoryStorage.d.ts +2 -5
- package/dist/storage/adapters/memoryStorage.js +2 -10
- package/dist/storage/adapters/opfsStorage.d.ts +2 -5
- package/dist/storage/adapters/opfsStorage.js +12 -54
- package/dist/storage/adapters/r2Storage.d.ts +0 -13
- package/dist/storage/adapters/r2Storage.js +18 -53
- package/dist/storage/adapters/s3CompatibleStorage.d.ts +2 -5
- package/dist/storage/adapters/s3CompatibleStorage.js +18 -70
- package/dist/storage/baseStorage.d.ts +46 -16
- package/dist/storage/baseStorage.js +54 -42
- package/dist/storage/cow/CommitLog.d.ts +24 -0
- package/dist/storage/cow/CommitLog.js +37 -0
- package/dist/types/brainy.types.d.ts +59 -0
- package/dist/utils/paramValidation.d.ts +43 -0
- package/dist/utils/paramValidation.js +135 -22
- package/package.json +1 -1
|
@@ -89,6 +89,16 @@ export class S3CompatibleStorage extends BaseStorage {
|
|
|
89
89
|
this.hnswLocks = new Map();
|
|
90
90
|
// Node cache to avoid redundant API calls
|
|
91
91
|
this.nodeCache = new Map();
|
|
92
|
+
/**
|
|
93
|
+
* Check if COW has been explicitly disabled via clear()
|
|
94
|
+
* v5.10.4: Fixes bug where clear() doesn't persist across instance restarts
|
|
95
|
+
* @returns true if marker object exists, false otherwise
|
|
96
|
+
* @protected
|
|
97
|
+
*/
|
|
98
|
+
/**
|
|
99
|
+
* v5.11.0: Removed checkClearMarker() and createClearMarker() methods
|
|
100
|
+
* COW is now always enabled - marker files are no longer used
|
|
101
|
+
*/
|
|
92
102
|
// Batch update timer ID
|
|
93
103
|
this.statisticsBatchUpdateTimerId = null;
|
|
94
104
|
// Flag to indicate if statistics have been modified since last save
|
|
@@ -1611,32 +1621,18 @@ export class S3CompatibleStorage extends BaseStorage {
|
|
|
1611
1621
|
}
|
|
1612
1622
|
}
|
|
1613
1623
|
};
|
|
1614
|
-
//
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
// Delete all objects in the noun metadata directory
|
|
1619
|
-
await deleteObjectsWithPrefix(this.metadataPrefix);
|
|
1620
|
-
// Delete all objects in the verb metadata directory
|
|
1621
|
-
await deleteObjectsWithPrefix(this.verbMetadataPrefix);
|
|
1622
|
-
// Delete all objects in the index directory
|
|
1623
|
-
await deleteObjectsWithPrefix(this.indexPrefix);
|
|
1624
|
-
// v5.6.1: Delete COW (copy-on-write) version control data
|
|
1625
|
-
// This includes all git-like versioning data (commits, trees, blobs, refs)
|
|
1626
|
-
// Must be deleted to fully clear all data including version history
|
|
1624
|
+
// v5.11.0: Clear ALL data using correct paths
|
|
1625
|
+
// Delete entire branches/ directory (includes ALL entities, ALL types, ALL VFS data, ALL forks)
|
|
1626
|
+
await deleteObjectsWithPrefix('branches/');
|
|
1627
|
+
// Delete COW version control data
|
|
1627
1628
|
await deleteObjectsWithPrefix('_cow/');
|
|
1628
|
-
//
|
|
1629
|
-
|
|
1630
|
-
//
|
|
1629
|
+
// Delete system metadata
|
|
1630
|
+
await deleteObjectsWithPrefix('_system/');
|
|
1631
|
+
// v5.11.0: Reset COW managers (but don't disable COW - it's always enabled)
|
|
1632
|
+
// COW will re-initialize automatically on next use
|
|
1631
1633
|
this.refManager = undefined;
|
|
1632
1634
|
this.blobStorage = undefined;
|
|
1633
1635
|
this.commitLog = undefined;
|
|
1634
|
-
this.cowEnabled = false;
|
|
1635
|
-
// v5.10.4: Create persistent marker object (CRITICAL FIX)
|
|
1636
|
-
// Bug: cowEnabled = false only affects current instance, not future instances
|
|
1637
|
-
// Fix: Create marker object that persists across instance restarts
|
|
1638
|
-
// When new instance calls initializeCOW(), it checks for this marker
|
|
1639
|
-
await this.createClearMarker();
|
|
1640
1636
|
// Clear the statistics cache
|
|
1641
1637
|
this.statisticsCache = null;
|
|
1642
1638
|
this.statisticsModified = false;
|
|
@@ -1736,54 +1732,6 @@ export class S3CompatibleStorage extends BaseStorage {
|
|
|
1736
1732
|
};
|
|
1737
1733
|
}
|
|
1738
1734
|
}
|
|
1739
|
-
/**
|
|
1740
|
-
* Check if COW has been explicitly disabled via clear()
|
|
1741
|
-
* v5.10.4: Fixes bug where clear() doesn't persist across instance restarts
|
|
1742
|
-
* @returns true if marker object exists, false otherwise
|
|
1743
|
-
* @protected
|
|
1744
|
-
*/
|
|
1745
|
-
async checkClearMarker() {
|
|
1746
|
-
await this.ensureInitialized();
|
|
1747
|
-
try {
|
|
1748
|
-
const { HeadObjectCommand } = await import('@aws-sdk/client-s3');
|
|
1749
|
-
const markerKey = `${this.systemPrefix}cow-disabled`;
|
|
1750
|
-
await this.s3Client.send(new HeadObjectCommand({
|
|
1751
|
-
Bucket: this.bucketName,
|
|
1752
|
-
Key: markerKey
|
|
1753
|
-
}));
|
|
1754
|
-
return true; // Marker exists
|
|
1755
|
-
}
|
|
1756
|
-
catch (error) {
|
|
1757
|
-
if (error.name === 'NotFound' || error.$metadata?.httpStatusCode === 404) {
|
|
1758
|
-
return false; // Marker doesn't exist
|
|
1759
|
-
}
|
|
1760
|
-
prodLog.warn('S3CompatibleStorage.checkClearMarker: Error checking marker', error);
|
|
1761
|
-
return false;
|
|
1762
|
-
}
|
|
1763
|
-
}
|
|
1764
|
-
/**
|
|
1765
|
-
* Create marker indicating COW has been explicitly disabled
|
|
1766
|
-
* v5.10.4: Called by clear() to prevent COW reinitialization on new instances
|
|
1767
|
-
* @protected
|
|
1768
|
-
*/
|
|
1769
|
-
async createClearMarker() {
|
|
1770
|
-
await this.ensureInitialized();
|
|
1771
|
-
try {
|
|
1772
|
-
const { PutObjectCommand } = await import('@aws-sdk/client-s3');
|
|
1773
|
-
const markerKey = `${this.systemPrefix}cow-disabled`;
|
|
1774
|
-
// Create empty marker object
|
|
1775
|
-
await this.s3Client.send(new PutObjectCommand({
|
|
1776
|
-
Bucket: this.bucketName,
|
|
1777
|
-
Key: markerKey,
|
|
1778
|
-
Body: Buffer.from(''),
|
|
1779
|
-
ContentType: 'text/plain'
|
|
1780
|
-
}));
|
|
1781
|
-
}
|
|
1782
|
-
catch (error) {
|
|
1783
|
-
prodLog.error('S3CompatibleStorage.createClearMarker: Failed to create marker object', error);
|
|
1784
|
-
// Don't throw - marker creation failure shouldn't break clear()
|
|
1785
|
-
}
|
|
1786
|
-
}
|
|
1787
1735
|
/**
|
|
1788
1736
|
* Get the statistics key for a specific date
|
|
1789
1737
|
* @param date The date to get the key for
|
|
@@ -58,7 +58,6 @@ export declare abstract class BaseStorage extends BaseStorageAdapter {
|
|
|
58
58
|
blobStorage?: BlobStorage;
|
|
59
59
|
commitLog?: CommitLog;
|
|
60
60
|
currentBranch: string;
|
|
61
|
-
protected cowEnabled: boolean;
|
|
62
61
|
protected nounCountsByType: Uint32Array<ArrayBuffer>;
|
|
63
62
|
protected verbCountsByType: Uint32Array<ArrayBuffer>;
|
|
64
63
|
protected nounTypeCache: Map<string, NounType>;
|
|
@@ -88,6 +87,8 @@ export declare abstract class BaseStorage extends BaseStorageAdapter {
|
|
|
88
87
|
* Called during init() to ensure all data is stored with branch prefixes from the start
|
|
89
88
|
* RefManager/BlobStorage/CommitLog are lazy-initialized on first fork()
|
|
90
89
|
* @param branch - Branch name to use (default: 'main')
|
|
90
|
+
*
|
|
91
|
+
* v5.11.0: COW is always enabled - this method now just sets the branch name (idempotent)
|
|
91
92
|
*/
|
|
92
93
|
enableCOWLightweight(branch?: string): void;
|
|
93
94
|
/**
|
|
@@ -119,6 +120,8 @@ export declare abstract class BaseStorage extends BaseStorageAdapter {
|
|
|
119
120
|
* Read object with inheritance from parent branches (COW layer)
|
|
120
121
|
* Tries current branch first, then walks commit history
|
|
121
122
|
* @protected - Available to subclasses for COW implementation
|
|
123
|
+
*
|
|
124
|
+
* v5.11.0: COW is always enabled - always use branch-scoped paths with inheritance
|
|
122
125
|
*/
|
|
123
126
|
protected readWithInheritance(path: string, branch?: string): Promise<any | null>;
|
|
124
127
|
/**
|
|
@@ -137,6 +140,8 @@ export declare abstract class BaseStorage extends BaseStorageAdapter {
|
|
|
137
140
|
* This enables fork to see parent's data in pagination operations
|
|
138
141
|
*
|
|
139
142
|
* Simplified approach: All branches inherit from main
|
|
143
|
+
*
|
|
144
|
+
* v5.11.0: COW is always enabled - always use inheritance
|
|
140
145
|
*/
|
|
141
146
|
protected listObjectsWithInheritance(prefix: string, branch?: string): Promise<string[]>;
|
|
142
147
|
/**
|
|
@@ -328,20 +333,9 @@ export declare abstract class BaseStorage extends BaseStorageAdapter {
|
|
|
328
333
|
*/
|
|
329
334
|
abstract clear(): Promise<void>;
|
|
330
335
|
/**
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
* Each adapter checks for a marker file/object (e.g., "_system/cow-disabled")
|
|
334
|
-
* @returns true if COW was disabled by clear(), false otherwise
|
|
335
|
-
* @protected
|
|
336
|
-
*/
|
|
337
|
-
protected abstract checkClearMarker(): Promise<boolean>;
|
|
338
|
-
/**
|
|
339
|
-
* Create marker indicating COW has been explicitly disabled
|
|
340
|
-
* v5.10.4: Called by clear() to prevent COW reinitialization on new instances
|
|
341
|
-
* Each adapter creates a marker file/object (e.g., "_system/cow-disabled")
|
|
342
|
-
* @protected
|
|
336
|
+
* v5.11.0: Removed checkClearMarker() and createClearMarker() abstract methods
|
|
337
|
+
* COW is now always enabled - marker files are no longer used
|
|
343
338
|
*/
|
|
344
|
-
protected abstract createClearMarker(): Promise<void>;
|
|
345
339
|
/**
|
|
346
340
|
* Get information about storage usage and capacity
|
|
347
341
|
* This method should be implemented by each specific adapter
|
|
@@ -410,8 +404,44 @@ export declare abstract class BaseStorage extends BaseStorageAdapter {
|
|
|
410
404
|
*/
|
|
411
405
|
protected saveNounMetadata_internal(id: string, metadata: NounMetadata): Promise<void>;
|
|
412
406
|
/**
|
|
413
|
-
* Get noun metadata from storage (
|
|
414
|
-
*
|
|
407
|
+
* Get noun metadata from storage (METADATA-ONLY, NO VECTORS)
|
|
408
|
+
*
|
|
409
|
+
* **Performance (v5.11.1)**: Fast path for metadata-only reads
|
|
410
|
+
* - **Speed**: 10ms vs 43ms (76-81% faster than getNoun)
|
|
411
|
+
* - **Bandwidth**: 300 bytes vs 6KB (95% less)
|
|
412
|
+
* - **Memory**: 300 bytes vs 6KB (87% less)
|
|
413
|
+
*
|
|
414
|
+
* **What's included**:
|
|
415
|
+
* - All entity metadata (data, type, timestamps, confidence, weight)
|
|
416
|
+
* - Custom user fields
|
|
417
|
+
* - VFS metadata (_vfs.path, _vfs.size, etc.)
|
|
418
|
+
*
|
|
419
|
+
* **What's excluded**:
|
|
420
|
+
* - 384-dimensional vector embeddings
|
|
421
|
+
* - HNSW graph connections
|
|
422
|
+
*
|
|
423
|
+
* **Usage**:
|
|
424
|
+
* - VFS operations (readFile, stat, readdir) - 100% of cases
|
|
425
|
+
* - Existence checks: `if (await storage.getNounMetadata(id))`
|
|
426
|
+
* - Metadata inspection: `metadata.data`, `metadata.noun` (type)
|
|
427
|
+
* - Relationship traversal: Just need IDs, not vectors
|
|
428
|
+
*
|
|
429
|
+
* **When to use getNoun() instead**:
|
|
430
|
+
* - Computing similarity on this specific entity
|
|
431
|
+
* - Manual vector operations
|
|
432
|
+
* - HNSW graph traversal
|
|
433
|
+
*
|
|
434
|
+
* @param id - Entity ID to retrieve metadata for
|
|
435
|
+
* @returns Metadata or null if not found
|
|
436
|
+
*
|
|
437
|
+
* @performance
|
|
438
|
+
* - Type cache O(1) lookup for cached entities
|
|
439
|
+
* - Type scan O(N_types) for cache misses (typically <100ms)
|
|
440
|
+
* - Uses readWithInheritance() for COW branch support
|
|
441
|
+
*
|
|
442
|
+
* @since v4.0.0
|
|
443
|
+
* @since v5.4.0 - Type-first paths
|
|
444
|
+
* @since v5.11.1 - Promoted to fast path for brain.get() optimization
|
|
415
445
|
*/
|
|
416
446
|
getNounMetadata(id: string): Promise<NounMetadata | null>;
|
|
417
447
|
/**
|
|
@@ -83,7 +83,7 @@ export class BaseStorage extends BaseStorageAdapter {
|
|
|
83
83
|
// Memory footprint: Bounded by batch size (typically <1000 items during imports)
|
|
84
84
|
this.writeCache = new Map();
|
|
85
85
|
this.currentBranch = 'main';
|
|
86
|
-
|
|
86
|
+
// v5.11.0: Removed cowEnabled flag - COW is ALWAYS enabled (mandatory, cannot be disabled)
|
|
87
87
|
// Type-first indexing support (v5.4.0)
|
|
88
88
|
// Built into all storage adapters for billion-scale efficiency
|
|
89
89
|
this.nounCountsByType = new Uint32Array(NOUN_TYPE_COUNT); // 168 bytes (Stage 3: 42 types)
|
|
@@ -191,13 +191,11 @@ export class BaseStorage extends BaseStorageAdapter {
|
|
|
191
191
|
* Called during init() to ensure all data is stored with branch prefixes from the start
|
|
192
192
|
* RefManager/BlobStorage/CommitLog are lazy-initialized on first fork()
|
|
193
193
|
* @param branch - Branch name to use (default: 'main')
|
|
194
|
+
*
|
|
195
|
+
* v5.11.0: COW is always enabled - this method now just sets the branch name (idempotent)
|
|
194
196
|
*/
|
|
195
197
|
enableCOWLightweight(branch = 'main') {
|
|
196
|
-
if (this.cowEnabled) {
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
198
|
this.currentBranch = branch;
|
|
200
|
-
this.cowEnabled = true;
|
|
201
199
|
// RefManager/BlobStorage/CommitLog remain undefined until first fork()
|
|
202
200
|
}
|
|
203
201
|
/**
|
|
@@ -212,27 +210,15 @@ export class BaseStorage extends BaseStorageAdapter {
|
|
|
212
210
|
* @returns Promise that resolves when COW is initialized
|
|
213
211
|
*/
|
|
214
212
|
async initializeCOW(options) {
|
|
215
|
-
// v5.
|
|
216
|
-
//
|
|
217
|
-
//
|
|
218
|
-
|
|
219
|
-
const markerExists = await this.checkClearMarker();
|
|
220
|
-
if (markerExists) {
|
|
221
|
-
return; // COW was disabled by clear() - don't recreate _cow/ directory
|
|
222
|
-
}
|
|
223
|
-
// v5.6.1: If COW was explicitly disabled (e.g., via clear()), don't reinitialize
|
|
224
|
-
// This prevents automatic recreation of COW data after clear() operations
|
|
225
|
-
if (this.cowEnabled === false) {
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
// Check if RefManager already initialized (full COW setup complete)
|
|
229
|
-
if (this.refManager) {
|
|
213
|
+
// v5.11.0: COW is ALWAYS enabled - idempotent initialization only
|
|
214
|
+
// Removed marker file check (cowEnabled flag removed, COW is mandatory)
|
|
215
|
+
// Check if RefManager already initialized (idempotent)
|
|
216
|
+
if (this.refManager && this.blobStorage && this.commitLog) {
|
|
230
217
|
return;
|
|
231
218
|
}
|
|
232
|
-
//
|
|
233
|
-
if (
|
|
234
|
-
this.currentBranch = options
|
|
235
|
-
this.cowEnabled = true;
|
|
219
|
+
// Set current branch if provided
|
|
220
|
+
if (options?.branch) {
|
|
221
|
+
this.currentBranch = options.branch;
|
|
236
222
|
}
|
|
237
223
|
// Create COWStorageAdapter bridge
|
|
238
224
|
// This adapts BaseStorage's methods to the simple key-value interface
|
|
@@ -334,7 +320,7 @@ export class BaseStorage extends BaseStorageAdapter {
|
|
|
334
320
|
await this.refManager.setHead(this.currentBranch);
|
|
335
321
|
}
|
|
336
322
|
}
|
|
337
|
-
|
|
323
|
+
// v5.11.0: COW is always enabled - no flag to set
|
|
338
324
|
}
|
|
339
325
|
/**
|
|
340
326
|
* Resolve branch-scoped path for COW isolation
|
|
@@ -348,9 +334,7 @@ export class BaseStorage extends BaseStorageAdapter {
|
|
|
348
334
|
if (basePath.startsWith('_cow/')) {
|
|
349
335
|
return basePath; // COW metadata is global across all branches
|
|
350
336
|
}
|
|
351
|
-
|
|
352
|
-
return basePath; // COW disabled, use direct path
|
|
353
|
-
}
|
|
337
|
+
// v5.11.0: COW is always enabled - always use branch-scoped paths
|
|
354
338
|
const targetBranch = branch || this.currentBranch || 'main';
|
|
355
339
|
// Branch-scoped path: branches/<branch>/<basePath>
|
|
356
340
|
return `branches/${targetBranch}/${basePath}`;
|
|
@@ -374,17 +358,10 @@ export class BaseStorage extends BaseStorageAdapter {
|
|
|
374
358
|
* Read object with inheritance from parent branches (COW layer)
|
|
375
359
|
* Tries current branch first, then walks commit history
|
|
376
360
|
* @protected - Available to subclasses for COW implementation
|
|
361
|
+
*
|
|
362
|
+
* v5.11.0: COW is always enabled - always use branch-scoped paths with inheritance
|
|
377
363
|
*/
|
|
378
364
|
async readWithInheritance(path, branch) {
|
|
379
|
-
if (!this.cowEnabled) {
|
|
380
|
-
// COW disabled: check write cache, then direct read
|
|
381
|
-
// v5.7.2: Check cache first for read-after-write consistency
|
|
382
|
-
const cachedData = this.writeCache.get(path);
|
|
383
|
-
if (cachedData !== undefined) {
|
|
384
|
-
return cachedData;
|
|
385
|
-
}
|
|
386
|
-
return this.readObjectFromPath(path);
|
|
387
|
-
}
|
|
388
365
|
const targetBranch = branch || this.currentBranch || 'main';
|
|
389
366
|
const branchPath = this.resolveBranchPath(path, targetBranch);
|
|
390
367
|
// v5.7.2: Check write cache FIRST (synchronous, instant)
|
|
@@ -461,11 +438,10 @@ export class BaseStorage extends BaseStorageAdapter {
|
|
|
461
438
|
* This enables fork to see parent's data in pagination operations
|
|
462
439
|
*
|
|
463
440
|
* Simplified approach: All branches inherit from main
|
|
441
|
+
*
|
|
442
|
+
* v5.11.0: COW is always enabled - always use inheritance
|
|
464
443
|
*/
|
|
465
444
|
async listObjectsWithInheritance(prefix, branch) {
|
|
466
|
-
if (!this.cowEnabled) {
|
|
467
|
-
return this.listObjectsInBranch(prefix, branch);
|
|
468
|
-
}
|
|
469
445
|
const targetBranch = branch || this.currentBranch || 'main';
|
|
470
446
|
// Collect paths from current branch
|
|
471
447
|
const pathsSet = new Set();
|
|
@@ -1433,8 +1409,44 @@ export class BaseStorage extends BaseStorageAdapter {
|
|
|
1433
1409
|
}
|
|
1434
1410
|
}
|
|
1435
1411
|
/**
|
|
1436
|
-
* Get noun metadata from storage (
|
|
1437
|
-
*
|
|
1412
|
+
* Get noun metadata from storage (METADATA-ONLY, NO VECTORS)
|
|
1413
|
+
*
|
|
1414
|
+
* **Performance (v5.11.1)**: Fast path for metadata-only reads
|
|
1415
|
+
* - **Speed**: 10ms vs 43ms (76-81% faster than getNoun)
|
|
1416
|
+
* - **Bandwidth**: 300 bytes vs 6KB (95% less)
|
|
1417
|
+
* - **Memory**: 300 bytes vs 6KB (87% less)
|
|
1418
|
+
*
|
|
1419
|
+
* **What's included**:
|
|
1420
|
+
* - All entity metadata (data, type, timestamps, confidence, weight)
|
|
1421
|
+
* - Custom user fields
|
|
1422
|
+
* - VFS metadata (_vfs.path, _vfs.size, etc.)
|
|
1423
|
+
*
|
|
1424
|
+
* **What's excluded**:
|
|
1425
|
+
* - 384-dimensional vector embeddings
|
|
1426
|
+
* - HNSW graph connections
|
|
1427
|
+
*
|
|
1428
|
+
* **Usage**:
|
|
1429
|
+
* - VFS operations (readFile, stat, readdir) - 100% of cases
|
|
1430
|
+
* - Existence checks: `if (await storage.getNounMetadata(id))`
|
|
1431
|
+
* - Metadata inspection: `metadata.data`, `metadata.noun` (type)
|
|
1432
|
+
* - Relationship traversal: Just need IDs, not vectors
|
|
1433
|
+
*
|
|
1434
|
+
* **When to use getNoun() instead**:
|
|
1435
|
+
* - Computing similarity on this specific entity
|
|
1436
|
+
* - Manual vector operations
|
|
1437
|
+
* - HNSW graph traversal
|
|
1438
|
+
*
|
|
1439
|
+
* @param id - Entity ID to retrieve metadata for
|
|
1440
|
+
* @returns Metadata or null if not found
|
|
1441
|
+
*
|
|
1442
|
+
* @performance
|
|
1443
|
+
* - Type cache O(1) lookup for cached entities
|
|
1444
|
+
* - Type scan O(N_types) for cache misses (typically <100ms)
|
|
1445
|
+
* - Uses readWithInheritance() for COW branch support
|
|
1446
|
+
*
|
|
1447
|
+
* @since v4.0.0
|
|
1448
|
+
* @since v5.4.0 - Type-first paths
|
|
1449
|
+
* @since v5.11.1 - Promoted to fast path for brain.get() optimization
|
|
1438
1450
|
*/
|
|
1439
1451
|
async getNounMetadata(id) {
|
|
1440
1452
|
await this.ensureInitialized();
|
|
@@ -115,6 +115,30 @@ export declare class CommitLog {
|
|
|
115
115
|
since?: number;
|
|
116
116
|
until?: number;
|
|
117
117
|
}): Promise<CommitObject[]>;
|
|
118
|
+
/**
|
|
119
|
+
* Stream commit history (memory-efficient for large histories)
|
|
120
|
+
*
|
|
121
|
+
* Yields commits one at a time without accumulating in memory.
|
|
122
|
+
* Use this for large commit histories (1000s of commits) where
|
|
123
|
+
* memory efficiency is important.
|
|
124
|
+
*
|
|
125
|
+
* @param ref - Starting ref
|
|
126
|
+
* @param options - Walk options
|
|
127
|
+
* @yields Commits in reverse chronological order (newest first)
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* // Stream all commits without memory accumulation
|
|
132
|
+
* for await (const commit of commitLog.streamHistory('main', { maxCount: 10000 })) {
|
|
133
|
+
* console.log(commit.message)
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
streamHistory(ref: string, options?: {
|
|
138
|
+
maxCount?: number;
|
|
139
|
+
since?: number;
|
|
140
|
+
until?: number;
|
|
141
|
+
}): AsyncIterableIterator<CommitObject>;
|
|
118
142
|
/**
|
|
119
143
|
* Count commits between two commits
|
|
120
144
|
*
|
|
@@ -183,6 +183,43 @@ export class CommitLog {
|
|
|
183
183
|
}
|
|
184
184
|
return commits;
|
|
185
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* Stream commit history (memory-efficient for large histories)
|
|
188
|
+
*
|
|
189
|
+
* Yields commits one at a time without accumulating in memory.
|
|
190
|
+
* Use this for large commit histories (1000s of commits) where
|
|
191
|
+
* memory efficiency is important.
|
|
192
|
+
*
|
|
193
|
+
* @param ref - Starting ref
|
|
194
|
+
* @param options - Walk options
|
|
195
|
+
* @yields Commits in reverse chronological order (newest first)
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* // Stream all commits without memory accumulation
|
|
200
|
+
* for await (const commit of commitLog.streamHistory('main', { maxCount: 10000 })) {
|
|
201
|
+
* console.log(commit.message)
|
|
202
|
+
* }
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
async *streamHistory(ref, options) {
|
|
206
|
+
let count = 0;
|
|
207
|
+
for await (const commit of this.walk(ref, {
|
|
208
|
+
maxDepth: options?.maxCount,
|
|
209
|
+
until: options?.until
|
|
210
|
+
})) {
|
|
211
|
+
// Filter by since timestamp if provided
|
|
212
|
+
if (options?.since && commit.timestamp < options.since) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
yield commit;
|
|
216
|
+
count++;
|
|
217
|
+
// Stop after maxCount commits
|
|
218
|
+
if (options?.maxCount && count >= options.maxCount) {
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
186
223
|
/**
|
|
187
224
|
* Count commits between two commits
|
|
188
225
|
*
|
|
@@ -421,6 +421,63 @@ export interface ImportResult {
|
|
|
421
421
|
error?: any;
|
|
422
422
|
}>;
|
|
423
423
|
}
|
|
424
|
+
/**
|
|
425
|
+
* Options for brain.get() entity retrieval
|
|
426
|
+
*
|
|
427
|
+
* **Performance Optimization (v5.11.1)**:
|
|
428
|
+
* By default, brain.get() loads ONLY metadata (not vectors), resulting in:
|
|
429
|
+
* - **76-81% faster** reads (10ms vs 43ms for metadata-only)
|
|
430
|
+
* - **95% less bandwidth** (300 bytes vs 6KB per entity)
|
|
431
|
+
* - **87% less memory** (optimal for VFS and large-scale operations)
|
|
432
|
+
*
|
|
433
|
+
* **When to use includeVectors**:
|
|
434
|
+
* - Computing similarity on a specific entity (not search): `brain.similar({ to: entity.vector })`
|
|
435
|
+
* - Manual vector operations: `cosineSimilarity(entity.vector, otherVector)`
|
|
436
|
+
* - Inspecting embeddings for debugging
|
|
437
|
+
*
|
|
438
|
+
* **When NOT to use includeVectors** (metadata-only is sufficient):
|
|
439
|
+
* - VFS operations (readFile, stat, readdir) - 100% of cases
|
|
440
|
+
* - Existence checks: `if (await brain.get(id))`
|
|
441
|
+
* - Metadata inspection: `entity.metadata`, `entity.data`, `entity.type`
|
|
442
|
+
* - Relationship traversal: `brain.getRelations({ from: id })`
|
|
443
|
+
* - Search operations: `brain.find()` generates embeddings automatically
|
|
444
|
+
*
|
|
445
|
+
* @example
|
|
446
|
+
* ```typescript
|
|
447
|
+
* // ✅ FAST (default): Metadata-only - 10ms, 300 bytes
|
|
448
|
+
* const entity = await brain.get(id)
|
|
449
|
+
* console.log(entity.data, entity.metadata) // ✅ Available
|
|
450
|
+
* console.log(entity.vector) // Empty Float32Array (stub)
|
|
451
|
+
*
|
|
452
|
+
* // ✅ FULL: Load vectors when needed - 43ms, 6KB
|
|
453
|
+
* const fullEntity = await brain.get(id, { includeVectors: true })
|
|
454
|
+
* const similarity = cosineSimilarity(fullEntity.vector, otherVector)
|
|
455
|
+
*
|
|
456
|
+
* // ✅ VFS automatically uses fast path (no change needed)
|
|
457
|
+
* await vfs.readFile('/file.txt') // 53ms → 10ms (81% faster)
|
|
458
|
+
* ```
|
|
459
|
+
*
|
|
460
|
+
* @since v5.11.1
|
|
461
|
+
*/
|
|
462
|
+
export interface GetOptions {
|
|
463
|
+
/**
|
|
464
|
+
* Include 384-dimensional vector embeddings in the response
|
|
465
|
+
*
|
|
466
|
+
* **Default: false** (metadata-only for 76-81% speedup)
|
|
467
|
+
*
|
|
468
|
+
* Set to `true` when you need to:
|
|
469
|
+
* - Compute similarity on this specific entity's vector
|
|
470
|
+
* - Perform manual vector operations
|
|
471
|
+
* - Inspect embeddings for debugging
|
|
472
|
+
*
|
|
473
|
+
* **Note**: Search operations (`brain.find()`) generate vectors automatically,
|
|
474
|
+
* so you don't need this flag for search. Only for direct vector operations
|
|
475
|
+
* on a retrieved entity.
|
|
476
|
+
*
|
|
477
|
+
* @default false
|
|
478
|
+
*/
|
|
479
|
+
includeVectors?: boolean;
|
|
480
|
+
}
|
|
424
481
|
/**
|
|
425
482
|
* Graph traversal parameters
|
|
426
483
|
*/
|
|
@@ -494,6 +551,8 @@ export interface BrainyConfig {
|
|
|
494
551
|
disableAutoOptimize?: boolean;
|
|
495
552
|
batchWrites?: boolean;
|
|
496
553
|
maxConcurrentOperations?: number;
|
|
554
|
+
maxQueryLimit?: number;
|
|
555
|
+
reservedQueryMemory?: number;
|
|
497
556
|
verbose?: boolean;
|
|
498
557
|
silent?: boolean;
|
|
499
558
|
}
|
|
@@ -5,6 +5,49 @@
|
|
|
5
5
|
* Only enforces universal truths, learns everything else
|
|
6
6
|
*/
|
|
7
7
|
import { FindParams, AddParams, UpdateParams, RelateParams } from '../types/brainy.types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Configuration options for ValidationConfig
|
|
10
|
+
*/
|
|
11
|
+
export interface ValidationConfigOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Explicit maximum query limit override
|
|
14
|
+
* Bypasses all auto-detection
|
|
15
|
+
*/
|
|
16
|
+
maxQueryLimit?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Memory reserved for query operations (in bytes)
|
|
19
|
+
* Bypasses auto-detection but still applies safety limits
|
|
20
|
+
*/
|
|
21
|
+
reservedQueryMemory?: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Auto-configured limits based on system resources
|
|
25
|
+
* These adapt to available memory and observed performance
|
|
26
|
+
*/
|
|
27
|
+
export declare class ValidationConfig {
|
|
28
|
+
private static instance;
|
|
29
|
+
maxLimit: number;
|
|
30
|
+
maxQueryLength: number;
|
|
31
|
+
maxVectorDimensions: number;
|
|
32
|
+
limitBasis: 'override' | 'reservedMemory' | 'containerMemory' | 'freeMemory';
|
|
33
|
+
detectedContainerLimit: number | null;
|
|
34
|
+
private avgQueryTime;
|
|
35
|
+
private queryCount;
|
|
36
|
+
private constructor();
|
|
37
|
+
static getInstance(options?: ValidationConfigOptions): ValidationConfig;
|
|
38
|
+
/**
|
|
39
|
+
* Reset singleton (for testing or reconfiguration)
|
|
40
|
+
*/
|
|
41
|
+
static reset(): void;
|
|
42
|
+
/**
|
|
43
|
+
* Reconfigure with new options
|
|
44
|
+
*/
|
|
45
|
+
static reconfigure(options: ValidationConfigOptions): ValidationConfig;
|
|
46
|
+
/**
|
|
47
|
+
* Learn from actual usage to adjust limits
|
|
48
|
+
*/
|
|
49
|
+
recordQuery(duration: number, resultCount: number): void;
|
|
50
|
+
}
|
|
8
51
|
/**
|
|
9
52
|
* Universal validations - things that are always invalid
|
|
10
53
|
* These are mathematical/logical truths, not configuration
|