@soulcraft/brainy 4.10.2 → 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.
@@ -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
  /**
@@ -32,12 +32,24 @@ export class AdaptiveBackpressure {
32
32
  };
33
33
  // Historical patterns for learning
34
34
  this.patterns = [];
35
- // Circuit breaker state
36
- this.circuitState = 'closed';
37
- this.circuitOpenTime = 0;
38
- this.circuitFailures = 0;
39
- this.circuitThreshold = 5;
40
- this.circuitTimeout = 30000; // 30 seconds
35
+ // Separate circuit breakers for read vs write operations
36
+ // This allows reads to continue even when writes are throttled
37
+ this.circuits = {
38
+ read: {
39
+ state: 'closed',
40
+ failures: 0,
41
+ openTime: 0,
42
+ threshold: 10, // More lenient for reads
43
+ timeout: 30000
44
+ },
45
+ write: {
46
+ state: 'closed',
47
+ failures: 0,
48
+ openTime: 0,
49
+ threshold: 5, // Stricter for writes
50
+ timeout: 30000
51
+ }
52
+ };
41
53
  // Performance tracking
42
54
  this.operationTimes = new Map();
43
55
  this.completedOps = [];
@@ -46,11 +58,22 @@ export class AdaptiveBackpressure {
46
58
  }
47
59
  /**
48
60
  * Request permission to proceed with an operation
61
+ * @param operationId Unique ID for this operation
62
+ * @param priority Priority level (higher = more important)
63
+ * @param operationType Type of operation (read or write) for circuit breaker isolation
49
64
  */
50
- async requestPermission(operationId, priority = 1) {
51
- // Check circuit breaker
52
- if (this.isCircuitOpen()) {
53
- throw new Error('Circuit breaker is open - system is recovering');
65
+ async requestPermission(operationId, priority = 1, operationType = 'write') {
66
+ const circuit = this.circuits[operationType];
67
+ // Check circuit breaker for this operation type
68
+ if (this.isCircuitOpen(circuit)) {
69
+ // KEY: Allow reads even if write circuit is open
70
+ if (operationType === 'read' && this.circuits.write.state === 'open') {
71
+ // Write circuit is open but read circuit is fine - allow read
72
+ this.activeOperations.add(operationId);
73
+ this.operationTimes.set(operationId, Date.now());
74
+ return;
75
+ }
76
+ throw new Error(`Circuit breaker is open for ${operationType} operations - system is recovering`);
54
77
  }
55
78
  // Fast path for low load
56
79
  if (this.activeOperations.size < this.maxConcurrent * 0.5 && this.queue.length === 0) {
@@ -84,8 +107,11 @@ export class AdaptiveBackpressure {
84
107
  }
85
108
  /**
86
109
  * Release permission after operation completes
110
+ * @param operationId Unique ID for this operation
111
+ * @param success Whether the operation succeeded
112
+ * @param operationType Type of operation (read or write) for circuit breaker tracking
87
113
  */
88
- releasePermission(operationId, success = true) {
114
+ releasePermission(operationId, success = true, operationType = 'write') {
89
115
  // Remove from active operations
90
116
  this.activeOperations.delete(operationId);
91
117
  // Track completion time
@@ -99,19 +125,20 @@ export class AdaptiveBackpressure {
99
125
  this.completedOps = this.completedOps.slice(-500);
100
126
  }
101
127
  }
102
- // Track errors for circuit breaker
128
+ // Track errors for circuit breaker per operation type
129
+ const circuit = this.circuits[operationType];
103
130
  if (!success) {
104
131
  this.errorOps++;
105
- this.circuitFailures++;
106
- // Check if we should open circuit
107
- if (this.circuitFailures >= this.circuitThreshold) {
108
- this.openCircuit();
132
+ circuit.failures++;
133
+ // Check if we should open circuit for this operation type
134
+ if (circuit.failures >= circuit.threshold) {
135
+ this.openCircuit(circuit, operationType);
109
136
  }
110
137
  }
111
138
  else {
112
139
  // Reset circuit failures on success
113
- if (this.circuitState === 'half-open') {
114
- this.closeCircuit();
140
+ if (circuit.state === 'half-open') {
141
+ this.closeCircuit(circuit, operationType);
115
142
  }
116
143
  }
117
144
  // Process queue if there are waiting operations
@@ -129,13 +156,14 @@ export class AdaptiveBackpressure {
129
156
  this.adaptIfNeeded();
130
157
  }
131
158
  /**
132
- * Check if circuit breaker is open
159
+ * Check if circuit breaker is open for a specific operation type
160
+ * @param circuit The circuit to check (read or write)
133
161
  */
134
- isCircuitOpen() {
135
- if (this.circuitState === 'open') {
162
+ isCircuitOpen(circuit) {
163
+ if (circuit.state === 'open') {
136
164
  // Check if timeout has passed
137
- if (Date.now() - this.circuitOpenTime > this.circuitTimeout) {
138
- this.circuitState = 'half-open';
165
+ if (Date.now() - circuit.openTime > circuit.timeout) {
166
+ circuit.state = 'half-open';
139
167
  this.logger.info('Circuit breaker entering half-open state');
140
168
  return false;
141
169
  }
@@ -144,26 +172,34 @@ export class AdaptiveBackpressure {
144
172
  return false;
145
173
  }
146
174
  /**
147
- * Open the circuit breaker
175
+ * Open the circuit breaker for a specific operation type
176
+ * @param circuit The circuit to open (read or write)
177
+ * @param operationType The operation type name for logging
148
178
  */
149
- openCircuit() {
150
- if (this.circuitState !== 'open') {
151
- this.circuitState = 'open';
152
- this.circuitOpenTime = Date.now();
153
- this.logger.warn('Circuit breaker opened due to high error rate');
154
- // Reduce load immediately
155
- this.maxConcurrent = Math.max(10, Math.floor(this.maxConcurrent * 0.3));
179
+ openCircuit(circuit, operationType) {
180
+ if (circuit.state !== 'open') {
181
+ circuit.state = 'open';
182
+ circuit.openTime = Date.now();
183
+ this.logger.warn(`Circuit breaker opened for ${operationType} operations due to high error rate`);
184
+ // Reduce load immediately for write operations
185
+ if (operationType === 'write') {
186
+ this.maxConcurrent = Math.max(10, Math.floor(this.maxConcurrent * 0.3));
187
+ }
156
188
  }
157
189
  }
158
190
  /**
159
- * Close the circuit breaker
191
+ * Close the circuit breaker for a specific operation type
192
+ * @param circuit The circuit to close (read or write)
193
+ * @param operationType The operation type name for logging
160
194
  */
161
- closeCircuit() {
162
- this.circuitState = 'closed';
163
- this.circuitFailures = 0;
164
- this.logger.info('Circuit breaker closed - system recovered');
165
- // Gradually increase capacity
166
- this.maxConcurrent = Math.min(500, Math.floor(this.maxConcurrent * 1.5));
195
+ closeCircuit(circuit, operationType) {
196
+ circuit.state = 'closed';
197
+ circuit.failures = 0;
198
+ this.logger.info(`Circuit breaker closed for ${operationType} - system recovered`);
199
+ // Gradually increase capacity for write operations
200
+ if (operationType === 'write') {
201
+ this.maxConcurrent = Math.min(500, Math.floor(this.maxConcurrent * 1.5));
202
+ }
167
203
  }
168
204
  /**
169
205
  * Adapt configuration based on metrics
@@ -262,13 +298,8 @@ export class AdaptiveBackpressure {
262
298
  // Allow queue depth to be 10 seconds worth of throughput
263
299
  this.config.maxQueueDepth = Math.max(100, Math.min(10000, Math.floor(this.metrics.throughput * 10)));
264
300
  }
265
- // Adapt circuit breaker threshold based on error patterns
266
- if (this.metrics.errorRate < 0.01 && this.circuitThreshold > 5) {
267
- this.circuitThreshold = Math.max(5, this.circuitThreshold - 1);
268
- }
269
- else if (this.metrics.errorRate > 0.05 && this.circuitThreshold < 20) {
270
- this.circuitThreshold = Math.min(20, this.circuitThreshold + 1);
271
- }
301
+ // Note: Circuit breaker thresholds are now fixed per operation type (read/write)
302
+ // and do not adapt dynamically to maintain predictable behavior
272
303
  }
273
304
  /**
274
305
  * Predict future load based on patterns
@@ -303,10 +334,24 @@ export class AdaptiveBackpressure {
303
334
  * Get current configuration and metrics
304
335
  */
305
336
  getStatus() {
337
+ // Combined circuit status for backward compatibility
338
+ let circuitStatus = 'closed';
339
+ if (this.circuits.read.state === 'open' && this.circuits.write.state === 'open') {
340
+ circuitStatus = 'open';
341
+ }
342
+ else if (this.circuits.write.state === 'open') {
343
+ circuitStatus = 'write-circuit-open';
344
+ }
345
+ else if (this.circuits.read.state === 'open') {
346
+ circuitStatus = 'read-circuit-open';
347
+ }
348
+ else if (this.circuits.read.state === 'half-open' || this.circuits.write.state === 'half-open') {
349
+ circuitStatus = 'half-open';
350
+ }
306
351
  return {
307
352
  config: { ...this.config },
308
353
  metrics: { ...this.metrics },
309
- circuit: this.circuitState,
354
+ circuit: circuitStatus,
310
355
  maxConcurrent: this.maxConcurrent,
311
356
  activeOps: this.activeOperations.size,
312
357
  queueLength: this.queue.length
@@ -322,8 +367,13 @@ export class AdaptiveBackpressure {
322
367
  this.completedOps = [];
323
368
  this.errorOps = 0;
324
369
  this.patterns = [];
325
- this.circuitState = 'closed';
326
- this.circuitFailures = 0;
370
+ // Reset both circuit breakers
371
+ this.circuits.read.state = 'closed';
372
+ this.circuits.read.failures = 0;
373
+ this.circuits.read.openTime = 0;
374
+ this.circuits.write.state = 'closed';
375
+ this.circuits.write.failures = 0;
376
+ this.circuits.write.openTime = 0;
327
377
  this.maxConcurrent = 100;
328
378
  this.logger.info('Backpressure system reset to defaults');
329
379
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soulcraft/brainy",
3
- "version": "4.10.2",
3
+ "version": "4.10.4",
4
4
  "description": "Universal Knowledge Protocol™ - World's first Triple Intelligence database unifying vector, graph, and document search in one API. 31 nouns × 40 verbs for infinite expressiveness.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",