chore: recovery checkpoint - v3.0 API successfully recovered

CRITICAL CHECKPOINT - DO NOT PUSH TO GITHUB

Recovery Status:
- Successfully recovered brainy.ts from compiled JavaScript
- All core v3.0 API methods functional (add, get, update, delete, relate, find, etc.)
- Neural subsystem intact (562KB embedded patterns, NLP working)
- Augmentation pipeline operational (20+ augmentations)
- HNSW clustering system complete
- Triple Intelligence compiled (needs constructor fix)
- Test suite validates functionality

Changes preserved:
- 898 files with changes from last 3 days
- 144,475 insertions
- All augmentation improvements
- All test coverage enhancements
- Complete v3.0 feature set

This is a LOCAL checkpoint only - contains recovered work after corruption incident.
Created backup in .backups/brainy-full-20250910-151314.tar.gz

Branch: recovery-checkpoint-20250910-151433
Date: Wed Sep 10 03:18:04 PM PDT 2025
This commit is contained in:
David Snelling 2025-09-10 15:18:04 -07:00
parent f65455fb22
commit 8ff382ca3b
895 changed files with 143654 additions and 28268 deletions

View file

@ -0,0 +1,342 @@
/**
* Connection Pool Augmentation
*
* Provides 10-20x throughput improvement for cloud storage (S3, R2, GCS)
* Manages connection pooling, request queuing, and parallel processing
* Critical for enterprise-scale operations with millions of entries
*/
import { BaseAugmentation } from './brainyAugmentation.js';
export class ConnectionPoolAugmentation extends BaseAugmentation {
constructor(config = {}) {
super();
this.name = 'ConnectionPool';
this.timing = 'around';
this.metadata = 'none'; // Connection pooling doesn't access metadata
this.operations = ['storage'];
this.priority = 95; // Very high priority for storage operations
this.connections = new Map();
this.requestQueue = [];
this.storageType = 'unknown';
this.stats = {
totalRequests: 0,
queuedRequests: 0,
activeConnections: 0,
totalConnections: 0,
averageLatency: 0,
throughputPerSecond: 0
};
this.config = {
enabled: config.enabled ?? true,
maxConnections: config.maxConnections ?? 50,
minConnections: config.minConnections ?? 5,
acquireTimeout: config.acquireTimeout ?? 30000, // 30s
idleTimeout: config.idleTimeout ?? 300000, // 5 minutes
maxQueueSize: config.maxQueueSize ?? 10000,
retryAttempts: config.retryAttempts ?? 3,
healthCheckInterval: config.healthCheckInterval ?? 60000 // 1 minute
};
}
async onInitialize() {
if (!this.config.enabled) {
this.log('Connection pooling disabled');
return;
}
// Detect storage type
this.storageType = this.detectStorageType();
if (this.isCloudStorage()) {
await this.initializeConnectionPool();
this.startHealthChecks();
this.startMetricsCollection();
this.log(`Connection pool initialized for ${this.storageType}: ${this.config.minConnections}-${this.config.maxConnections} connections`);
}
else {
this.log(`Connection pooling skipped for ${this.storageType} (local storage)`);
}
}
shouldExecute(operation, params) {
return this.config.enabled &&
this.isCloudStorage() &&
this.isStorageOperation(operation);
}
async execute(operation, params, next) {
if (!this.shouldExecute(operation, params)) {
return next();
}
const startTime = Date.now();
this.stats.totalRequests++;
try {
// High priority for critical operations
const priority = this.getOperationPriority(operation);
// Execute with pooled connection
const result = await this.executeWithPool(operation, params, next, priority);
// Update metrics
const latency = Date.now() - startTime;
this.updateLatencyMetrics(latency);
return result;
}
catch (error) {
this.log(`Connection pool error for ${operation}: ${error}`, 'error');
// Fallback to direct execution for reliability
return next();
}
}
detectStorageType() {
const storage = this.context?.storage;
if (!storage)
return 'unknown';
const className = storage.constructor.name.toLowerCase();
if (className.includes('s3'))
return 's3';
if (className.includes('r2'))
return 'r2';
if (className.includes('gcs') || className.includes('google'))
return 'gcs';
if (className.includes('azure'))
return 'azure';
if (className.includes('filesystem'))
return 'filesystem';
if (className.includes('memory'))
return 'memory';
return 'unknown';
}
isCloudStorage() {
return ['s3', 'r2', 'gcs', 'azure'].includes(this.storageType);
}
isStorageOperation(operation) {
return operation.includes('save') ||
operation.includes('get') ||
operation.includes('delete') ||
operation.includes('list') ||
operation.includes('backup') ||
operation.includes('restore');
}
getOperationPriority(operation) {
// Critical operations get highest priority
if (operation.includes('save') || operation.includes('update'))
return 10;
if (operation.includes('delete'))
return 9;
if (operation.includes('get'))
return 7;
if (operation.includes('list'))
return 5;
if (operation.includes('backup'))
return 3;
return 1;
}
async executeWithPool(operation, params, executor, priority) {
// Check queue size
if (this.requestQueue.length >= this.config.maxQueueSize) {
throw new Error('Connection pool queue full - system overloaded');
}
// Try to get available connection immediately
const connection = await this.getOrCreateConnection();
if (connection && connection.isIdle) {
return this.executeWithConnection(connection, operation, executor);
}
// Queue the request with the actual executor
return this.queueRequest(operation, params, executor, priority);
}
getAvailableConnection() {
// Find idle connection with best health score
let bestConnection = null;
let bestScore = -1;
for (const connection of this.connections.values()) {
if (connection.isIdle && connection.healthScore > bestScore) {
bestConnection = connection;
bestScore = connection.healthScore;
}
}
return bestConnection;
}
async executeWithConnection(connection, operation, executor) {
// Mark connection as active
connection.isIdle = false;
connection.activeRequests++;
connection.lastUsed = Date.now();
this.stats.activeConnections++;
try {
const result = await executor();
// Update connection health on success
connection.healthScore = Math.min(connection.healthScore + 1, 100);
return result;
}
catch (error) {
// Decrease health on failure
connection.healthScore = Math.max(connection.healthScore - 5, 0);
throw error;
}
finally {
// Release connection
connection.isIdle = true;
connection.activeRequests--;
this.stats.activeConnections--;
// Process next queued request
this.processQueue();
}
}
async queueRequest(operation, params, executor, priority) {
return new Promise((resolve, reject) => {
const request = {
id: `req_${Date.now()}_${Math.random()}`,
operation,
params,
executor, // Store the actual executor function
resolver: resolve,
rejector: reject,
timestamp: Date.now(),
priority
};
// Insert by priority (higher priority first)
const insertIndex = this.requestQueue.findIndex(r => r.priority < priority);
if (insertIndex === -1) {
this.requestQueue.push(request);
}
else {
this.requestQueue.splice(insertIndex, 0, request);
}
this.stats.queuedRequests++;
// Set timeout
setTimeout(() => {
const index = this.requestQueue.findIndex(r => r.id === request.id);
if (index !== -1) {
this.requestQueue.splice(index, 1);
this.stats.queuedRequests--;
reject(new Error(`Connection pool timeout: ${this.config.acquireTimeout}ms`));
}
}, this.config.acquireTimeout);
});
}
processQueue() {
if (this.requestQueue.length === 0)
return;
const connection = this.getAvailableConnection();
if (!connection)
return;
const request = this.requestQueue.shift();
this.stats.queuedRequests--;
// Execute queued request with the REAL executor
this.executeWithConnection(connection, request.operation, request.executor)
.then(request.resolver)
.catch(request.rejector);
}
async initializeConnectionPool() {
// Create minimum connections
for (let i = 0; i < this.config.minConnections; i++) {
await this.createConnection();
}
}
async createConnection() {
const connectionId = `conn_${Date.now()}_${Math.random()}`;
// Create actual connection based on storage type
const actualConnection = await this.createStorageConnection();
const connection = {
id: connectionId,
connection: actualConnection,
isIdle: true,
lastUsed: Date.now(),
healthScore: 100,
activeRequests: 0,
requestCount: 0
};
this.connections.set(connectionId, connection);
this.stats.totalConnections++;
return connection;
}
async createStorageConnection() {
// For cloud storage, reuse the existing storage instance
// Connection pooling in this context means managing concurrent requests
// not creating multiple storage instances (which would be wasteful)
const storage = this.context?.storage;
if (!storage) {
throw new Error('Storage not available for connection pooling');
}
// Return a connection wrapper that tracks usage
return {
storage,
created: Date.now(),
requestCount: 0
};
}
async getOrCreateConnection() {
// Try to get an available connection
let connection = this.getAvailableConnection();
// If no connection available and under max, create new one
if (!connection && this.connections.size < this.config.maxConnections) {
connection = await this.createConnection();
}
return connection;
}
startHealthChecks() {
this.healthCheckInterval = setInterval(() => {
this.performHealthChecks();
}, this.config.healthCheckInterval);
}
performHealthChecks() {
const now = Date.now();
const toRemove = [];
for (const [id, connection] of this.connections) {
// Remove idle connections that are too old
if (connection.isIdle &&
now - connection.lastUsed > this.config.idleTimeout &&
this.connections.size > this.config.minConnections) {
toRemove.push(id);
}
// Remove unhealthy connections
if (connection.healthScore < 20) {
toRemove.push(id);
}
}
// Remove unhealthy/old connections
for (const id of toRemove) {
this.connections.delete(id);
this.stats.totalConnections--;
}
// Ensure minimum connections
while (this.connections.size < this.config.minConnections) {
this.createConnection();
}
}
startMetricsCollection() {
setInterval(() => {
this.updateThroughputMetrics();
}, 1000); // Update every second
}
updateLatencyMetrics(latency) {
// Simple moving average
this.stats.averageLatency = (this.stats.averageLatency * 0.9) + (latency * 0.1);
}
updateThroughputMetrics() {
// Reset counter for next second
this.stats.throughputPerSecond = this.stats.totalRequests;
// Reset total for next measurement (in practice, use sliding window)
}
/**
* Get connection pool statistics
*/
getStats() {
return {
...this.stats,
queueSize: this.requestQueue.length,
activeConnections: this.stats.activeConnections,
totalConnections: this.connections.size,
poolUtilization: `${Math.round((this.stats.activeConnections / this.connections.size) * 100)}%`,
storageType: this.storageType
};
}
async onShutdown() {
if (this.healthCheckInterval) {
clearInterval(this.healthCheckInterval);
}
// Close all connections
this.connections.clear();
// Reject all queued requests
this.requestQueue.forEach(request => {
request.rejector(new Error('Connection pool shutting down'));
});
this.requestQueue = [];
const stats = this.getStats();
this.log(`Connection pool shutdown: ${stats.totalRequests} requests processed, ${stats.poolUtilization} avg utilization`);
}
}
//# sourceMappingURL=connectionPoolAugmentation.js.map