@soulcraft/brainy 4.10.3 → 4.10.4

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.
@@ -3,6 +3,7 @@
3
3
  * Provides common functionality for all storage adapters, including statistics tracking
4
4
  */
5
5
  import { StatisticsData, StorageAdapter, HNSWNoun, HNSWVerb, HNSWNounWithMetadata, HNSWVerbWithMetadata, NounMetadata, VerbMetadata } from '../../coreTypes.js';
6
+ import { StorageBatchConfig } from '../baseStorage.js';
6
7
  /**
7
8
  * Base class for storage adapters that implements statistics tracking
8
9
  */
@@ -50,6 +51,18 @@ export declare abstract class BaseStorageAdapter implements StorageAdapter {
50
51
  quota: number | null;
51
52
  details?: Record<string, any>;
52
53
  }>;
54
+ /**
55
+ * Get optimal batch configuration for this storage adapter
56
+ * Override in subclasses to provide storage-specific optimization
57
+ *
58
+ * This method allows each storage adapter to declare its optimal batch behavior
59
+ * for rate limiting and performance. The configuration is used by addMany(),
60
+ * relateMany(), and import operations to automatically adapt to storage capabilities.
61
+ *
62
+ * @returns Batch configuration optimized for this storage type
63
+ * @since v4.11.0
64
+ */
65
+ getBatchConfig(): StorageBatchConfig;
53
66
  /**
54
67
  * Get nouns with pagination and filtering
55
68
  * @param options Pagination and filtering options
@@ -60,6 +60,32 @@ export class BaseStorageAdapter {
60
60
  this.countPersistBatchSize = 10; // Operations before forcing persist (cloud storage)
61
61
  this.countPersistInterval = 5000; // Milliseconds before forcing persist (cloud storage)
62
62
  }
63
+ /**
64
+ * Get optimal batch configuration for this storage adapter
65
+ * Override in subclasses to provide storage-specific optimization
66
+ *
67
+ * This method allows each storage adapter to declare its optimal batch behavior
68
+ * for rate limiting and performance. The configuration is used by addMany(),
69
+ * relateMany(), and import operations to automatically adapt to storage capabilities.
70
+ *
71
+ * @returns Batch configuration optimized for this storage type
72
+ * @since v4.11.0
73
+ */
74
+ getBatchConfig() {
75
+ // Conservative defaults that work safely across all storage types
76
+ // Cloud storage adapters should override with higher throughput values
77
+ // Local storage adapters should override with no delays
78
+ return {
79
+ maxBatchSize: 50,
80
+ batchDelayMs: 100,
81
+ maxConcurrent: 50,
82
+ supportsParallelWrites: false,
83
+ rateLimit: {
84
+ operationsPerSecond: 100,
85
+ burstCapacity: 200
86
+ }
87
+ };
88
+ }
63
89
  /**
64
90
  * Save statistics data
65
91
  * @param statistics The statistics data to save
@@ -3,7 +3,7 @@
3
3
  * File system storage adapter for Node.js environments
4
4
  */
5
5
  import { HNSWNoun, HNSWVerb, HNSWNounWithMetadata, HNSWVerbWithMetadata, StatisticsData } from '../../coreTypes.js';
6
- import { BaseStorage } from '../baseStorage.js';
6
+ import { BaseStorage, StorageBatchConfig } from '../baseStorage.js';
7
7
  type HNSWNode = HNSWNoun;
8
8
  type Edge = HNSWVerb;
9
9
  /**
@@ -39,6 +39,19 @@ export declare class FileSystemStorage extends BaseStorage {
39
39
  compression?: boolean;
40
40
  compressionLevel?: number;
41
41
  });
42
+ /**
43
+ * Get FileSystem-optimized batch configuration
44
+ *
45
+ * File system storage is I/O bound but not rate limited:
46
+ * - Large batch sizes (500 items)
47
+ * - No delays needed (0ms)
48
+ * - Moderate concurrency (100 operations) - limited by I/O threads
49
+ * - Parallel processing supported
50
+ *
51
+ * @returns FileSystem-optimized batch configuration
52
+ * @since v4.11.0
53
+ */
54
+ getBatchConfig(): StorageBatchConfig;
42
55
  /**
43
56
  * Initialize the storage adapter
44
57
  */
@@ -70,6 +70,30 @@ export class FileSystemStorage extends BaseStorage {
70
70
  }
71
71
  // Defer path operations until init() when path module is guaranteed to be loaded
72
72
  }
73
+ /**
74
+ * Get FileSystem-optimized batch configuration
75
+ *
76
+ * File system storage is I/O bound but not rate limited:
77
+ * - Large batch sizes (500 items)
78
+ * - No delays needed (0ms)
79
+ * - Moderate concurrency (100 operations) - limited by I/O threads
80
+ * - Parallel processing supported
81
+ *
82
+ * @returns FileSystem-optimized batch configuration
83
+ * @since v4.11.0
84
+ */
85
+ getBatchConfig() {
86
+ return {
87
+ maxBatchSize: 500,
88
+ batchDelayMs: 0,
89
+ maxConcurrent: 100,
90
+ supportsParallelWrites: true, // Filesystem handles parallel I/O
91
+ rateLimit: {
92
+ operationsPerSecond: 5000, // Depends on disk speed
93
+ burstCapacity: 2000
94
+ }
95
+ };
96
+ }
73
97
  /**
74
98
  * Initialize the storage adapter
75
99
  */
@@ -9,7 +9,7 @@
9
9
  * 4. HMAC Keys (fallback for backward compatibility)
10
10
  */
11
11
  import { HNSWNoun, HNSWVerb, HNSWNounWithMetadata, HNSWVerbWithMetadata, StatisticsData } from '../../coreTypes.js';
12
- import { BaseStorage } from '../baseStorage.js';
12
+ import { BaseStorage, StorageBatchConfig } from '../baseStorage.js';
13
13
  type HNSWNode = HNSWNoun;
14
14
  type Edge = HNSWVerb;
15
15
  /**
@@ -76,6 +76,21 @@ export declare class GcsStorage extends BaseStorage {
76
76
  };
77
77
  readOnly?: boolean;
78
78
  });
79
+ /**
80
+ * Get GCS-optimized batch configuration
81
+ *
82
+ * GCS has strict rate limits (~5000 writes/second per bucket) and benefits from:
83
+ * - Moderate batch sizes (50 items)
84
+ * - Sequential processing (not parallel)
85
+ * - Delays between batches (100ms)
86
+ *
87
+ * Note: Each entity write involves 2 operations (vector + metadata),
88
+ * so 800 ops/sec = ~400 entities/sec = ~2500 actual GCS writes/sec
89
+ *
90
+ * @returns GCS-optimized batch configuration
91
+ * @since v4.11.0
92
+ */
93
+ getBatchConfig(): StorageBatchConfig;
79
94
  /**
80
95
  * Initialize the storage adapter
81
96
  */
@@ -92,6 +92,32 @@ export class GcsStorage extends BaseStorage {
92
92
  prodLog.info('🚀 High-volume mode FORCED via BRAINY_FORCE_HIGH_VOLUME environment variable');
93
93
  }
94
94
  }
95
+ /**
96
+ * Get GCS-optimized batch configuration
97
+ *
98
+ * GCS has strict rate limits (~5000 writes/second per bucket) and benefits from:
99
+ * - Moderate batch sizes (50 items)
100
+ * - Sequential processing (not parallel)
101
+ * - Delays between batches (100ms)
102
+ *
103
+ * Note: Each entity write involves 2 operations (vector + metadata),
104
+ * so 800 ops/sec = ~400 entities/sec = ~2500 actual GCS writes/sec
105
+ *
106
+ * @returns GCS-optimized batch configuration
107
+ * @since v4.11.0
108
+ */
109
+ getBatchConfig() {
110
+ return {
111
+ maxBatchSize: 50,
112
+ batchDelayMs: 100,
113
+ maxConcurrent: 50,
114
+ supportsParallelWrites: false, // Sequential is safer for GCS rate limits
115
+ rateLimit: {
116
+ operationsPerSecond: 800, // Conservative estimate for entity operations
117
+ burstCapacity: 200
118
+ }
119
+ };
120
+ }
95
121
  /**
96
122
  * Initialize the storage adapter
97
123
  */
@@ -3,7 +3,7 @@
3
3
  * In-memory storage adapter for environments where persistent storage is not available or needed
4
4
  */
5
5
  import { HNSWNoun, HNSWVerb, HNSWNounWithMetadata, HNSWVerbWithMetadata, StatisticsData } from '../../coreTypes.js';
6
- import { BaseStorage } from '../baseStorage.js';
6
+ import { BaseStorage, StorageBatchConfig } from '../baseStorage.js';
7
7
  /**
8
8
  * In-memory storage adapter
9
9
  * Uses Maps to store data in memory
@@ -17,6 +17,19 @@ export declare class MemoryStorage extends BaseStorage {
17
17
  private get nounMetadata();
18
18
  private get verbMetadata();
19
19
  constructor();
20
+ /**
21
+ * Get Memory-optimized batch configuration
22
+ *
23
+ * Memory storage has no rate limits and can handle very high throughput:
24
+ * - Large batch sizes (1000 items)
25
+ * - No delays needed (0ms)
26
+ * - High concurrency (1000 operations)
27
+ * - Parallel processing maximizes throughput
28
+ *
29
+ * @returns Memory-optimized batch configuration
30
+ * @since v4.11.0
31
+ */
32
+ getBatchConfig(): StorageBatchConfig;
20
33
  /**
21
34
  * Initialize the storage adapter
22
35
  * Nothing to initialize for in-memory storage
@@ -32,6 +32,30 @@ export class MemoryStorage extends BaseStorage {
32
32
  // Even in-memory operations need serialization to prevent async race conditions
33
33
  this.hnswLocks = new Map();
34
34
  }
35
+ /**
36
+ * Get Memory-optimized batch configuration
37
+ *
38
+ * Memory storage has no rate limits and can handle very high throughput:
39
+ * - Large batch sizes (1000 items)
40
+ * - No delays needed (0ms)
41
+ * - High concurrency (1000 operations)
42
+ * - Parallel processing maximizes throughput
43
+ *
44
+ * @returns Memory-optimized batch configuration
45
+ * @since v4.11.0
46
+ */
47
+ getBatchConfig() {
48
+ return {
49
+ maxBatchSize: 1000,
50
+ batchDelayMs: 0,
51
+ maxConcurrent: 1000,
52
+ supportsParallelWrites: true, // Memory loves parallel operations
53
+ rateLimit: {
54
+ operationsPerSecond: 100000, // Virtually unlimited
55
+ burstCapacity: 100000
56
+ }
57
+ };
58
+ }
35
59
  /**
36
60
  * Initialize the storage adapter
37
61
  * Nothing to initialize for in-memory storage
@@ -3,7 +3,7 @@
3
3
  * Provides persistent storage for the vector database using the Origin Private File System API
4
4
  */
5
5
  import { HNSWNoun, HNSWVerb, HNSWNounWithMetadata, HNSWVerbWithMetadata, StatisticsData } from '../../coreTypes.js';
6
- import { BaseStorage } from '../baseStorage.js';
6
+ import { BaseStorage, StorageBatchConfig } from '../baseStorage.js';
7
7
  import '../../types/fileSystemTypes.js';
8
8
  type HNSWNode = HNSWNoun;
9
9
  /**
@@ -30,6 +30,19 @@ export declare class OPFSStorage extends BaseStorage {
30
30
  private activeLocks;
31
31
  private lockPrefix;
32
32
  constructor();
33
+ /**
34
+ * Get OPFS-optimized batch configuration
35
+ *
36
+ * OPFS (Origin Private File System) is browser-based storage with moderate performance:
37
+ * - Moderate batch sizes (100 items)
38
+ * - Small delays (10ms) for browser event loop
39
+ * - Limited concurrency (50 operations) - browser constraints
40
+ * - Sequential processing preferred for stability
41
+ *
42
+ * @returns OPFS-optimized batch configuration
43
+ * @since v4.11.0
44
+ */
45
+ getBatchConfig(): StorageBatchConfig;
33
46
  /**
34
47
  * Initialize the storage adapter
35
48
  */
@@ -51,6 +51,30 @@ export class OPFSStorage extends BaseStorage {
51
51
  'storage' in navigator &&
52
52
  'getDirectory' in navigator.storage;
53
53
  }
54
+ /**
55
+ * Get OPFS-optimized batch configuration
56
+ *
57
+ * OPFS (Origin Private File System) is browser-based storage with moderate performance:
58
+ * - Moderate batch sizes (100 items)
59
+ * - Small delays (10ms) for browser event loop
60
+ * - Limited concurrency (50 operations) - browser constraints
61
+ * - Sequential processing preferred for stability
62
+ *
63
+ * @returns OPFS-optimized batch configuration
64
+ * @since v4.11.0
65
+ */
66
+ getBatchConfig() {
67
+ return {
68
+ maxBatchSize: 100,
69
+ batchDelayMs: 10,
70
+ maxConcurrent: 50,
71
+ supportsParallelWrites: false, // Sequential safer in browser
72
+ rateLimit: {
73
+ operationsPerSecond: 1000,
74
+ burstCapacity: 500
75
+ }
76
+ };
77
+ }
54
78
  /**
55
79
  * Initialize the storage adapter
56
80
  */
@@ -12,7 +12,7 @@
12
12
  * Based on latest GCS and S3 implementations with R2-specific enhancements
13
13
  */
14
14
  import { HNSWNoun, HNSWVerb, HNSWNounWithMetadata, HNSWVerbWithMetadata, StatisticsData } from '../../coreTypes.js';
15
- import { BaseStorage } from '../baseStorage.js';
15
+ import { BaseStorage, StorageBatchConfig } from '../baseStorage.js';
16
16
  type HNSWNode = HNSWNoun;
17
17
  type Edge = HNSWVerb;
18
18
  /**
@@ -75,6 +75,23 @@ export declare class R2Storage extends BaseStorage {
75
75
  };
76
76
  readOnly?: boolean;
77
77
  });
78
+ /**
79
+ * Get R2-optimized batch configuration
80
+ *
81
+ * Cloudflare R2 has S3-compatible characteristics with some advantages:
82
+ * - Zero egress fees (can cache more aggressively)
83
+ * - Global edge network
84
+ * - Similar throughput to S3
85
+ *
86
+ * R2 benefits from the same configuration as S3:
87
+ * - Larger batch sizes (100 items)
88
+ * - Parallel processing
89
+ * - Short delays (50ms)
90
+ *
91
+ * @returns R2-optimized batch configuration
92
+ * @since v4.11.0
93
+ */
94
+ getBatchConfig(): StorageBatchConfig;
78
95
  /**
79
96
  * Initialize the storage adapter
80
97
  */
@@ -94,6 +94,34 @@ export class R2Storage extends BaseStorage {
94
94
  prodLog.info('🚀 R2: High-volume mode FORCED via environment variable');
95
95
  }
96
96
  }
97
+ /**
98
+ * Get R2-optimized batch configuration
99
+ *
100
+ * Cloudflare R2 has S3-compatible characteristics with some advantages:
101
+ * - Zero egress fees (can cache more aggressively)
102
+ * - Global edge network
103
+ * - Similar throughput to S3
104
+ *
105
+ * R2 benefits from the same configuration as S3:
106
+ * - Larger batch sizes (100 items)
107
+ * - Parallel processing
108
+ * - Short delays (50ms)
109
+ *
110
+ * @returns R2-optimized batch configuration
111
+ * @since v4.11.0
112
+ */
113
+ getBatchConfig() {
114
+ return {
115
+ maxBatchSize: 100,
116
+ batchDelayMs: 50,
117
+ maxConcurrent: 100,
118
+ supportsParallelWrites: true, // R2 handles parallel writes like S3
119
+ rateLimit: {
120
+ operationsPerSecond: 3500, // Similar to S3 throughput
121
+ burstCapacity: 1000
122
+ }
123
+ };
124
+ }
97
125
  /**
98
126
  * Initialize the storage adapter
99
127
  */
@@ -4,7 +4,7 @@
4
4
  * including Amazon S3, Cloudflare R2, and Google Cloud Storage
5
5
  */
6
6
  import { Change, HNSWNoun, HNSWVerb, HNSWNounWithMetadata, HNSWVerbWithMetadata, StatisticsData } from '../../coreTypes.js';
7
- import { BaseStorage } from '../baseStorage.js';
7
+ import { BaseStorage, StorageBatchConfig } from '../baseStorage.js';
8
8
  import { OperationConfig } from '../../utils/operationUtils.js';
9
9
  type HNSWNode = HNSWNoun;
10
10
  type Edge = HNSWVerb;
@@ -96,6 +96,20 @@ export declare class S3CompatibleStorage extends BaseStorage {
96
96
  };
97
97
  readOnly?: boolean;
98
98
  });
99
+ /**
100
+ * Get S3-optimized batch configuration
101
+ *
102
+ * S3 has higher throughput than GCS and handles parallel writes efficiently:
103
+ * - Larger batch sizes (100 items)
104
+ * - Parallel processing supported
105
+ * - Shorter delays between batches (50ms)
106
+ *
107
+ * S3 can handle ~3500 operations/second per bucket with good performance
108
+ *
109
+ * @returns S3-optimized batch configuration
110
+ * @since v4.11.0
111
+ */
112
+ getBatchConfig(): StorageBatchConfig;
99
113
  /**
100
114
  * Initialize the storage adapter
101
115
  */
@@ -114,6 +114,31 @@ export class S3CompatibleStorage extends BaseStorage {
114
114
  this.nounCacheManager = new CacheManager(options.cacheConfig);
115
115
  this.verbCacheManager = new CacheManager(options.cacheConfig);
116
116
  }
117
+ /**
118
+ * Get S3-optimized batch configuration
119
+ *
120
+ * S3 has higher throughput than GCS and handles parallel writes efficiently:
121
+ * - Larger batch sizes (100 items)
122
+ * - Parallel processing supported
123
+ * - Shorter delays between batches (50ms)
124
+ *
125
+ * S3 can handle ~3500 operations/second per bucket with good performance
126
+ *
127
+ * @returns S3-optimized batch configuration
128
+ * @since v4.11.0
129
+ */
130
+ getBatchConfig() {
131
+ return {
132
+ maxBatchSize: 100,
133
+ batchDelayMs: 50,
134
+ maxConcurrent: 100,
135
+ supportsParallelWrites: true, // S3 handles parallel writes efficiently
136
+ rateLimit: {
137
+ operationsPerSecond: 3500, // S3 is more permissive than GCS
138
+ burstCapacity: 1000
139
+ }
140
+ };
141
+ }
117
142
  /**
118
143
  * Initialize the storage adapter
119
144
  */
@@ -5,6 +5,30 @@
5
5
  import { GraphAdjacencyIndex } from '../graph/graphAdjacencyIndex.js';
6
6
  import { GraphVerb, HNSWNoun, HNSWVerb, NounMetadata, VerbMetadata, HNSWNounWithMetadata, HNSWVerbWithMetadata, StatisticsData } from '../coreTypes.js';
7
7
  import { BaseStorageAdapter } from './adapters/baseStorageAdapter.js';
8
+ /**
9
+ * Storage adapter batch configuration profile
10
+ * Each storage adapter declares its optimal batch behavior for rate limiting
11
+ * and performance optimization
12
+ *
13
+ * @since v4.11.0
14
+ */
15
+ export interface StorageBatchConfig {
16
+ /** Maximum items per batch */
17
+ maxBatchSize: number;
18
+ /** Delay between batches in milliseconds (for rate limiting) */
19
+ batchDelayMs: number;
20
+ /** Maximum concurrent operations this storage can handle */
21
+ maxConcurrent: number;
22
+ /** Whether storage can handle parallel writes efficiently */
23
+ supportsParallelWrites: boolean;
24
+ /** Rate limit characteristics of this storage adapter */
25
+ rateLimit: {
26
+ /** Approximate operations per second this storage can handle */
27
+ operationsPerSecond: number;
28
+ /** Maximum burst capacity before throttling occurs */
29
+ burstCapacity: number;
30
+ };
31
+ }
8
32
  export declare const NOUNS_METADATA_DIR = "entities/nouns/metadata";
9
33
  export declare const VERBS_METADATA_DIR = "entities/verbs/metadata";
10
34
  export declare const SYSTEM_DIR = "_system";
@@ -27,33 +27,40 @@ export declare class AdaptiveBackpressure {
27
27
  private metrics;
28
28
  private config;
29
29
  private patterns;
30
- private circuitState;
31
- private circuitOpenTime;
32
- private circuitFailures;
33
- private circuitThreshold;
34
- private circuitTimeout;
30
+ private circuits;
35
31
  private operationTimes;
36
32
  private completedOps;
37
33
  private errorOps;
38
34
  private lastAdaptation;
39
35
  /**
40
36
  * Request permission to proceed with an operation
37
+ * @param operationId Unique ID for this operation
38
+ * @param priority Priority level (higher = more important)
39
+ * @param operationType Type of operation (read or write) for circuit breaker isolation
41
40
  */
42
- requestPermission(operationId: string, priority?: number): Promise<void>;
41
+ requestPermission(operationId: string, priority?: number, operationType?: 'read' | 'write'): Promise<void>;
43
42
  /**
44
43
  * Release permission after operation completes
44
+ * @param operationId Unique ID for this operation
45
+ * @param success Whether the operation succeeded
46
+ * @param operationType Type of operation (read or write) for circuit breaker tracking
45
47
  */
46
- releasePermission(operationId: string, success?: boolean): void;
48
+ releasePermission(operationId: string, success?: boolean, operationType?: 'read' | 'write'): void;
47
49
  /**
48
- * Check if circuit breaker is open
50
+ * Check if circuit breaker is open for a specific operation type
51
+ * @param circuit The circuit to check (read or write)
49
52
  */
50
53
  private isCircuitOpen;
51
54
  /**
52
- * Open the circuit breaker
55
+ * Open the circuit breaker for a specific operation type
56
+ * @param circuit The circuit to open (read or write)
57
+ * @param operationType The operation type name for logging
53
58
  */
54
59
  private openCircuit;
55
60
  /**
56
- * Close the circuit breaker
61
+ * Close the circuit breaker for a specific operation type
62
+ * @param circuit The circuit to close (read or write)
63
+ * @param operationType The operation type name for logging
57
64
  */
58
65
  private closeCircuit;
59
66
  /**