tpmjs/docs/CASCADING_COMPLEXITY_AND_FRAGILITY.md
Ajax Davis 150d48d0ba feat(ui): reimagine Spinner as brutalist grid-based loader
- Replace orbital spinner with 3x3 grid of blocks
- Diagonal wave animation matches dithering aesthetic
- Sharp squares, no rounded corners (brutalist)
- Inline horizontal layout with monospace text
- Consistent styling across all loading states

The new loader evokes "tools being constructed" - fitting
for a tool registry. Uses staggered opacity/scale animation
creating a wave pattern across the grid.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-11 08:19:44 +10:00

1112 lines
90 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Cascading Complexity and Logical Fragility
How adding more steps, conditions, and branches can transform reliable systems into brittle ones—and strategies to maintain robustness.
---
## The Conjunction Fallacy in Tool Plans
The conjunction fallacy (Linda problem) demonstrates that humans intuitively believe:
```
P(A ∧ B) > P(A)
"Linda is a bank teller AND active in the feminist movement"
seems more likely than
"Linda is a bank teller"
```
This is mathematically impossible. Adding conditions can only maintain or reduce probability:
```
P(A ∧ B) ≤ P(A)
```
**Applied to tool plans:**
```
Plan A: 3 steps
─────────────────────────────────────────────────────
Step 1 (P=0.95) → Step 2 (P=0.92) → Step 3 (P=0.90)
P(success) = 0.95 × 0.92 × 0.90 = 0.787 (78.7%)
Plan B: 8 steps
─────────────────────────────────────────────────────
Step 1 (P=0.95) → Step 2 (P=0.92) → Step 3 (P=0.90) → Step 4 (P=0.93)
→ Step 5 (P=0.91) → Step 6 (P=0.94) → Step 7 (P=0.89) → Step 8 (P=0.92)
P(success) = 0.95 × 0.92 × 0.90 × 0.93 × 0.91 × 0.94 × 0.89 × 0.92 = 0.478 (47.8%)
Plan C: 20 steps (each P=0.95)
─────────────────────────────────────────────────────
P(success) = 0.95^20 = 0.358 (35.8%)
```
**More steps = more conjunction = lower probability of complete success**
---
## The Fragility Spectrum
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ ROBUST ◄─────────────────────────────────────────────────────► FRAGILE │
│ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ 1 step │ │ 3 steps │ │ 8 steps │ │15 steps │ │25 steps │ │
│ │ P=0.95 │ │ P=0.78 │ │ P=0.48 │ │ P=0.28 │ │ P=0.13 │ │
│ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │
│ │
│ Simple Standard Complex Enterprise Ambitious │
│ task workflow pipeline process fantasy │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
---
## Types of Fragility in Cascading Systems
### 1. Sequential Fragility
Each step depends on the previous. One failure breaks the chain.
```
┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐
│ A │───►│ B │───►│ C │───►│ D │───►│ E │
└─────┘ └─────┘ └──┬──┘ └─────┘ └─────┘
✗ FAIL
Everything after C is blocked
```
### 2. Conditional Fragility
Branching logic compounds failure modes.
```
┌─────┐
│ A │
└──┬──┘
┌────────┼────────┐
│ │ │
▼ ▼ ▼
┌─────┐ ┌─────┐ ┌─────┐
│if X │ │if Y │ │if Z │
└──┬──┘ └──┬──┘ └──┬──┘
│ │ │
▼ ▼ ▼
┌─────┐ ┌─────┐ ┌─────┐
│ B │ │ C │ │ D │
└─────┘ └─────┘ └─────┘
Each branch is its own failure domain.
Condition evaluation itself can fail.
Wrong branch selection = cascading wrongness.
```
### 3. Accumulation Fragility
Errors compound. Small inaccuracies become large ones.
```
Step 1: Extract data → 2% error rate
Step 2: Transform data → 3% error rate
Step 3: Analyze data → 2% error rate
Step 4: Generate report → 1% error rate
But errors compound:
┌─────────────────────────────────────────────────────────────────┐
│ │
│ Input: 1000 records │
│ │
│ After Step 1: 980 correct, 20 errors introduced │
│ After Step 2: 951 correct, 29 errors (some errors on errors) │
│ After Step 3: 932 correct, 19 new errors + propagated errors │
│ After Step 4: 923 correct, 9 new errors + all previous │
│ │
│ Final accuracy: ~77% (not 92% as naive multiplication suggests)│
│ │
│ Some errors AMPLIFY through the pipeline. │
│ │
└─────────────────────────────────────────────────────────────────┘
```
### 4. Context Fragility
Information loss at each handoff.
```
┌────────────────────────────────────────────────────────────────────────────┐
│ │
│ Original user intent: "Find cheap flights to Tokyo in cherry blossom │
│ season, preferably window seat, vegetarian meal" │
│ │
│ Step 1 output: { destination: "Tokyo", dates: "March-April" } │
│ ↓ │
│ Lost: "cheap", "window seat", "vegetarian" │
│ │
│ Step 2 output: { flights: [...] } │
│ ↓ │
│ Lost: "cherry blossom season" nuance │
│ │
│ Step 3 output: { booking: "confirmed" } │
│ ↓ │
│ User gets: expensive flight, middle seat, regular meal │
│ │
│ Each step loses context. By the end, original intent is unrecognizable. │
│ │
└────────────────────────────────────────────────────────────────────────────┘
```
---
## Occam's Razor Applied to Tool Plans
> "When multiple explanations exist, the one requiring the fewest assumptions
> is the most likely to be true."
**Applied to tool orchestration:**
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ Task: "Convert this CSV to a formatted Excel report" │
│ │
│ PLAN A (Occam's Razor): │
│ ─────────────────────── │
│ ┌─────────────────────────┐ │
│ │ csv-to-xlsx-converter │ ← 1 tool, 1 assumption │
│ │ (handles formatting) │ │
│ └─────────────────────────┘ │
│ │
│ Assumptions: 1 │
│ P(success) ≈ 0.95 │
│ │
│ │
│ PLAN B (Over-engineered): │
│ ───────────────────────── │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ csv-parser │──►│ data-cleaner │──►│ formatter │──►│ xlsx-writer │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
│ Assumptions: 4 │
│ P(success) ≈ 0.95^4 = 0.81 │
│ │
│ │
│ PLAN C (Kitchen sink): │
│ ────────────────────── │
│ csv-parser → validator → type-inferrer → null-handler → normalizer │
│ → enricher → formatter → styler → chart-generator → xlsx-writer │
│ │
│ Assumptions: 10 │
│ P(success) ≈ 0.95^10 = 0.60 │
│ │
│ ═══════════════════════════════════════════════════════════════════════ │
│ Occam says: Use Plan A unless you have specific evidence you need more. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
---
## The Determinism vs. Flexibility Tradeoff
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ DETERMINISTIC FLEXIBLE │
│ ───────────── ──────── │
│ │
│ ┌─────────────────────────┐ ┌─────────────────────────────────────┐ │
│ │ │ │ │ │
│ │ A → B → C → D │ │ A ──┬──► B ──┬──► D │ │
│ │ │ │ │ │ │ │
│ │ Same path every time │ │ └──► C ──┘ │ │
│ │ Predictable │ │ │ │
│ │ Testable │ │ Path varies by context │ │
│ │ Auditable │ │ Adaptive │ │
│ │ │ │ Handles edge cases │ │
│ │ But: Brittle to edge │ │ │ │
│ │ cases │ │ But: Unpredictable │ │
│ │ │ │ Hard to debug │ │
│ └─────────────────────────┘ └─────────────────────────────────────┘ │
│ │
│ │
│ THE LEARNED PATHWAY APPROACH: │
│ ───────────────────────────── │
│ │
│ Start flexible, converge toward deterministic based on usage: │
│ │
│ Week 1 Week 4 Week 12 │
│ ─────── ─────── ──────── │
│ │
│ A ─┬─► B A ─┬─► B A ────► B │
│ │ │ (85%) (99%) │
│ ├─► C │ │ │
│ │ └─► C │ │
│ └─► D (15%) ▼ │
│ C │
│ (99%) │
│ Many paths Dominant path Near-deterministic │
│ explored emerges with escape hatch │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
---
## Strategies for Managing Fragility
### 1. Minimize Conjunction (Fewer Steps)
```
Before: 8 steps
┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐
│ 1 │►│ 2 │►│ 3 │►│ 4 │►│ 5 │►│ 6 │►│ 7 │►│ 8 │
└───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘ └───┘
After: Combine into 3 "super-tools"
┌─────────┐ ┌─────────┐ ┌─────────┐
│ 1,2,3 │►│ 4,5,6 │►│ 7,8 │
└─────────┘ └─────────┘ └─────────┘
Same capability, fewer failure points.
```
### 2. Parallel Over Sequential
```
Sequential (fragile): Parallel (robust):
A → B → C → D A ──┬──► B ──┐
│ │
If B fails, C and D blocked. ├──► C ──┼──► E
│ │
└──► D ──┘
If B fails, C and D still run.
E gets partial results.
```
### 3. Checkpoints and Recovery
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐ │
│ │ A │────►│ B │────►│ C │────►│ D │────►│ E │ │
│ └───┘ └─┬─┘ └───┘ └─┬─┘ └───┘ │
│ │ │ │
│ ▼ ▼ │
│ [CHECKPOINT] [CHECKPOINT] │
│ Save state Save state │
│ │
│ If D fails: │
│ • Don't restart from A │
│ • Resume from checkpoint after B │
│ • Retry only C → D → E │
│ │
│ Reduces effective conjunction from 5 steps to 3 steps max. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 4. Fallback Chains
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ Instead of: │
│ ┌─────────────┐ │
│ │ Tool A │──── FAIL ────► Pipeline stops │
│ └─────────────┘ │
│ │
│ Use: │
│ ┌─────────────┐ │
│ │ Tool A │──── FAIL ────┐ │
│ └─────────────┘ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ Tool A' │──── FAIL ────┐ │
│ │ (fallback) │ │ │
│ └─────────────┘ ▼ │
│ ┌─────────────┐ │
│ │ Tool A'' │ │
│ │ (last resort)│ │
│ └─────────────┘ │
│ │
│ P(at least one works) = 1 - P(all fail) │
│ = 1 - (0.05)³ │
│ = 0.999875 │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 5. Graceful Degradation
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ Full plan (ideal): │
│ A → B → C → D → E → F → G │
│ Output: Comprehensive report with charts, analysis, and recommendations │
│ │
│ Degraded plan (if D fails): │
│ A → B → C → [skip D] → E' → F' → G' │
│ Output: Report with analysis and recommendations (no charts) │
│ │
│ Minimal plan (if B and D fail): │
│ A → [skip B] → C' → [skip D] → E'' → G'' │
│ Output: Basic summary with key findings │
│ │
│ ═══════════════════════════════════════════════════════════════════════ │
│ Something is better than nothing. │
│ Define acceptable degradation levels upfront. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 6. Learned Pathway Weighting (K-Factor)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ Track which paths users actually take: │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Path A → B → C → D: Used 847 times (84.7%) ◄── K = 0.847 │ │
│ │ Path A → B → X → D: Used 102 times (10.2%) │ │
│ │ Path A → Y → C → D: Used 38 times ( 3.8%) │ │
│ │ Path A → B → C → Z: Used 13 times ( 1.3%) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ When generating new plans, weight toward learned paths: │
│ │
│ P(suggest path) = base_probability × (1 + K × learning_weight) │
│ │
│ As K → 1.0 for a path, it becomes effectively deterministic. │
│ But the escape hatch remains for the 0.1% edge cases. │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ K = 0.50: Suggest learned path, but explore alternatives │ │
│ │ K = 0.85: Strongly prefer learned path │ │
│ │ K = 0.99: Almost deterministic, rare deviation │ │
│ │ K = 1.00: Locked in (manual override to change) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
---
## The Fragility Budget
Every plan has a "fragility budget"—the maximum acceptable failure probability.
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ FRAGILITY BUDGET ALLOCATION │
│ │
│ Acceptable failure rate: 10% (P(success) ≥ 90%) │
│ │
│ Budget equation: │
│ P(success) = P₁ × P₂ × P₃ × ... × Pₙ ≥ 0.90 │
│ │
│ If each step has P = 0.98: │
│ 0.98ⁿ ≥ 0.90 │
│ n ≤ 5.2 │
│ │
│ Maximum steps: 5 │
│ │
│ ───────────────────────────────────────────────────────────────────────── │
│ │
│ If each step has P = 0.95: │
│ 0.95ⁿ ≥ 0.90 │
│ n ≤ 2.0 │
│ │
│ Maximum steps: 2 (!) │
│ │
│ ───────────────────────────────────────────────────────────────────────── │
│ │
│ To allow more steps, you must: │
│ • Increase individual step reliability │
│ • Add fallbacks (changes the math) │
│ • Accept higher failure rate │
│ • Use parallel branches │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
---
## When More Steps ARE Justified
Not all conjunction is bad. More steps are justified when:
### 1. Each step genuinely adds value
```
Good: A (fetch) → B (parse) → C (analyze) → D (format)
Each step transforms data in a necessary way.
Bad: A (fetch) → B (validate fetch) → C (log fetch) → D (cache fetch) → E (parse)
Steps B, C, D are defensive overhead that could be internal to A.
```
### 2. Steps have independent failure recovery
```
A → [checkpoint] → B → [checkpoint] → C → [checkpoint] → D
Each checkpoint isolates failure.
Effective conjunction = max(steps between checkpoints), not total steps.
```
### 3. The domain genuinely requires it
```
Medical diagnosis:
symptoms → differential → tests → results → diagnosis → treatment
You can't skip steps. The conjunction is inherent to the domain.
Accept the fragility, but add maximum safeguards.
```
### 4. Parallel execution changes the math
```
┌── B ──┐
│ │
A ───┼── C ──┼─── E
│ │
└── D ──┘
P(success) = P(A) × P(at least one of B,C,D) × P(E)
= P(A) × (1 - P(B fails) × P(C fails) × P(D fails)) × P(E)
= 0.95 × (1 - 0.05³) × 0.95
= 0.95 × 0.999875 × 0.95
= 0.902
Much better than sequential B → C → D!
```
---
## Pathway Learning Algorithms
Beyond simple "most used" frequency counting, here are algorithms for learning and suggesting pathways:
### 1. Multi-Armed Bandit (Explore vs Exploit)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ EPSILON-GREEDY STRATEGY │
│ ─────────────────────── │
│ │
│ ε = exploration rate (e.g., 0.1 = 10% exploration) │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Roll random(0,1) │ │
│ │ │ │ │
│ │ ├───► < ε ───► EXPLORE: Pick random path │ │
│ │ │ (discover potentially better routes) │ │
│ │ │ │ │
│ │ └───► ≥ ε ───► EXPLOIT: Pick best known path │ │
│ │ (use what's worked before) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Over time, ε decays: │
│ │
│ Week 1: ε = 0.30 ███████████░░░░░░░░░░░░░░░░░░░ 30% exploration │
│ Week 4: ε = 0.15 █████░░░░░░░░░░░░░░░░░░░░░░░░░ 15% exploration │
│ Week 12: ε = 0.05 ██░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 5% exploration │
│ Week 52: ε = 0.01 ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 1% exploration │
│ │
│ System converges to best paths while always leaving door open. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 2. Upper Confidence Bound (UCB)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ UCB ALGORITHM: Optimism in the face of uncertainty │
│ ────────────── │
│ │
│ Score(path) = average_success + C × √(ln(total_runs) / path_runs) │
│ ─────────────── ───────────────────────────────── │
│ exploitation exploration bonus │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Path A: 100 runs, 92% success │ │
│ │ Score = 0.92 + 0.5 × √(ln(500)/100) = 0.92 + 0.12 = 1.04 │ │
│ │ │ │
│ │ Path B: 5 runs, 80% success │ │
│ │ Score = 0.80 + 0.5 × √(ln(500)/5) = 0.80 + 0.78 = 1.58 │ │
│ │ ▲ │ │
│ │ │ │ │
│ │ Path B wins! It has high uncertainty, deserves exploration. │ │
│ │ │ │
│ │ After 50 more runs of Path B (now 70% success): │ │
│ │ Score = 0.70 + 0.5 × √(ln(550)/55) = 0.70 + 0.24 = 0.94 │ │
│ │ │ │
│ │ Now Path A wins. Exploration revealed B is actually worse. │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Automatically balances exploration of uncertain paths vs exploitation. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 3. Thompson Sampling (Bayesian)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ THOMPSON SAMPLING: Sample from belief distributions │
│ ───────────────── │
│ │
│ Each path has a Beta distribution: Beta(successes + 1, failures + 1) │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Path A: 90 successes, 10 failures │ │
│ │ Beta(91, 11) │ │
│ │ │ │
│ │ Probability density: │ │
│ │ ▄▄▄▄ │ │
│ │ ▄██████▄ │ │
│ │ ▄██████████▄ │ │
│ │ ──────▄██████████████▄────── │ │
│ │ 0.7 0.8 0.9 1.0 │ │
│ │ ▲ │ │
│ │ tight peak (confident) │ │
│ │ │ │
│ │ Path B: 4 successes, 1 failure │ │
│ │ Beta(5, 2) │ │
│ │ │ │
│ │ Probability density: │ │
│ │ ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ │ │
│ │ ▄████████████████████▄ │ │
│ │ ▄████████████████████████▄ │ │
│ │ ────────────────────────── │ │
│ │ 0.2 0.4 0.6 0.8 1.0 │ │
│ │ ▲ │ │
│ │ wide spread (uncertain) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Selection: Sample one value from each distribution, pick highest. │
│ │
│ Path A sample: 0.88 │
│ Path B sample: 0.73 ← Sometimes samples high due to uncertainty! │
│ │
│ Naturally explores uncertain options proportional to their potential. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 4. Contextual Bandits (User/Query Aware)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ CONTEXTUAL BANDITS: Path selection depends on context │
│ ────────────────── │
│ │
│ Context features: │
│ • User type (developer, marketer, analyst) │
│ • Query complexity (simple, medium, complex) │
│ • Time of day (morning, afternoon, evening) │
│ • Previous success rate with this user │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Model learns: P(success | path, context) │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────┐ │ │
│ │ │ Context: {user: "developer", complexity: "high"} │ │ │
│ │ │ │ │ │
│ │ │ Path A (thorough): P(success) = 0.89 ◄── BEST FOR THIS │ │ │
│ │ │ Path B (quick): P(success) = 0.62 CONTEXT │ │ │
│ │ │ Path C (balanced): P(success) = 0.78 │ │ │
│ │ └─────────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────────────────┐ │ │
│ │ │ Context: {user: "marketer", complexity: "low"} │ │ │
│ │ │ │ │ │
│ │ │ Path A (thorough): P(success) = 0.71 │ │ │
│ │ │ Path B (quick): P(success) = 0.94 ◄── BEST FOR THIS │ │ │
│ │ │ Path C (balanced): P(success) = 0.85 CONTEXT │ │ │
│ │ └─────────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Different users/contexts get different "best" paths automatically. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 5. Collaborative Filtering (Similar Users)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ COLLABORATIVE FILTERING: "Users like you also used..." │
│ ─────────────────────── │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ User-Path Success Matrix: │ │
│ │ │ │
│ │ Path A Path B Path C Path D Path E │ │
│ │ User 1: ✓ ✗ ✓ ✓ ? │ │
│ │ User 2: ✓ ✓ ✗ ✓ ✓ │ │
│ │ User 3: ✗ ✓ ✓ ✗ ✓ │ │
│ │ User 4: ✓ ✗ ✓ ✓ ? ◄── Current │ │
│ │ │ │
│ │ User 4 is most similar to User 1 (matching pattern). │ │
│ │ User 1 succeeded with Path E? → Recommend Path E to User 4. │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Matrix factorization finds latent features: │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ User 4 latent vector: [0.8, 0.2, 0.9, 0.1] │ │
│ │ Path E latent vector: [0.7, 0.3, 0.8, 0.2] │ │
│ │ │ │
│ │ Predicted score = dot_product = 0.56 + 0.06 + 0.72 + 0.02 = 1.36 │ │
│ │ │ │
│ │ High score → Recommend! │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 6. Q-Learning (Reinforcement Learning)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ Q-LEARNING: Learn value of state-action pairs │
│ ────────── │
│ │
│ Q(state, action) = expected future reward │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ State = (current_step, accumulated_context, user_feedback) │ │
│ │ Action = which tool to call next │ │
│ │ Reward = +1 (task success), -0.1 (per step), -1 (failure) │ │
│ │ │ │
│ │ ┌────────────────────────────────────────────────────────┐ │ │
│ │ │ │ │ │
│ │ │ State: "just fetched webpage" │ │ │
│ │ │ │ │ │
│ │ │ Q-values: │ │ │
│ │ │ → parse-html: Q = 0.82 ◄── Highest, select │ │ │
│ │ │ → extract-text: Q = 0.71 │ │ │
│ │ │ → screenshot: Q = 0.45 │ │ │
│ │ │ → validate: Q = 0.23 │ │ │
│ │ │ │ │ │
│ │ └────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ Update rule (after each execution): │ │
│ │ │ │
│ │ Q(s,a) ← Q(s,a) + α × [reward + γ×max(Q(s',a')) - Q(s,a)] │ │
│ │ ▲ ▲ │ │
│ │ learning rate discount future rewards │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Learns optimal policy through trial and error over time. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 7. Monte Carlo Tree Search (MCTS)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ MCTS: Simulate many possible futures, pick best path │
│ ──── │
│ │
│ Four phases: SELECT → EXPAND → SIMULATE → BACKPROPAGATE │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ ┌─────┐ │ │
│ │ │START│ visits: 1000 │ │
│ │ └──┬──┘ │ │
│ │ ┌───────┼───────┐ │ │
│ │ ▼ ▼ ▼ │ │
│ │ ┌───┐ ┌───┐ ┌───┐ │ │
│ │ │ A │ │ B │ │ C │ SELECT: Follow UCB down tree │ │
│ │ │420│ │380│ │200│ (balance visits & wins) │ │
│ │ └─┬─┘ └─┬─┘ └───┘ │ │
│ │ │ │ │ │
│ │ ▼ ▼ │ │
│ │ ┌───┐ ┌───┐ ┌───┐ │ │
│ │ │A1 │ │B1 │ │B2 │ EXPAND: Add new node │ │
│ │ │350│ │200│ │180│ │ │
│ │ └───┘ └─┬─┘ └───┘ │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ ┌───┐ │ │
│ │ │???│ SIMULATE: Random rollout to terminal │ │
│ │ │NEW│ → Success! │ │
│ │ └───┘ │ │
│ │ │ │
│ │ BACKPROPAGATE: Update all ancestors with result │ │
│ │ B1: 200 → 201 wins │ │
│ │ B: 380 → 381 wins │ │
│ │ START: 1000 → 1001 visits │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ After N simulations, pick path with most visits (most confident). │
│ Great for planning multiple steps ahead. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 8. Genetic Algorithms (Evolve Pathways)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ GENETIC ALGORITHM: Evolve better pathways over generations │
│ ───────────────── │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Generation 1: Random pathways │ │
│ │ ───────────────────────────────── │ │
│ │ Path 1: [A, B, C, D, E] fitness: 0.65 │ │
│ │ Path 2: [A, C, B, E, D] fitness: 0.72 │ │
│ │ Path 3: [B, A, D, C, E] fitness: 0.58 │ │
│ │ Path 4: [A, B, D, C, E] fitness: 0.81 ◄── Best │ │
│ │ │ │
│ │ ───────────────────────────────────────────────────────────────── │ │
│ │ │ │
│ │ SELECTION: Keep top 50% │ │
│ │ Path 4: [A, B, D, C, E] ✓ │ │
│ │ Path 2: [A, C, B, E, D] ✓ │ │
│ │ │ │
│ │ CROSSOVER: Combine successful paths │ │
│ │ Parent 1: [A, B, | D, C, E] │ │
│ │ Parent 2: [A, C, | B, E, D] │ │
│ │ ↓ │ │
│ │ Child: [A, B, | B, E, D] (take prefix from P1, suffix from P2)│ │
│ │ │ │
│ │ MUTATION: Random tweaks (5% chance per gene) │ │
│ │ [A, B, B, E, D] → [A, B, F, E, D] (B mutated to F) │ │
│ │ │ │
│ │ ───────────────────────────────────────────────────────────────── │ │
│ │ │ │
│ │ Generation 10: │ │
│ │ Best path: [A, B, D, E] fitness: 0.94 │ │
│ │ (Evolved to drop unnecessary step C!) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Discovers optimal pathways through evolution, not explicit programming. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 9. Inverse Reinforcement Learning (Learn from Experts)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ INVERSE RL: Infer reward function from expert demonstrations │
│ ────────── │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Observe expert (power user) behavior: │ │
│ │ │ │
│ │ Expert Session 1: A → B → D → E (skipped C) │ │
│ │ Expert Session 2: A → B → D → E (skipped C) │ │
│ │ Expert Session 3: A → B → C → D → E (included C for edge case) │ │
│ │ Expert Session 4: A → B → D → E (skipped C) │ │
│ │ │ │
│ │ Inferred reward function: │ │
│ │ • High reward for: A → B, B → D, D → E │ │
│ │ • Low/negative reward for: B → C (usually skipped) │ │
│ │ • Context-dependent: C only when specific conditions met │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Now apply learned reward to new users: │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ New user at step B: │ │
│ │ │ │
│ │ Options: │ │
│ │ B → C: reward = -0.2 (experts usually skip) │ │
│ │ B → D: reward = +0.8 (experts prefer) ◄── SUGGEST │ │
│ │ B → E: reward = +0.1 (sometimes works) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ System learns "what experts value" rather than explicit rules. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 10. Bayesian Optimization (Efficient Exploration)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ BAYESIAN OPTIMIZATION: Smart exploration with Gaussian Processes │
│ ──────────────────── │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Model uncertainty about unexplored paths: │ │
│ │ │ │
│ │ Success │ │
│ │ Rate │ │
│ │ │ │ │
│ │ 1.0┤ ╭───╮ │ │
│ │ │ ╭─┤ ├─╮ ← Uncertainty band │ │
│ │ 0.8┤ ● ╭─┤ │ ├╮ │ │
│ │ │ ╲ ╭─┤ │ │ │ ╲ │ │
│ │ 0.6┤ │ │ │ │ ╲ ● = observed data │ │
│ │ │ ╲╱ ╰─┤ │ ├───╲ │ │
│ │ 0.4┤ ● ● ╰───┴───╯ ╲ │ │
│ │ │ ╲● │ │
│ │ 0.2┤ │ │
│ │ │ ● │ │
│ │ 0.0┼──────────────────────────────────────────────── │ │
│ │ Path Path Path Path Path Path Path Path │ │
│ │ A B C D E F G H │ │
│ │ │ │
│ │ Acquisition function picks next path to try: │ │
│ │ • Expected Improvement (EI) │ │
│ │ • Upper Confidence Bound (UCB) │ │
│ │ • Probability of Improvement (PI) │ │
│ │ │ │
│ │ → Try Path E: High uncertainty + decent predicted mean │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Minimizes trials needed to find optimal path. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 11. Hierarchical Clustering (Path Families)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ HIERARCHICAL CLUSTERING: Group similar paths into families │
│ ─────────────────────── │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ ┌───────────┴───────────┐ │ │
│ │ │ │ │ │
│ │ ┌───────┴───────┐ ┌───────┴───────┐ │ │
│ │ │ │ │ │ │ │
│ │ ┌─────┴─────┐ ┌─────┴─────┐ │ ┌─────┴─────┐ │ │
│ │ │ │ │ │ │ │ │ │ │
│ │ [A→B→D→E] [A→B→D→F] [A→B→C→E] [A→C→D→E] [B→A→D→E] [B→C→D→E] │ │
│ │ │ │
│ │ ════════════════════════════════════════════════════════════════ │ │
│ │ "Direct paths" "C-inclusive" "B-first variants" │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Benefits: │
│ • When "Direct paths" cluster works, prefer it as a family │
│ • If it fails, try "C-inclusive" family │
│ • Don't randomly jump between distant clusters │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Selection strategy: │ │
│ │ │ │
│ │ 1. Pick best cluster (based on cluster-level success rate) │ │
│ │ 2. Pick best path within cluster (based on path-level success) │ │
│ │ 3. If cluster fails repeatedly, move to sibling cluster │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### 12. Recency-Weighted Success (Time Decay)
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ RECENCY WEIGHTING: Recent outcomes matter more than old ones │
│ ──────────────── │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Path A history: │ │
│ │ │ │
│ │ Time Outcome Raw Weight Decayed Weight (λ=0.95) │ │
│ │ ───── ─────── ────────── ────────────────────── │ │
│ │ t-30 Success 1.0 0.95^30 = 0.21 │ │
│ │ t-20 Success 1.0 0.95^20 = 0.36 │ │
│ │ t-10 Failure 1.0 0.95^10 = 0.60 │ │
│ │ t-5 Success 1.0 0.95^5 = 0.77 │ │
│ │ t-2 Success 1.0 0.95^2 = 0.90 │ │
│ │ t-1 Failure 1.0 0.95^1 = 0.95 │ │
│ │ t-0 Success 1.0 0.95^0 = 1.00 │ │
│ │ │ │
│ │ Naive success rate: 5/7 = 71% │ │
│ │ Recency-weighted: (0.21+0.36+0.77+0.90+1.00) / total = 68% │ │
│ │ (recent failure pulls it down) │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ Why it matters: │
│ • Tools get updated (newer versions may be better/worse) │
│ • User preferences drift │
│ • External APIs change behavior │
│ • Old successes may not predict current performance │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
### Algorithm Comparison Matrix
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ ALGORITHM SELECTION GUIDE │
│ │
│ ┌──────────────────┬───────────┬───────────┬───────────┬─────────────┐ │
│ │ Algorithm │ Best For │ Data Req │ Compute │ Convergence │ │
│ ├──────────────────┼───────────┼───────────┼───────────┼─────────────┤ │
│ │ ε-Greedy │ Simple │ Low │ O(1) │ Slow │ │
│ │ UCB │ Balanced │ Low │ O(1) │ Medium │ │
│ │ Thompson │ Uncertain │ Low │ O(1) │ Fast │ │
│ │ Contextual │ Personlzd │ Medium │ O(n) │ Medium │ │
│ │ Collaborative │ Multi-usr │ High │ O(n²) │ Medium │ │
│ │ Q-Learning │ Sequences │ High │ O(s×a) │ Slow │ │
│ │ MCTS │ Planning │ Low │ O(sims) │ Fast │ │
│ │ Genetic │ Discovery │ Medium │ O(pop×gen)│ Variable │ │
│ │ Inverse RL │ Experts │ Medium │ O(demos) │ Fast │ │
│ │ Bayesian Opt │ Expensive │ Low │ O(n³) │ Very Fast │ │
│ │ Clustering │ Families │ Medium │ O(n²) │ N/A │ │
│ │ Recency │ Drift │ Low │ O(1) │ Adaptive │ │
│ └──────────────────┴───────────┴───────────┴───────────┴─────────────┘ │
│ │
│ Hybrid approaches often work best: │
│ • UCB + Recency for drifting environments │
│ • Contextual + Collaborative for personalization │
│ • MCTS + Q-Learning for complex sequential decisions │
│ • Clustering + Thompson for structured exploration │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
---
## Summary: The Fragility Principles
```
┌─────────────────────────────────────────────────────────────────────────────┐
│ │
│ 1. CONJUNCTION REDUCES PROBABILITY │
│ Every additional step multiplies failure risk. │
│ P(A ∧ B) ≤ P(A) │
│ │
│ 2. OCCAM'S RAZOR APPLIES │
│ Prefer fewer assumptions. Fewer steps = fewer assumptions. │
│ Don't add steps "just in case." │
│ │
│ 3. DETERMINISM EMERGES FROM LEARNING │
│ Don't design deterministic upfront. │
│ Let frequently-used paths become deterministic through K-factor. │
│ │
│ 4. BUDGET YOUR FRAGILITY │
│ Know your acceptable failure rate. │
│ Calculate maximum steps accordingly. │
│ │
│ 5. PARALLEL > SEQUENTIAL │
│ When possible, run steps in parallel. │
│ Changes multiplication to "at least one succeeds" math. │
│ │
│ 6. CHECKPOINTS BOUND FAILURE │
│ Divide long chains into recoverable segments. │
│ Effective fragility = longest segment, not total length. │
│ │
│ 7. FALLBACKS COMPOUND SUCCESS │
│ P(at least one works) = 1 - P(all fail) │
│ Three 95% tools as fallbacks = 99.99% effective reliability. │
│ │
│ 8. GRACEFUL DEGRADATION > TOTAL FAILURE │
│ Define "good enough" outputs at each degradation level. │
│ Something is better than nothing. │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
```
---
## Visualizing the Tradeoff
```
CAPABILITY
│ ╭────────────────────╮
OPTIMAL ZONE ╲
(enough steps for ╲
capability, not so ╲
many that it breaks) ╲
┌─────────────────────────────────┐
│ │
│╱ │ FRAGILITY CLIFF │
│ │ (too many steps, │
│ │ system breaks more │
│ │ than it works) │
│ │ │
│ └─────────────────────────────────┘
└──────────────────────────────────────────► STEPS
1 3 5 8 12 20 50
Robust ◄───────────────────► Fragile
```
The goal: **Stay in the optimal zone**—enough capability to solve the problem, not so many steps that the system becomes unreliable.