Spaces:
Sleeping
Sleeping
File size: 19,109 Bytes
f9b1ad5 |
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 |
# ToGMAL Architecture
## System Overview
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Claude Desktop β
β (or other MCP Client) β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β stdio/MCP Protocol
β
ββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββ
β ToGMAL MCP Server β
β (togmal_mcp.py) β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β MCP Tools Layer β β
β β - togmal_analyze_prompt β β
β β - togmal_analyze_response β β
β β - togmal_submit_evidence β β
β β - togmal_get_taxonomy β β
β β - togmal_get_statistics β β
β ββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββ β
β β β
β ββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββ β
β β Detection Heuristics β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β Math/Physics Speculation Detector β β β
β β β - Pattern: "theory of everything" β β β
β β β - Pattern: "new equation" β β β
β β β - Pattern: excessive notation β β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β Ungrounded Medical Advice Detector β β β
β β β - Pattern: "you probably have" β β β
β β β - Pattern: "take Xmg" β β β
β β β - Check: has_sources β β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β Dangerous File Operations Detector β β β
β β β - Pattern: "rm -rf" β β β
β β β - Pattern: recursive deletion β β β
β β β - Check: has_safeguards β β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β Vibe Coding Overreach Detector β β β
β β β - Pattern: "complete app" β β β
β β β - Pattern: large line counts β β β
β β β - Check: has_planning β β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β Unsupported Claims Detector β β β
β β β - Pattern: "always/never" β β β
β β β - Pattern: statistics without source β β β
β β β - Check: has_hedging β β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β ββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββ β
β β β
β ββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββ β
β β Risk Assessment & Interventions β β
β β - Calculate weighted risk score β β
β β - Map to risk levels (LOW β CRITICAL) β β
β β - Recommend interventions β β
β ββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββ β
β β β
β ββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββ β
β β Taxonomy Database β β
β β - In-memory storage (extendable to persistent) β β
β β - Evidence entries with metadata β β
β β - Filtering and pagination β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
## Data Flow - Prompt Analysis
```
User Prompt
β
βββββββββββββββββββββββββββββββββββββββββββββββ
β β
βΌ β
togmal_analyze_prompt β
β β
ββββΊ Math/Physics Detector βββΊ Result 1 β
β β
ββββΊ Medical Advice Detector βββΊ Result 2 β
β β
ββββΊ File Ops Detector βββΊ Result 3 β
β β
ββββΊ Vibe Coding Detector βββΊ Result 4 β
β β
ββββΊ Unsupported Claims Detector βββΊ Result 5β
β
βββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
Risk Calculation
β
βββΊ Weight results
βββΊ Calculate score
βββΊ Map to risk level
β
βΌ
Intervention Recommendation
β
βββΊ Step breakdown?
βββΊ Human-in-loop?
βββΊ Web search?
βββΊ Simplified scope?
β
βΌ
Format Response (Markdown/JSON)
β
ββββΊ Return to Client
```
## Detection Pipeline
```
Input Text
β
βΌ
βββββββββββββββββββββββββββββ
β Preprocessing β
β - Lowercase β
β - Strip whitespace β
βββββββββββββ¬ββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββ
β Pattern Matching β
β - Regex patterns β
β - Keyword detection β
β - Structural analysis β
βββββββββββββ¬ββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββ
β Confidence Scoring β
β - Count matches β
β - Weight by type β
β - Normalize to [0,1] β
βββββββββββββ¬ββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββ
β Context Checks β
β - has_sources? β
β - has_hedging? β
β - has_safeguards? β
βββββββββββββ¬ββββββββββββββββ
β
βΌ
Detection Result
{
detected: bool,
categories: list,
confidence: float,
metadata: dict
}
```
## Risk Calculation Algorithm
```
For each detection category:
Math/Physics:
risk += confidence Γ 0.5
Medical Advice:
risk += confidence Γ 1.5 # Highest weight
File Operations:
risk += confidence Γ 2.0 # Critical actions
Vibe Coding:
risk += confidence Γ 0.4
Unsupported Claims:
risk += confidence Γ 0.3
Total Risk Score:
β₯ 1.5 β CRITICAL
β₯ 1.0 β HIGH
β₯ 0.5 β MODERATE
< 0.5 β LOW
```
## Intervention Decision Tree
```
Detection Results
β
βββββββββββββββββββΌββββββββββββββββββ
β β β
βΌ βΌ βΌ
Math/Physics? Medical Advice? File Operations?
β β β
βββΊ Yes βββΊ Yes βββΊ Yes
β β β β β β
β βββΊ Step β βββΊ Human β βββΊ Human
β β Breakdown β β in Loop β β in Loop
β β β β β β
β βββΊ Web β βββΊ Web β βββΊ Step
β Search β Search β Breakdown
β β β
βββΊ No βββΊ No βββΊ No
β β β
βΌ βΌ βΌ
Continue Continue Continue
βββββββββββββ
β Combine β
β Results β
βββββββ¬ββββββ
β
βΌ
Intervention List
(deduplicated)
```
## Taxonomy Database Schema
```
TAXONOMY_DB = {
"category_name": [
{
"id": "abc123def456",
"category": "math_physics_speculation",
"prompt": "User's prompt text...",
"response": "LLM's response text...",
"description": "Why problematic...",
"severity": "high",
"timestamp": "2025-10-18T00:00:00",
"prompt_hash": "a1b2c3d4"
},
{ ... more entries ... }
],
"another_category": [ ... ]
}
Indices:
- By category (dict key)
- By severity (filter)
- By timestamp (sort)
- By hash (deduplication)
```
## Component Responsibilities
### MCP Tools Layer
**Responsibilities:**
- Input validation (Pydantic models)
- Parameter extraction
- Tool orchestration
- Response formatting
- Character limit enforcement
**Does NOT:**
- Perform detection logic
- Calculate risk scores
- Store data directly
### Detection Heuristics Layer
**Responsibilities:**
- Pattern matching
- Confidence scoring
- Context analysis
- Detection result generation
**Does NOT:**
- Make intervention decisions
- Format responses
- Handle I/O
### Risk Assessment Layer
**Responsibilities:**
- Aggregate detection results
- Calculate weighted risk scores
- Map scores to risk levels
- Generate intervention recommendations
**Does NOT:**
- Perform detection
- Format responses
- Store data
### Taxonomy Database
**Responsibilities:**
- Store evidence entries
- Support filtering/pagination
- Provide statistics
- Maintain capacity limits
**Does NOT:**
- Perform analysis
- Make decisions
- Format responses
## Extension Points
### Adding New Detection Categories
```python
# 1. Add enum value
class CategoryType(str, Enum):
NEW_CATEGORY = "new_category"
# 2. Create detector function
def detect_new_category(text: str) -> Dict[str, Any]:
patterns = { ... }
# Detection logic
return {
'detected': bool,
'categories': list,
'confidence': float
}
# 3. Update analysis functions
def analyze_prompt(params):
results['new_category'] = detect_new_category(params.prompt)
# ... rest of logic
# 4. Update risk calculation
def calculate_risk_level(results):
if results['new_category']['detected']:
risk_score += results['new_category']['confidence'] * WEIGHT
# 5. Add intervention logic
def recommend_interventions(results):
if results['new_category']['detected']:
interventions.append({ ... })
```
### Adding Persistent Storage
```python
# 1. Define storage backend
class TaxonomyStorage:
def save(self, category, entry): ...
def load(self, category, filters): ...
def get_stats(self): ...
# 2. Replace in-memory dict
storage = TaxonomyStorage(backend="sqlite") # or "postgres", "mongodb"
# 3. Update tool functions
@mcp.tool()
async def submit_evidence(params):
# Instead of: TAXONOMY_DB[category].append(entry)
await storage.save(params.category, entry)
```
### Adding ML Models
```python
# 1. Define model interface
class AnomalyDetector:
def fit(self, X): ...
def predict(self, x) -> float: ...
# 2. Train from taxonomy
detector = AnomalyDetector()
training_data = get_training_data_from_taxonomy()
detector.fit(training_data)
# 3. Use in detection
def detect_with_ml(text: str) -> float:
features = extract_features(text)
anomaly_score = detector.predict(features)
return anomaly_score
```
## Performance Characteristics
### Time Complexity
- **Pattern Matching**: O(n) where n = text length
- **All Detectors**: O(n) (parallel constant time)
- **Risk Calculation**: O(1) (fixed number of categories)
- **Taxonomy Query**: O(mΒ·log m) where m = matching entries
- **Overall**: O(n + mΒ·log m)
### Space Complexity
- **Server Base**: ~50 MB
- **Per Request**: ~1 KB (temporary)
- **Per Taxonomy Entry**: ~1 KB
- **Total with 1000 entries**: ~51 MB
### Latency
- **Single Detection**: ~10-50 ms
- **All Detections**: ~50-100 ms
- **Format Response**: ~1-10 ms
- **Total Per Request**: ~100-150 ms
## Security Considerations
### Input Validation
```
User Input
β
βΌ
Pydantic Model
β
βββΊ Type checking
βββΊ Length limits
βββΊ Pattern validation
βββΊ Field constraints
β
βΌ
Valid Input
```
### Privacy Protection
```
ββββββββββββββββββββββββββββββββββββββ
β NO External API Calls β
β NO Data Transmission β
β NO Logging Sensitive Info β
β YES Local Processing Only β
β YES User Consent Required β
β YES Data Stays on Device β
ββββββββββββββββββββββββββββββββββββββ
```
### Human-in-the-Loop
```
Sensitive Operation Detected
β
βΌ
Request User Confirmation
β
βββΊ Yes β Proceed
β
βββΊ No β Cancel
```
## Scalability Path
### Current: Single Instance
```
Client β stdio β ToGMAL Server β Response
```
### Future: HTTP Transport
```
Multiple Clients β HTTP β ToGMAL Server β Response
β
Shared Database
```
### Advanced: Distributed
```
Clients β Load Balancer β ToGMAL Servers (N)
β
Shared Database
β
ML Model Cache
```
## Monitoring Points
```
βββββββββββββββββββββββββββββββββββββββ
β Metrics to Track β
βββββββββββββββββββββββββββββββββββββββ€
β - Tool call frequency β
β - Detection rates by category β
β - Risk level distribution β
β - Intervention effectiveness β
β - False positive rate β
β - Response latency β
β - Taxonomy growth rate β
β - User feedback submissions β
βββββββββββββββββββββββββββββββββββββββ
```
---
This architecture supports:
- β
Privacy-preserving analysis
- β
Low-latency detection
- β
Extensible design
- β
Production readiness
- β
Future ML integration
|