File size: 8,202 Bytes
0f01961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Autonomous MCP System - Integration Guide

## Quick Start

### 1. Initialize Cognitive Memory

```typescript
import { initializeDatabase, getDatabase } from './database/index.js';
import { initCognitiveMemory } from './mcp/autonomous/index.js';

// Initialize database
await initializeDatabase();
const db = getDatabase();

// Initialize cognitive memory
const memory = initCognitiveMemory(db);
```

### 2. Create Source Registry

```typescript
import { SourceRegistry, DataSource } from './mcp/autonomous/index.js';

class SimpleSourceRegistry implements SourceRegistry {
  private sources: Map<string, DataSource> = new Map();
  
  registerSource(source: DataSource) {
    this.sources.set(source.name, source);
  }
  
  getCapableSources(intent: QueryIntent): DataSource[] {
    // Filter sources that can handle this query
    return Array.from(this.sources.values()).filter(source => {
      // Check if source supports this operation
      return source.capabilities.includes(intent.type) ||
             source.capabilities.includes('*');
    });
  }
  
  getAllSources(): DataSource[] {
    return Array.from(this.sources.values());
  }
}

const registry = new SimpleSourceRegistry();
```

### 3. Register Data Sources

```typescript
// Example: PostgreSQL source
registry.registerSource({
  name: 'postgres-main',
  type: 'database',
  capabilities: ['agents.list', 'agents.get', 'agents.update'],
  isHealthy: async () => {
    try {
      await db.query('SELECT 1');
      return true;
    } catch {
      return false;
    }
  },
  estimatedLatency: 50,
  costPerQuery: 0
});

// Example: API source
registry.registerSource({
  name: 'external-api',
  type: 'api',
  capabilities: ['security.search', 'security.list'],
  isHealthy: async () => {
    const response = await fetch('https://api.example.com/health');
    return response.ok;
  },
  estimatedLatency: 200,
  costPerQuery: 0.01
});
```

### 4. Create Autonomous Agent

```typescript
import { AutonomousAgent, startAutonomousLearning } from './mcp/autonomous/index.js';

const agent = new AutonomousAgent(memory, registry);

// Start autonomous learning (runs every 5 minutes)
startAutonomousLearning(agent, 300000);
```

### 5. Use in Routes

```typescript
import { mcpRouter } from './mcp/mcpRouter.js';

mcpRouter.post('/autonomous/query', async (req, res) => {
  try {
    const query = req.body;
    
    // Agent autonomously selects best source and executes
    const result = await agent.executeAndLearn(query, async (source) => {
      // Your execute logic here
      return await yourDataFetcher(source, query);
    });
    
    res.json({
      success: true,
      data: result.data,
      meta: {
        source: result.source,
        latency: result.latencyMs,
        cached: result.cached
      }
    });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});
```

## Advanced Usage

### Wrap Existing Providers with Self-Healing

```typescript
import { SelfHealingAdapter } from './mcp/autonomous/index.js';

const primaryProvider: DataProvider = {
  name: 'postgres-main',
  type: 'database',
  query: async (op, params) => { /* ... */ },
  health: async () => ({ healthy: true, score: 1.0 })
};

const fallbackProvider: DataProvider = {
  name: 'postgres-replica',
  type: 'database',
  query: async (op, params) => { /* ... */ },
  health: async () => ({ healthy: true, score: 1.0 })
};

// Wrap with self-healing
const selfHealing = new SelfHealingAdapter(
  primaryProvider,
  memory,
  fallbackProvider
);

// Now use selfHealing instead of primaryProvider
```

### Predictive Pre-fetching

```typescript
// Pre-fetch data for a widget before it requests
await agent.predictAndPrefetch('AgentMonitorWidget');

// This analyzes historical patterns and pre-warms likely data
```

### Query with Intelligence

```typescript
const result = await agent.executeAndLearn({
  type: 'agents.list',
  widgetId: 'AgentMonitorWidget',
  priority: 'high',  // Favor speed over cost
  freshness: 'realtime'  // Need fresh data
}, async (source) => {
  // Your fetch logic
  return await fetchFromSource(source);
});
```

## Monitoring

### Get Agent Statistics

```typescript
const stats = await agent.getStats();
console.log(`Total decisions: ${stats.totalDecisions}`);
console.log(`Average confidence: ${(stats.averageConfidence * 100).toFixed(1)}%`);
console.log(`Top sources:`, stats.topSources);
```

### Get Source Intelligence

```typescript
const intel = await memory.getSourceIntelligence('postgres-main');
console.log(`Average latency: ${intel.averageLatency}ms`);
console.log(`Success rate: ${(intel.overallSuccessRate * 100).toFixed(1)}%`);
console.log(`Recent failures: ${intel.recentFailures}`);

if (intel.lastFailure) {
  console.log(`Last failure: ${intel.lastFailure.errorType}`);
  console.log(`Known recovery paths:`, intel.knownRecoveryPaths);
}
```

### Health Dashboard Data

```typescript
const healthHistory = await memory.getHealthHistory('postgres-main', 100);

// Analyze trends
const latencies = healthHistory.map(h => h.latency.p95);
const avgLatency = latencies.reduce((a, b) => a + b, 0) / latencies.length;
const trend = latencies[0] > latencies[latencies.length - 1] ? 'improving' : 'degrading';

console.log(`Average P95 latency: ${avgLatency.toFixed(0)}ms (${trend})`);
```

## Best Practices

### 1. Always Initialize Database First

```typescript
// ✅ Correct order
await initializeDatabase();
const memory = initCognitiveMemory(getDatabase());

// ❌ Wrong - will fail
const memory = initCognitiveMemory(getDatabase());
await initializeDatabase();
```

### 2. Register Sources at Startup

```typescript
// Register all sources before starting agent
registry.registerSource(source1);
registry.registerSource(source2);
registry.registerSource(source3);

// Then create agent
const agent = new AutonomousAgent(memory, registry);
```

### 3. Let Agent Learn Before Production

```typescript
// Run in learning mode for 1 week
const agent = new AutonomousAgent(memory, registry);

// Agent observes and learns patterns
// After 1 week of data, confidence will be high
```

### 4. Implement Graceful Fallbacks

```typescript
// Always provide fallback sources
const adapter = new SelfHealingAdapter(
  primarySource,
  memory,
  fallbackSource  // ✅ Always provide this
);
```

### 5. Monitor Decision Quality

```typescript
// Periodically check if agent is making good decisions
setInterval(async () => {
  const stats = await agent.getStats();
  
  if (stats.averageConfidence < 0.6) {
    console.warn('Low decision confidence - agent needs more data');
  }
}, 3600000); // Every hour
```

## Troubleshooting

### Agent Always Selects Same Source

**Problem**: Not enough variety in registered sources or historical data.

**Solution**:
```typescript
// Check registered sources
const sources = registry.getAllSources();
console.log(`Registered sources: ${sources.length}`);

// Check historical patterns
const patterns = await memory.getWidgetPatterns('YourWidget');
console.log(`Common sources:`, patterns.commonSources);
```

### Self-Healing Not Working

**Problem**: Circuit breaker may be stuck open.

**Solution**:
```typescript
// Check circuit breaker state in logs
// Look for: "Circuit breaker OPEN"

// Manually reset by restarting or adjusting thresholds
adapter.failureThreshold = 10; // More lenient
```

### Memory Growing Too Large

**Problem**: Not cleaning old data.

**Solution**:
```typescript
// Run cleanup periodically
setInterval(async () => {
  await memory.cleanup(30); // Keep last 30 days
}, 86400000); // Daily
```

## Next Steps

1. **Tune Decision Weights**: Adjust weights in `DecisionEngine` based on your priorities
2. **Add Custom Recovery Actions**: Extend `SelfHealingAdapter` with domain-specific recovery
3. **Implement ML Models**: Replace heuristics with trained models for predictions
4. **Build Admin Dashboard**: Visualize agent decisions and source health

## API Reference

See individual files for detailed API documentation:
- `DecisionEngine.ts` - Scoring algorithms
- `AutonomousAgent.ts` - Main orchestrator
- `SelfHealingAdapter.ts` - Recovery mechanisms
- `CognitiveMemory.ts` - Memory interface