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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# MiroOrg v1.1 Architecture

## System Overview

MiroOrg v1.1 is a 5-layer AI intelligence system designed for single-user local deployment with autonomous learning capabilities.

## Five-Layer Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Layer 5: Autonomous Knowledge Evolution                    β”‚
β”‚  - Knowledge ingestion from web/news                        β”‚
β”‚  - Experience learning from cases                           β”‚
β”‚  - Prompt evolution via A/B testing                         β”‚
β”‚  - Skill distillation from patterns                         β”‚
β”‚  - Trust & freshness management                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Layer 4: Simulation Lab Integration                        β”‚
β”‚  - MiroFish adapter for scenario modeling                   β”‚
β”‚  - Case-simulation linking                                  β”‚
β”‚  - Simulation result synthesis                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Layer 3: Agent Orchestration                               β”‚
β”‚  - Switchboard (4D routing)                                 β”‚
β”‚  - Research (domain-enhanced)                               β”‚
β”‚  - Verifier (credibility scoring)                           β”‚
β”‚  - Planner (simulation-aware)                               β”‚
β”‚  - Synthesizer (uncertainty quantification)                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Layer 2: Domain Intelligence                               β”‚
β”‚  - Pluggable domain packs                                   β”‚
β”‚  - Finance pack (market data, news, analysis)               β”‚
β”‚  - Domain registry & detection                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Layer 1: Core Platform                                     β”‚
β”‚  - Multi-provider LLM (OpenRouter/Ollama/OpenAI)           β”‚
β”‚  - Automatic fallback                                       β”‚
β”‚  - Configuration management                                 β”‚
β”‚  - Data persistence                                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Layer 1: Core Platform

### Multi-Provider LLM Abstraction

The system supports three LLM providers with automatic fallback:

1. **OpenRouter** - Recommended for production
   - Access to multiple models
   - Pay-per-use pricing
   - High availability

2. **Ollama** - Local deployment
   - Privacy-focused
   - No API costs
   - Requires local GPU/CPU

3. **OpenAI** - High quality
   - GPT-4 and GPT-3.5
   - Reliable performance
   - Higher cost

**Fallback Chain:**
```
Primary Provider β†’ Fallback Provider β†’ Error
```

### Configuration Management

All configuration via environment variables:
- Provider selection
- API keys
- Feature flags
- Domain pack enablement
- Learning layer settings

### Data Persistence

JSON-based storage for single-user deployment:
- Cases: `backend/app/data/memory/`
- Simulations: `backend/app/data/simulations/`
- Knowledge: `backend/app/data/knowledge/`
- Skills: `backend/app/data/skills/`
- Prompt versions: `backend/app/data/prompt_versions/`

## Layer 2: Domain Intelligence

### Domain Pack System

Domain packs are pluggable modules that enhance agent capabilities:

```python
class DomainPack(ABC):
    @property
    @abstractmethod
    def name(self) -> str:
        """Domain pack name (e.g., 'finance')"""
        pass
    
    @property
    @abstractmethod
    def keywords(self) -> List[str]:
        """Keywords for domain detection"""
        pass
    
    @abstractmethod
    async def enhance_research(self, query: str, context: Dict) -> Dict:
        """Enhance research with domain-specific capabilities"""
        pass
    
    @abstractmethod
    async def enhance_verification(self, claims: List[str], context: Dict) -> Dict:
        """Enhance verification with domain-specific checks"""
        pass
    
    @abstractmethod
    def get_capabilities(self) -> List[str]:
        """List domain-specific capabilities"""
        pass
```

### Finance Domain Pack

Included capabilities:
- **Market Data**: Real-time quotes via Alpha Vantage
- **News**: Financial news via NewsAPI
- **Entity Resolution**: Extract and normalize company names
- **Ticker Resolution**: Map companies to stock tickers
- **Source Checking**: Credibility scoring for financial sources
- **Rumor Detection**: Identify unverified claims
- **Scam Detection**: Flag potential scams
- **Stance Detection**: Analyze sentiment and stance
- **Event Analysis**: Analyze market-moving events
- **Prediction**: Generate market predictions

### Domain Detection

Automatic domain detection based on keywords:
```python
# Finance keywords
["stock", "market", "trading", "investment", "portfolio", 
 "earnings", "dividend", "IPO", "merger", "acquisition"]
```

## Layer 3: Agent Orchestration

### Agent Pipeline

```
User Input
    ↓
Switchboard (4D Routing)
    ↓
Research (Domain-Enhanced)
    ↓
Planner (Simulation-Aware)
    ↓
Verifier (Credibility Scoring)
    ↓
Synthesizer (Final Answer)
    ↓
Case Storage
```

### Switchboard Agent

**Four-Dimensional Classification:**

1. **Task Family**
   - `normal`: Standard analysis
   - `simulation`: Scenario modeling

2. **Domain Pack**
   - `finance`: Financial domain
   - `general`: General knowledge
   - `policy`: Policy analysis
   - `custom`: Custom domains

3. **Complexity**
   - `simple`: ≀5 words
   - `medium`: 6-25 words
   - `complex`: >25 words

4. **Execution Mode**
   - `solo`: Synthesizer only (simple queries)
   - `standard`: Research β†’ Synthesizer (medium queries)
   - `deep`: Full pipeline (complex queries)

### Research Agent

**Capabilities:**
- Web search via Tavily
- News search via NewsAPI
- Domain-specific data sources
- Entity extraction
- Source credibility assessment

**Domain Enhancement:**
```python
if domain == "finance":
    # Extract tickers and entities
    # Fetch market data
    # Get financial news
    # Score source credibility
```

### Verifier Agent

**Verification Process:**
1. Extract claims from research
2. Check source reliability
3. Detect rumors and scams
4. Quantify uncertainty
5. Flag high-risk claims

**Domain Enhancement:**
```python
if domain == "finance":
    # Check financial source credibility
    # Detect market manipulation
    # Verify regulatory compliance
```

### Planner Agent

**Planning Process:**
1. Analyze research findings
2. Identify action items
3. Detect simulation opportunities
4. Create structured plan

**Simulation Detection:**
- Keywords: "predict", "what if", "scenario", "impact"
- Uncertainty level: High
- Recommendation: Suggest simulation mode

### Synthesizer Agent

**Synthesis Process:**
1. Combine all agent outputs
2. Quantify uncertainty
3. Generate final answer
4. Add metadata (confidence, sources, etc.)

**Output Structure:**
```json
{
  "summary": "Final answer",
  "confidence": 0.85,
  "uncertainty_factors": [...],
  "sources": [...],
  "simulation_recommended": false
}
```

## Layer 4: Simulation Lab Integration

### MiroFish Adapter

Adapter pattern for simulation integration:

```python
class MiroFishClient:
    async def submit_simulation(self, title, seed_text, prediction_goal):
        """Submit simulation to MiroFish"""
        
    async def get_status(self, simulation_id):
        """Get simulation status"""
        
    async def get_report(self, simulation_id):
        """Get simulation report"""
        
    async def chat(self, simulation_id, message):
        """Chat with simulation"""
```

### Case-Simulation Linking

Cases and simulations are linked:
```json
{
  "case_id": "uuid",
  "simulation_id": "uuid",
  "user_input": "...",
  "route": {...},
  "outputs": {...},
  "final_answer": "..."
}
```

### Simulation Workflow

```
User Input with Simulation Keywords
    ↓
Switchboard (task_family="simulation")
    ↓
Research (gather context)
    ↓
Submit to MiroFish
    ↓
Poll for completion
    ↓
Synthesize results
    ↓
Return to user
```

## Layer 5: Autonomous Knowledge Evolution

### Knowledge Ingestion

**Sources:**
- Web search (Tavily)
- News (NewsAPI)
- URLs (Jina Reader)

**Process:**
1. Fetch content from source
2. Compress to 2-4KB summary using LLM
3. Store with metadata
4. Enforce 200MB storage limit
5. LRU eviction when limit reached

**Scheduling:**
- Every 6 hours (configurable)
- Only when system idle (CPU <50%)
- Only when battery OK (>30%)

### Experience Learning

**Learning from Cases:**
1. Extract metadata from case execution
2. Detect patterns (domain, sources, agents)
3. Track route effectiveness
4. Track prompt performance
5. Track provider reliability

**Pattern Detection:**
- Domain expertise patterns
- Preferred source patterns
- Agent workflow patterns
- Minimum frequency: 3 occurrences

### Prompt Evolution

**A/B Testing Process:**
1. Create prompt variant using LLM
2. Test variant with sample inputs
3. Measure quality metrics
4. Compare win rates
5. Promote if criteria met (>70% win rate, >10 tests)

**Versioning:**
```json
{
  "id": "uuid",
  "prompt_name": "research",
  "version": 2,
  "status": "testing",
  "test_count": 15,
  "win_count": 12,
  "win_rate": 0.80
}
```

### Skill Distillation

**Skill Creation:**
1. Detect patterns in successful cases
2. Distill into reusable skills
3. Test skills with validation cases
4. Track usage and success rate

**Skill Structure:**
```json
{
  "id": "domain_expertise_finance",
  "type": "domain_expertise",
  "trigger_patterns": ["stock", "market"],
  "recommended_agents": ["research", "verifier"],
  "preferred_sources": ["bloomberg", "reuters"],
  "success_rate": 0.85
}
```

### Trust Management

**Source Trust Scoring:**
- Initial score: 0.5 (neutral)
- Updated based on verification outcomes
- Exponential moving average
- Minimum verifications: 3

**Trust Categories:**
- Trusted: score β‰₯0.7, verifications β‰₯3
- Untrusted: score ≀0.3, verifications β‰₯3
- Unknown: verifications <3

### Freshness Management

**Domain-Specific Expiration:**
- Finance: 7-day half-life
- General: 30-day half-life
- Exponential decay: `freshness = 2^(-age_days / half_life_days)`

**Refresh Recommendations:**
- Freshness <0.3: Stale
- Prioritize by staleness
- Automatic refresh during scheduled ingestion

### Learning Scheduler

**Safeguards:**
- CPU usage check: <50%
- Battery level check: >30%
- System idle detection
- Error handling with backoff

**Scheduled Tasks:**
- Knowledge ingestion: Every 6 hours
- Expired cleanup: Daily
- Pattern detection: Daily
- Skill distillation: Weekly
- Prompt optimization: Weekly

## Data Flow

### Normal Request Flow

```
1. User submits request
2. Switchboard classifies (4D)
3. Domain pack detected
4. Research gathers info (domain-enhanced)
5. Planner creates plan
6. Verifier validates (domain-enhanced)
7. Synthesizer produces answer
8. Case saved
9. Learning extracts metadata
```

### Simulation Request Flow

```
1. User submits request with simulation keywords
2. Switchboard detects simulation
3. Research gathers context
4. Submit to MiroFish
5. Poll for completion
6. Synthesize results
7. Case and simulation saved
8. Learning extracts metadata
```

### Learning Flow

```
1. Scheduler checks system conditions
2. If idle and battery OK:
   a. Ingest knowledge from sources
   b. Compress to 2-4KB summaries
   c. Store with metadata
   d. Enforce storage limits
3. Detect patterns in recent cases
4. Distill skills from patterns
5. Optimize prompts via A/B testing
6. Update trust scores
7. Calculate freshness scores
```

## Deployment Considerations

### Single-User Local (Recommended)

**Optimizations:**
- JSON storage (no database)
- Learning scheduler with safeguards
- 200MB knowledge cache limit
- CPU/battery awareness
- Automatic LRU eviction

**Hardware Requirements:**
- 8GB RAM minimum
- 256GB storage minimum
- M2 Air or equivalent

### Multi-User Production

**Required Changes:**
- PostgreSQL for storage
- Redis for caching
- Authentication/authorization
- Rate limiting
- Request queuing
- Monitoring/alerting
- Horizontal scaling

## Security

### API Key Management

- All keys in environment variables
- Never committed to source control
- Validated on startup

### Error Handling

- Sanitized error messages
- No internal details leaked
- Comprehensive logging

### Input Validation

- Pydantic schemas for all inputs
- Type checking
- Length limits
- Sanitization

## Performance

### Response Times

- Simple queries: <5s
- Medium queries: <15s
- Complex queries: <30s
- Simulations: 60-120s

### Optimization Strategies

- Connection pooling for external APIs
- Request timeouts (30s)
- Caching for market quotes (5min TTL)
- Rate limiting for external APIs
- Async/await throughout

## Monitoring

### Health Checks

- `/health` - Basic health
- `/health/deep` - Detailed health with provider status

### Metrics

- Case execution time
- Provider success/failure rates
- Domain pack usage
- Learning task completion
- Storage usage

### Logging

- Structured logging
- Log levels: DEBUG, INFO, WARNING, ERROR
- Rotation and retention
- Provider fallback events
- Learning task events