@@ -1,481 +0,0 @@ |
| 1 | | -# 🧠 Tier 6 Intelligence: Advanced ML-Inspired Fallback Systems |
| 2 | | - |
| 3 | | -## Overview |
| 4 | | - |
| 5 | | -Parrot's Tier 6 Intelligence represents the **most advanced fallback insult generation system** ever implemented in a CLI tool. These systems go **far beyond** simple template matching or even Tier 5's ensemble ML methods. They implement cutting-edge techniques inspired by modern machine learning, including: |
| 6 | | - |
| 7 | | -- **Contextual Memory Graphs** (relationship tracking) |
| 8 | | -- **Adversarial Generation** (GAN-inspired generator vs. critic) |
| 9 | | -- **Edit Distance Matching** (adaptive insult reuse) |
| 10 | | -- **Contextual Embeddings** (vector-space semantics) |
| 11 | | -- **Reinforcement Learning** (quality feedback loops) |
| 12 | | - |
| 13 | | -## Architecture |
| 14 | | - |
| 15 | | -``` |
| 16 | | -┌─────────────────────────────────────────────────────────────────┐ |
| 17 | | -│ TIER 6 INTELLIGENCE │ |
| 18 | | -│ │ |
| 19 | | -│ ┌──────────────┐ ┌─────────────┐ ┌──────────────────────┐ │ |
| 20 | | -│ │ Contextual │ │ Adversarial │ │ Edit Distance │ │ |
| 21 | | -│ │ Memory │→ │ Generator │→ │ Matcher │ │ |
| 22 | | -│ │ Graph │ │ (GAN-like) │ │ (Levenshtein) │ │ |
| 23 | | -│ └──────────────┘ └─────────────┘ └──────────────────────┘ │ |
| 24 | | -│ ↓ ↓ ↓ │ |
| 25 | | -│ ┌──────────────────────────────────────────────────────────┐ │ |
| 26 | | -│ │ Reinforcement Learning Feedback Loop │ │ |
| 27 | | -│ │ (Tracks effectiveness, adjusts weights, learns) │ │ |
| 28 | | -│ └──────────────────────────────────────────────────────────┘ │ |
| 29 | | -│ ↓ │ |
| 30 | | -│ ┌──────────────────────────────────────────────────────────┐ │ |
| 31 | | -│ │ Contextual Vector Embeddings │ │ |
| 32 | | -│ │ (32-dimensional semantic space) │ │ |
| 33 | | -│ └──────────────────────────────────────────────────────────┘ │ |
| 34 | | -└─────────────────────────────────────────────────────────────────┘ |
| 35 | | - ↓ |
| 36 | | - ┌───────────────────────┐ |
| 37 | | - │ Smart Fallback │ |
| 38 | | - │ Insult Selection │ |
| 39 | | - └───────────────────────┘ |
| 40 | | -``` |
| 41 | | - |
| 42 | | -## Components |
| 43 | | - |
| 44 | | -### 1. Contextual Memory Graph |
| 45 | | - |
| 46 | | -**File:** `internal/llm/contextual_memory_graph.go` |
| 47 | | - |
| 48 | | -**What It Does:** |
| 49 | | -Tracks failure contexts as a **directed graph** where nodes are contexts (command type + error + project) and edges represent transitions between failures. |
| 50 | | - |
| 51 | | -**Key Features:** |
| 52 | | -- **Transition Detection**: Recognizes when user fails command A, then command B |
| 53 | | -- **Specialized Insult Pools**: Each context node has its own pool of effective insults |
| 54 | | -- **Weighted Edges**: Tracks how often specific failure sequences occur |
| 55 | | -- **Dynamic Learning**: Insult effectiveness scores updated via RL |
| 56 | | - |
| 57 | | -**Example:** |
| 58 | | -``` |
| 59 | | -User runs: git push origin main (fails) |
| 60 | | -Then runs: git pull origin main (fails) |
| 61 | | - |
| 62 | | -Graph learns: git_push → git_pull is a common sequence |
| 63 | | -Special insult: "Still can't sync? Maybe git isn't the problem." |
| 64 | | -``` |
| 65 | | - |
| 66 | | -**Data Structures:** |
| 67 | | -```go |
| 68 | | -type ContextNode struct { |
| 69 | | - ID string |
| 70 | | - Transitions map[string]*Transition // Edges to other contexts |
| 71 | | - InsultPool []WeightedInsult // Context-specific insults |
| 72 | | - VisitCount int // How often we see this |
| 73 | | -} |
| 74 | | - |
| 75 | | -type Transition struct { |
| 76 | | - ToContext string |
| 77 | | - Count int // How many times |
| 78 | | - AvgTimeBetween float64 // Seconds between failures |
| 79 | | - SpecialInsults []string // Sequence-specific insults |
| 80 | | -} |
| 81 | | -``` |
| 82 | | - |
| 83 | | -**Persistence:** Saves to `~/.parrot/context_graph.json` |
| 84 | | - |
| 85 | | ---- |
| 86 | | - |
| 87 | | -### 2. Adversarial Insult Generator |
| 88 | | - |
| 89 | | -**File:** `internal/llm/adversarial_generator.go` |
| 90 | | - |
| 91 | | -**What It Does:** |
| 92 | | -Implements a **GAN-inspired** (Generative Adversarial Network) system where: |
| 93 | | -- **Generator** creates insult candidates |
| 94 | | -- **Critic** scores them on multiple dimensions |
| 95 | | -- Iterative improvement through adversarial training |
| 96 | | - |
| 97 | | -**Key Features:** |
| 98 | | -- **Multi-Strategy Generation**: |
| 99 | | - - Template-based (safe, consistent) |
| 100 | | - - Composite (semantic building blocks) |
| 101 | | - - Markov chain (creative, novel) |
| 102 | | -- **5-Dimensional Quality Scoring**: |
| 103 | | - - Relevance (context match) |
| 104 | | - - Novelty (uniqueness) |
| 105 | | - - Brutality (savage level) |
| 106 | | - - Coherence (makes sense) |
| 107 | | - - Length (appropriate size) |
| 108 | | -- **Adaptive Creativity**: Adjusts mode based on performance |
| 109 | | - - Safe: 70% templates, 30% Markov |
| 110 | | - - Balanced: 40% templates, 40% composite, 20% Markov |
| 111 | | - - Wild: 60% Markov, 30% composite, 10% templates |
| 112 | | - |
| 113 | | -**Composite Template Engine:** |
| 114 | | -Builds insults from semantic components: |
| 115 | | -``` |
| 116 | | -[Subject] + [Verb] + [Object] + [Punchline] |
| 117 | | - |
| 118 | | -"Your docker build" + "failed harder than" + "your career" + "combined" |
| 119 | | -= "Your docker build failed harder than your career combined." |
| 120 | | -``` |
| 121 | | - |
| 122 | | -**Component Libraries:** |
| 123 | | -- **Subjects**: "Your code", "This commit", "Your build"... |
| 124 | | -- **Verbs**: "failed harder than", "crashed like", "died faster than"... |
| 125 | | -- **Objects**: "your career", "a burning dumpster", "your hopes"... |
| 126 | | -- **Punchlines**: "combined", "on steroids", "in production"... |
| 127 | | - |
| 128 | | -**Quality Scoring Example:** |
| 129 | | -``` |
| 130 | | -Insult: "Your git push failed harder than your last deployment." |
| 131 | | - |
| 132 | | -Relevance: 0.9 (mentions git push, deployment) |
| 133 | | -Novelty: 0.8 (first time used) |
| 134 | | -Brutality: 0.7 (mentions failure, deployment) |
| 135 | | -Coherence: 1.0 (well-formed, punctuation) |
| 136 | | -Length: 1.0 (53 chars, optimal range) |
| 137 | | -───────────────── |
| 138 | | -Overall: 0.85 (weighted combination) |
| 139 | | -``` |
| 140 | | - |
| 141 | | ---- |
| 142 | | - |
| 143 | | -### 3. Edit Distance Matcher |
| 144 | | - |
| 145 | | -**File:** `internal/llm/edit_distance_matcher.go` |
| 146 | | - |
| 147 | | -**What It Does:** |
| 148 | | -Finds **similar past command failures** using Levenshtein distance and adapts their successful insults to the current context. |
| 149 | | - |
| 150 | | -**Key Features:** |
| 151 | | -- **Levenshtein Distance**: Calculates edit distance between commands |
| 152 | | -- **Similarity Threshold**: 70% similarity required (configurable) |
| 153 | | -- **Insult Adaptation**: Replaces command-specific parts |
| 154 | | -- **Effectiveness Tracking**: Remembers which insults worked |
| 155 | | - |
| 156 | | -**Example:** |
| 157 | | -``` |
| 158 | | -Past failure: "git push origin feature-123" |
| 159 | | - Insult: "feature-123 rejected harder than your PR comments." |
| 160 | | - |
| 161 | | -Current: "git push origin bugfix-456" |
| 162 | | - |
| 163 | | -Adapted: "bugfix-456 rejected harder than your PR comments." |
| 164 | | -``` |
| 165 | | - |
| 166 | | -**Algorithm:** |
| 167 | | -1. Tokenize commands: `git push origin feature` → [`git`, `push`, `origin`, `feature`] |
| 168 | | -2. Calculate Levenshtein distance for all historical commands |
| 169 | | -3. Normalize: `similarity = 1.0 - (distance / max_length)` |
| 170 | | -4. Select top matches above threshold (0.7) |
| 171 | | -5. Adapt insult by replacing unique tokens |
| 172 | | - |
| 173 | | -**Data Structure:** |
| 174 | | -```go |
| 175 | | -type CommandRecord struct { |
| 176 | | - Command string |
| 177 | | - Insult string |
| 178 | | - Effectiveness float64 // RL score |
| 179 | | - Timestamp time.Time |
| 180 | | -} |
| 181 | | -``` |
| 182 | | - |
| 183 | | -**Persistence:** Saves to `~/.parrot/command_history.json` |
| 184 | | - |
| 185 | | ---- |
| 186 | | - |
| 187 | | -### 4. Contextual Vector Embeddings |
| 188 | | - |
| 189 | | -**File:** `internal/llm/contextual_embeddings.go` |
| 190 | | - |
| 191 | | -**What It Does:** |
| 192 | | -Represents failure contexts as **32-dimensional vectors** in semantic space, enabling similarity matching without external ML libraries. |
| 193 | | - |
| 194 | | -**Key Features:** |
| 195 | | -- **32 Dimensions** divided into categories: |
| 196 | | - - Dims 0-7: Command type (git, docker, node...) |
| 197 | | - - Dims 8-15: Error pattern (permission, network, timeout...) |
| 198 | | - - Dims 16-19: Project context (language, git branch, dependencies) |
| 199 | | - - Dims 20-23: Temporal context (time of day, repeated failures) |
| 200 | | - - Dims 24-27: Environmental (CI/CD, shell type, complexity) |
| 201 | | - - Dims 28-31: Behavioral patterns (working dir, risky commands) |
| 202 | | - |
| 203 | | -- **Cosine Similarity**: Measures context similarity |
| 204 | | -- **Feature Importance**: Shows which features contributed most |
| 205 | | -- **Normalized Vectors**: Unit vectors for consistent comparison |
| 206 | | - |
| 207 | | -**Encoding Examples:** |
| 208 | | - |
| 209 | | -**Time of Day (Cyclical):** |
| 210 | | -``` |
| 211 | | -Hour 0 (midnight): sin(0) = 0.0, cos(0) = 1.0 |
| 212 | | -Hour 6 (6am): sin(π/2) = 1.0, cos(π/2) = 0.0 |
| 213 | | -Hour 12 (noon): sin(π) = 0.0, cos(π) = -1.0 |
| 214 | | -``` |
| 215 | | -This ensures hour 23 is "close" to hour 0 in vector space. |
| 216 | | - |
| 217 | | -**One-Hot Encoding:** |
| 218 | | -``` |
| 219 | | -Command: "docker" |
| 220 | | -Vector[0-7]: [0, 1, 0, 0, 0, 0, 0, 0] (docker = index 1) |
| 221 | | -``` |
| 222 | | - |
| 223 | | -**Similarity Calculation:** |
| 224 | | -```go |
| 225 | | -cosine_similarity = dot_product(v1, v2) / (magnitude(v1) * magnitude(v2)) |
| 226 | | - |
| 227 | | -Range: -1.0 (opposite) to 1.0 (identical) |
| 228 | | -``` |
| 229 | | - |
| 230 | | -**Use Cases:** |
| 231 | | -- Find similar past contexts for insult reuse |
| 232 | | -- Cluster common failure patterns |
| 233 | | -- Detect unusual/novel failures |
| 234 | | - |
| 235 | | ---- |
| 236 | | - |
| 237 | | -### 5. Reinforcement Learning Simulator |
| 238 | | - |
| 239 | | -**Integration:** Throughout all Tier 6 systems |
| 240 | | - |
| 241 | | -**What It Does:** |
| 242 | | -Simulates **reinforcement learning** by tracking insult effectiveness and adjusting weights accordingly. |
| 243 | | - |
| 244 | | -**Key Metrics:** |
| 245 | | -- **Usage Count**: How often insult is used |
| 246 | | -- **Effectiveness**: How well it worked (user moved on = success) |
| 247 | | -- **Recency**: When last used (for novelty) |
| 248 | | -- **Success Rate**: Exponential moving average of outcomes |
| 249 | | - |
| 250 | | -**Feedback Loop:** |
| 251 | | -``` |
| 252 | | -1. Insult delivered → Record use time |
| 253 | | -2. User fails again → Effectiveness decreases (didn't help) |
| 254 | | -3. User succeeds → Effectiveness increases (it worked!) |
| 255 | | -4. User changes command → Success! (they learned/adapted) |
| 256 | | -``` |
| 257 | | - |
| 258 | | -**Weight Updates:** |
| 259 | | -```go |
| 260 | | -// Exponential moving average |
| 261 | | -effectiveness = alpha * new_score + (1-alpha) * old_effectiveness |
| 262 | | - |
| 263 | | -// Boost recently successful insults |
| 264 | | -if time_since_use < 5_minutes && user_succeeded { |
| 265 | | - effectiveness = 0.2 * 1.0 + 0.8 * effectiveness |
| 266 | | -} |
| 267 | | - |
| 268 | | -// Decay overused insults |
| 269 | | -dynamic_weight = decay * dynamic_weight + (1-decay) * base_weight |
| 270 | | -``` |
| 271 | | - |
| 272 | | -**Persistence:** |
| 273 | | -- Contextual Graph tracks insult pool effectiveness |
| 274 | | -- Edit Distance Matcher tracks command record effectiveness |
| 275 | | -- Both save to disk periodically |
| 276 | | - |
| 277 | | ---- |
| 278 | | - |
| 279 | | -## Integration & Execution Flow |
| 280 | | - |
| 281 | | -### When a Command Fails: |
| 282 | | - |
| 283 | | -``` |
| 284 | | -1. Parse context (command, error, project, time, etc.) |
| 285 | | - ↓ |
| 286 | | -2. TIER 6 INTELLIGENCE (Priority Order): |
| 287 | | - |
| 288 | | - a) Contextual Memory Graph |
| 289 | | - - Record this context |
| 290 | | - - Check for failure sequence (transition) |
| 291 | | - - If transition found → Use special sequence insult |
| 292 | | - ✓ Return if found |
| 293 | | - |
| 294 | | - b) Adversarial Generator (GAN-inspired) |
| 295 | | - - Generate 5 candidates (generator) |
| 296 | | - - Score each (critic: relevance, novelty, brutality, coherence, length) |
| 297 | | - - Select best scoring candidate |
| 298 | | - - If quality >= 0.6 threshold → Return |
| 299 | | - ✓ Record in graph & edit distance matcher |
| 300 | | - |
| 301 | | - c) Edit Distance Matcher |
| 302 | | - - Find similar past commands (Levenshtein distance) |
| 303 | | - - Adapt their successful insults to current context |
| 304 | | - - If similarity >= 0.7 threshold → Return |
| 305 | | - ✓ Record in graph |
| 306 | | - |
| 307 | | - d) Contextual Graph Memory |
| 308 | | - - Check context-specific insult pool |
| 309 | | - - Select based on effectiveness & novelty |
| 310 | | - ✓ Return if found |
| 311 | | - |
| 312 | | - e) Fall through to TIER 5 (Ensemble ML) |
| 313 | | - ✓ Record Tier 5 result in Tier 6 systems for learning |
| 314 | | - |
| 315 | | -3. Record insult use for RL |
| 316 | | - ↓ |
| 317 | | -4. Track user outcome (next command = success/failure) |
| 318 | | - ↓ |
| 319 | | -5. Update effectiveness scores |
| 320 | | - ↓ |
| 321 | | -6. Save to disk periodically |
| 322 | | -``` |
| 323 | | - |
| 324 | | ---- |
| 325 | | - |
| 326 | | -## Performance & Optimization |
| 327 | | - |
| 328 | | -### Memory Usage |
| 329 | | -- **Contextual Graph**: ~10 KB per 100 contexts |
| 330 | | -- **Edit Distance Matcher**: ~5 KB per 100 commands |
| 331 | | -- **Embeddings**: 256 bytes per embedding (32 dimensions × 8 bytes) |
| 332 | | -- **Total Overhead**: ~50-100 KB for typical use |
| 333 | | - |
| 334 | | -### Computational Cost |
| 335 | | -- **Graph Operations**: O(1) lookup, O(n) for transitions |
| 336 | | -- **Adversarial Generation**: O(k) where k = candidates (default 5) |
| 337 | | -- **Edit Distance**: O(n × m × l) where n = history size, m = avg command length |
| 338 | | -- **Embedding Creation**: O(1) (32 dimensions, fixed) |
| 339 | | -- **Cosine Similarity**: O(d) where d = dimensions (32) |
| 340 | | - |
| 341 | | -### Optimizations |
| 342 | | -- **Async Training**: Ensemble trains in background |
| 343 | | -- **Lazy Loading**: Systems initialize on first use |
| 344 | | -- **Periodic Saves**: Only save every N operations |
| 345 | | -- **Decay Schedule**: Hourly background task |
| 346 | | -- **Caching**: Vector magnitudes pre-computed |
| 347 | | - |
| 348 | | ---- |
| 349 | | - |
| 350 | | -## Novelty & Innovation |
| 351 | | - |
| 352 | | -### What Makes This Unique? |
| 353 | | - |
| 354 | | -**1. Never Before Seen in CLI Tools:** |
| 355 | | -- GAN-inspired adversarial generation for text |
| 356 | | -- Contextual memory graphs for failure sequences |
| 357 | | -- RL-based effectiveness tracking |
| 358 | | -- Multi-dimensional quality scoring |
| 359 | | - |
| 360 | | -**2. No External Dependencies:** |
| 361 | | -- Pure Go implementation |
| 362 | | -- No TensorFlow, PyTorch, or external ML libraries |
| 363 | | -- Lightweight vector embeddings (32D) |
| 364 | | -- Custom Levenshtein distance implementation |
| 365 | | - |
| 366 | | -**3. Production-Ready:** |
| 367 | | -- Persistent storage (JSON) |
| 368 | | -- Atomic writes (tmp → rename) |
| 369 | | -- Concurrent-safe (sync.RWMutex) |
| 370 | | -- Graceful degradation (fallback to lower tiers) |
| 371 | | - |
| 372 | | -**4. Self-Improving:** |
| 373 | | -- Learns from user behavior |
| 374 | | -- Adapts personality based on effectiveness |
| 375 | | -- Discovers new insult combinations |
| 376 | | -- Tracks what works, forgets what doesn't |
| 377 | | - |
| 378 | | ---- |
| 379 | | - |
| 380 | | -## Configuration & Tuning |
| 381 | | - |
| 382 | | -### Contextual Memory Graph |
| 383 | | -```go |
| 384 | | -decayFactor: 0.98 // How quickly weights decay |
| 385 | | -minEffectiveness: 0.3 // Minimum to keep in pool |
| 386 | | -maxPoolSize: 50 // Max insults per context |
| 387 | | -``` |
| 388 | | - |
| 389 | | -### Adversarial Generator |
| 390 | | -```go |
| 391 | | -minQuality: 0.6 // Minimum overall score |
| 392 | | -targetQuality: 0.8 // Target for early exit |
| 393 | | -creativityMode: "balanced" // safe|balanced|wild |
| 394 | | -``` |
| 395 | | - |
| 396 | | -### Edit Distance Matcher |
| 397 | | -```go |
| 398 | | -maxHistory: 1000 // Max commands to remember |
| 399 | | -similarityThresh: 0.7 // 70% similarity required |
| 400 | | -``` |
| 401 | | - |
| 402 | | -### Embedding Engine |
| 403 | | -```go |
| 404 | | -dimensions: 32 // Vector dimensions |
| 405 | | -``` |
| 406 | | - |
| 407 | | ---- |
| 408 | | - |
| 409 | | -## Future Enhancements |
| 410 | | - |
| 411 | | -### Potential Improvements |
| 412 | | -1. **LSTM-based Sequence Learning**: Predict next failure |
| 413 | | -2. **Transfer Learning**: Learn from other users (privacy-preserving) |
| 414 | | -3. **Multi-Armed Bandit**: Optimal insult selection |
| 415 | | -4. **Attention Mechanisms**: Focus on important context features |
| 416 | | -5. **Clustering**: Auto-discover failure patterns |
| 417 | | -6. **Anomaly Detection**: Detect unusual errors |
| 418 | | -7. **Meta-Learning**: Learn to learn faster |
| 419 | | - |
| 420 | | -### Research Opportunities |
| 421 | | -- **Benchmark against LLMs**: Compare quality to GPT-4 |
| 422 | | -- **A/B Testing**: Measure user improvement |
| 423 | | -- **Psychological Impact**: Does brutality correlate with learning? |
| 424 | | -- **Cross-Domain Transfer**: Can git failures predict docker failures? |
| 425 | | - |
| 426 | | ---- |
| 427 | | - |
| 428 | | -## Statistics & Monitoring |
| 429 | | - |
| 430 | | -Access system stats via debug mode: |
| 431 | | - |
| 432 | | -```bash |
| 433 | | -PARROT_DEBUG=true ./parrot mock "git push" "1" |
| 434 | | -``` |
| 435 | | - |
| 436 | | -**Contextual Graph Stats:** |
| 437 | | -- Total contexts tracked |
| 438 | | -- Total transitions recorded |
| 439 | | -- Insult pool sizes |
| 440 | | -- Average effectiveness |
| 441 | | - |
| 442 | | -**Adversarial Generator Stats:** |
| 443 | | -- Generator score (how well it fools critic) |
| 444 | | -- Critic score (how well it evaluates) |
| 445 | | -- Training rounds completed |
| 446 | | -- Current creativity mode |
| 447 | | -- Unique insults generated |
| 448 | | - |
| 449 | | -**Edit Distance Stats:** |
| 450 | | -- Total commands in history |
| 451 | | -- Average effectiveness |
| 452 | | -- Similarity threshold |
| 453 | | - |
| 454 | | ---- |
| 455 | | - |
| 456 | | -## Academic References |
| 457 | | - |
| 458 | | -This implementation is inspired by: |
| 459 | | - |
| 460 | | -1. **GANs** (Goodfellow et al., 2014): Adversarial training |
| 461 | | -2. **BM25** (Robertson & Zaragoza, 2009): Text ranking |
| 462 | | -3. **Levenshtein Distance** (Levenshtein, 1966): Edit distance |
| 463 | | -4. **Word2Vec** (Mikolov et al., 2013): Vector embeddings |
| 464 | | -5. **Q-Learning** (Watkins, 1989): Reinforcement learning |
| 465 | | -6. **PageRank** (Page et al., 1998): Graph-based ranking |
| 466 | | - |
| 467 | | ---- |
| 468 | | - |
| 469 | | -## Conclusion |
| 470 | | - |
| 471 | | -Tier 6 Intelligence represents a **quantum leap** in fallback system sophistication. By combining graph theory, information retrieval, reinforcement learning, and adversarial generation, parrot delivers insults that: |
| 472 | | - |
| 473 | | -✅ **Adapt** to user patterns |
| 474 | | -✅ **Learn** from effectiveness |
| 475 | | -✅ **Generate** novel combinations |
| 476 | | -✅ **Remember** what works |
| 477 | | -✅ **Rival LLM quality** without external APIs |
| 478 | | - |
| 479 | | -This is not just a fallback system—it's a **self-improving, context-aware, intelligent insult engine** that pushes the boundaries of what's possible in CLI tooling. |
| 480 | | - |
| 481 | | -**Status:** Production-ready, battle-tested, ready to roast users at scale. 🔥 |