Back to Ruflo

ReasoningBank Advanced Tutorial: Self-Learning & Optimization

v2/docs/reasoningbank/tutorial-advanced.md

3.6.3023.2 KB
Original Source

ReasoningBank Advanced Tutorial: Self-Learning & Optimization

Overview

This advanced tutorial covers SAFLA (Self-Aware Feedback Loop Algorithm), pattern linking, cognitive diversity patterns, and optimization strategies for building intelligent, self-improving AI systems.

Prerequisites: Complete Basic Tutorial first Time Required: 60 minutes Level: Advanced

Table of Contents

  1. SAFLA: Self-Aware Feedback Loop Algorithm
  2. Pattern Linking & Causal Reasoning
  3. Cognitive Diversity Patterns
  4. Task Trajectory Tracking
  5. Bayesian Confidence Learning
  6. Advanced Query Strategies
  7. Building Self-Learning Agents
  8. Performance Optimization

SAFLA: Self-Aware Feedback Loop Algorithm

SAFLA is the core intelligence system that makes ReasoningBank self-aware and continuously improving.

How SAFLA Works

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  SAFLA Cycle                         β”‚
β”‚                                                      β”‚
β”‚  1. OBSERVE                                          β”‚
β”‚     └─ Track pattern usage and outcomes             β”‚
β”‚                                                      β”‚
β”‚  2. ANALYZE                                          β”‚
β”‚     └─ Evaluate success/failure of patterns         β”‚
β”‚                                                      β”‚
β”‚  3. LEARN                                            β”‚
β”‚     └─ Update confidence scores (Bayesian)          β”‚
β”‚                                                      β”‚
β”‚  4. ADAPT                                            β”‚
β”‚     └─ Adjust rankings and recommendations          β”‚
β”‚                                                      β”‚
β”‚  5. FEEDBACK                                         β”‚
β”‚     └─ Return to step 1 (continuous loop)           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

SAFLA Key Capabilities

1. Semantic Understanding πŸ”

Example: Finding related patterns without exact keywords

bash
# Store security patterns
npx claude-flow@alpha memory store sql_injection \
  "Use parameterized queries to prevent SQL injection attacks" \
  --namespace security --reasoningbank

npx claude-flow@alpha memory store xss_prevention \
  "Sanitize user input and escape HTML to prevent XSS" \
  --namespace security --reasoningbank

npx claude-flow@alpha memory store csrf_protection \
  "Use CSRF tokens for state-changing operations" \
  --namespace security --reasoningbank

# Query with broad term
npx claude-flow@alpha memory query "prevent attacks" \
  --namespace security --reasoningbank

Output:

βœ… Found 3 results (semantic search)

1. sql_injection
   Match Score: 82%
   Confidence: 50%
   β†’ System understands "prevent attacks" relates to SQL injection

2. xss_prevention
   Match Score: 79%
   Confidence: 50%
   β†’ Also finds XSS without exact keyword match

3. csrf_protection
   Match Score: 71%
   Confidence: 50%
   β†’ Finds CSRF through semantic similarity

2. Usage Tracking πŸ“Š

SAFLA tracks every pattern access to identify valuable knowledge:

bash
# Check pattern statistics
npx claude-flow@alpha memory status --detailed --reasoningbank

Output:

Usage Statistics (Last 30 Days):

Most Accessed Patterns:
1. jwt_authentication (42 uses) ⭐
2. error_handling (31 uses)
3. database_pooling (28 uses)
4. cache_strategy (15 uses)
5. rate_limiting (8 uses)

Least Accessed:
- deprecated_api_v1 (0 uses) ⚠️
- old_auth_method (1 use) ⚠️

πŸ’‘ High usage patterns get priority in search results

3. Confidence Scoring πŸ’―

Every pattern has a confidence score that evolves based on usage:

bash
# Track confidence evolution
npx claude-flow@alpha memory info jwt_authentication --reasoningbank

Output:

Pattern: jwt_authentication
Content: JWT with refresh token rotation and 15-min expiration

Confidence History:
β”œβ”€ Initial: 50% (2025-10-01)
β”œβ”€ After 5 uses: 58% (2025-10-05)
β”œβ”€ After 10 uses: 65% (2025-10-08)
β”œβ”€ After 20 uses: 73% (2025-10-12)
└─ Current: 78% (2025-10-14) βœ…

Usage: 42 times
Last Success: 2025-10-14
Last Failure: None

Trend: ↗️ Increasing (High reliability)

4. Cross-Domain Learning 🌐

SAFLA builds a knowledge graph connecting related concepts:

bash
# Store related patterns across domains
npx claude-flow@alpha memory store jwt_auth \
  "JWT tokens with HMAC SHA256 signing" \
  --namespace backend --reasoningbank

npx claude-flow@alpha memory store token_storage \
  "Store tokens in httpOnly cookies, not localStorage" \
  --namespace frontend --reasoningbank

npx claude-flow@alpha memory store token_refresh \
  "Refresh tokens before expiration using interceptors" \
  --namespace frontend --reasoningbank

# Query finds related patterns across namespaces!
npx claude-flow@alpha memory query "token management" --reasoningbank

Output:

βœ… Found 3 results across 2 namespaces

Backend:
1. jwt_auth (88% match)

Frontend:
2. token_storage (82% match)
3. token_refresh (79% match)

πŸ’‘ SAFLA discovered that backend JWT and frontend storage are related!

5. Match Scoring 🎯

SAFLA uses MMR (Maximal Marginal Relevance) scoring:

javascript
Score = 0.4 * Semantic_Similarity +
        0.3 * Reliability_Confidence +
        0.2 * Recency +
        0.1 * Diversity

Example:

bash
npx claude-flow@alpha memory query "authentication" \
  --namespace backend --reasoningbank --verbose

Output:

βœ… Found 2 results (with scoring breakdown)

1. jwt_auth_v2
   Total Score: 87%
   β”œβ”€ Semantic: 35% (0.4 * 87.5% similarity)
   β”œβ”€ Reliability: 23% (0.3 * 78% confidence)
   β”œβ”€ Recency: 18% (0.2 * 92% - used 2 days ago)
   └─ Diversity: 11% (0.1 * 100% - unique result)

2. basic_auth_deprecated
   Total Score: 42%
   β”œβ”€ Semantic: 30% (0.4 * 75% similarity)
   β”œβ”€ Reliability: 9% (0.3 * 31% confidence) ⚠️ Low!
   β”œβ”€ Recency: 2% (0.2 * 12% - used 180 days ago) ⚠️ Old!
   └─ Diversity: 1% (0.1 * 5% - similar to result 1)

Pattern Linking & Causal Reasoning

Pattern linking enables causal reasoning - understanding how patterns relate to each other.

TypeMeaningExample
causesA leads to BPoor error handling β†’ Production crashes
requiresA needs B firstJWT auth β†’ Secret key management
conflictsA incompatible with BStateless auth ↔ Server sessions
enhancesA improves BCaching β†’ Database performance
alternativeA substitutes BOAuth2 vs Basic auth
bash
# Method 1: Explicit linking (future feature)
npx claude-flow@alpha memory link \
  jwt_authentication requires secret_key_rotation \
  --strength 0.9 --reasoningbank

# Method 2: Automatic discovery (current)
# SAFLA automatically discovers links based on:
# - Co-occurrence in queries
# - Semantic similarity
# - Usage patterns
bash
# Store patterns
npx claude-flow@alpha memory store postgres_db \
  "PostgreSQL with connection pooling" \
  --namespace database --reasoningbank

npx claude-flow@alpha memory store db_indexing \
  "B-tree indexes on foreign keys and WHERE clause columns" \
  --namespace database --reasoningbank

npx claude-flow@alpha memory store query_optimization \
  "Use EXPLAIN ANALYZE to identify slow queries" \
  --namespace database --reasoningbank

# Query multiple times - SAFLA learns relationships
npx claude-flow@alpha memory query "database performance" --reasoningbank
npx claude-flow@alpha memory query "slow queries" --reasoningbank
npx claude-flow@alpha memory query "PostgreSQL optimization" --reasoningbank

# After usage, SAFLA discovers links:
# db_indexing β†’ enhances β†’ postgres_db
# query_optimization β†’ requires β†’ db_indexing
bash
# Find all patterns that REQUIRE a specific pattern
npx claude-flow@alpha memory links jwt_authentication --type requires

Expected Output:

Patterns that REQUIRE jwt_authentication:

1. token_refresh (strength: 0.95)
   β†’ Can't refresh tokens without JWT system

2. api_authorization (strength: 0.88)
   β†’ API auth depends on JWT validation

3. user_session_management (strength: 0.75)
   β†’ Sessions need JWT for state

Cognitive Diversity Patterns

ReasoningBank supports 6 cognitive reasoning strategies inspired by cognitive science research.

The Six Patterns

PatternDescriptionUse Case
ConvergentFocus on single best solutionOptimization, debugging
DivergentExplore multiple possibilitiesBrainstorming, architecture
LateralCreative indirect approachesInnovation, problem-solving
SystemsHolistic interconnected thinkingComplex systems, architecture
CriticalEvaluate and challenge assumptionsCode review, security
AdaptiveLearn and evolve strategiesSelf-improving agents

Using Cognitive Patterns

bash
# Store pattern with cognitive type
npx claude-flow@alpha memory store debug_approach \
  "Use binary search to isolate bugs: test middle, eliminate half" \
  --namespace debugging \
  --cognitive-pattern convergent \
  --reasoningbank

npx claude-flow@alpha memory store architecture_brainstorm \
  "Consider microservices, monolith, serverless, hybrid approaches" \
  --namespace architecture \
  --cognitive-pattern divergent \
  --reasoningbank

npx claude-flow@alpha memory store security_audit \
  "Question all assumptions: Is this input validated? Can this be spoofed?" \
  --namespace security \
  --cognitive-pattern critical \
  --reasoningbank

Query by Cognitive Pattern

bash
# Find convergent (focused) approaches
npx claude-flow@alpha memory query "optimization" \
  --cognitive-pattern convergent --reasoningbank

Output:

βœ… Found convergent thinking patterns:

1. debug_approach
   Binary search to isolate bugs
   Pattern: Convergent (narrowing down)

2. performance_profiling
   Profile first, optimize bottleneck, measure again
   Pattern: Convergent (focused optimization)

Cognitive Pattern in Action

Scenario: Building a new authentication system

bash
# Phase 1: Divergent (explore options)
npx claude-flow@alpha memory query "authentication approaches" \
  --cognitive-pattern divergent --reasoningbank
# Returns: JWT, OAuth2, Session-based, API keys, certificates

# Phase 2: Critical (evaluate each)
npx claude-flow@alpha memory query "JWT security concerns" \
  --cognitive-pattern critical --reasoningbank
# Returns: Token theft, XSS risks, CSRF considerations

# Phase 3: Convergent (pick best solution)
npx claude-flow@alpha memory query "production JWT implementation" \
  --cognitive-pattern convergent --reasoningbank
# Returns: JWT with refresh rotation (best practice)

# Phase 4: Systems (understand full picture)
npx claude-flow@alpha memory query "JWT integration points" \
  --cognitive-pattern systems --reasoningbank
# Returns: Frontend storage, backend validation, refresh flow, logout

# Phase 5: Adaptive (learn from implementation)
# After implementation, confidence scores update based on success/failure

Task Trajectory Tracking

Track sequential reasoning steps to learn complete workflows.

Recording Trajectories

bash
# Start a task trajectory
npx claude-flow@alpha memory trajectory start build_api \
  "Building RESTful API with authentication" \
  --reasoningbank

# Record each step
npx claude-flow@alpha memory trajectory step build_api \
  "Designed database schema with users, tokens tables" \
  --reasoningbank

npx claude-flow@alpha memory trajectory step build_api \
  "Implemented JWT token generation with HMAC SHA256" \
  --reasoningbank

npx claude-flow@alpha memory trajectory step build_api \
  "Added refresh token rotation with secure cookies" \
  --reasoningbank

npx claude-flow@alpha memory trajectory step build_api \
  "Tested with Postman: 100% pass rate" \
  --reasoningbank

# Complete trajectory
npx claude-flow@alpha memory trajectory end build_api \
  --outcome success --reasoningbank

Retrieving Trajectories

bash
# Get full trajectory
npx claude-flow@alpha memory trajectory get build_api --reasoningbank

Output:

Trajectory: build_api
Task: Building RESTful API with authentication
Outcome: βœ… Success
Steps: 4

Step 1: Designed database schema with users, tokens tables
  Duration: 15 minutes

Step 2: Implemented JWT token generation with HMAC SHA256
  Duration: 30 minutes

Step 3: Added refresh token rotation with secure cookies
  Duration: 20 minutes

Step 4: Tested with Postman: 100% pass rate
  Duration: 10 minutes

Total Time: 75 minutes
Confidence: 75% (successful trajectory)

Learning from Trajectories

SAFLA uses trajectories to:

  1. Identify successful sequences - "These steps led to success"
  2. Spot failure patterns - "Step 3 always causes issues"
  3. Recommend workflows - "Users who did A, B, C succeeded"
  4. Estimate timelines - "This task typically takes 60-90 minutes"

Example:

bash
# Query for similar task
npx claude-flow@alpha memory query "build authentication API" \
  --include-trajectories --reasoningbank

Output:

βœ… Found 1 matching trajectory: build_api

Recommended Steps (based on successful past execution):
1. Design database schema (est. 15 min)
2. Implement JWT generation (est. 30 min)
3. Add refresh rotation (est. 20 min)
4. Test thoroughly (est. 10 min)

Total Estimated Time: 75 minutes
Success Rate: 100% (1/1 trajectories succeeded)
Confidence: 75%

Bayesian Confidence Learning

SAFLA uses Bayesian updating to learn from outcomes.

The Formula

P(pattern works | evidence) = P(evidence | pattern works) * P(pattern works)
                               ─────────────────────────────────────────────
                                            P(evidence)

Simplified:
  new_confidence = old_confidence + learning_rate * (outcome - old_confidence)

Learning Curve Example

bash
# Day 1: Store new pattern
npx claude-flow@alpha memory store async_pattern \
  "Use async/await with try-catch for error handling" \
  --namespace javascript --reasoningbank
# Confidence: 50% (prior belief: "probably works")

# Day 5: First use β†’ Success
# Confidence: 50% β†’ 60% (+20% of remaining 50%)

# Day 10: Second use β†’ Success
# Confidence: 60% β†’ 68% (+20% of remaining 40%)

# Day 15: Third use β†’ Failure!
# Confidence: 68% β†’ 58% (-15% of current 68%)

# Day 20: Fourth use β†’ Success
# Confidence: 58% β†’ 66% (+20% of remaining 42%)

# Day 30: Fifth use β†’ Success
# Confidence: 66% β†’ 73% (+20% of remaining 34%)

Manual Confidence Updates

bash
# Report success (future feature)
npx claude-flow@alpha memory feedback async_pattern \
  --outcome success --notes "Worked perfectly in production" \
  --reasoningbank

# Report failure (future feature)
npx claude-flow@alpha memory feedback async_pattern \
  --outcome failure --notes "Didn't handle promise rejection properly" \
  --reasoningbank

Confidence-Based Recommendations

bash
# Query with confidence threshold
npx claude-flow@alpha memory query "error handling" \
  --min-confidence 0.7 --reasoningbank

Output:

βœ… Found 3 results (confidence >= 70%)

1. try_catch_async (confidence: 87%) ⭐⭐⭐
   Proven approach, highly reliable

2. error_middleware (confidence: 79%) ⭐⭐
   Good approach, moderately tested

3. logging_errors (confidence: 72%) ⭐
   Acceptable approach, some validation

Excluded (low confidence):
- manual_error_check (31%) ⚠️ Unreliable
- custom_error_class (45%) ⚠️ Untested

Advanced Query Strategies

1. Multi-Factor Queries

bash
# Prioritize recent, reliable patterns
npx claude-flow@alpha memory query "database optimization" \
  --min-confidence 0.6 \
  --max-age 90 \
  --namespace backend \
  --reasoningbank

2. Similarity Threshold

bash
# Only return highly relevant results
npx claude-flow@alpha memory query "authentication" \
  --min-similarity 0.75 \
  --reasoningbank
bash
# Search all namespaces, group by domain
npx claude-flow@alpha memory query "performance" \
  --all-namespaces \
  --group-by namespace \
  --reasoningbank

Output:

βœ… Found 8 results across 3 namespaces

Backend (4 results):
1. db_connection_pooling (89% match)
2. query_optimization (82% match)
3. caching_strategy (78% match)
4. api_rate_limiting (71% match)

Frontend (3 results):
5. lazy_loading (85% match)
6. code_splitting (79% match)
7. image_optimization (74% match)

DevOps (1 result):
8. cdn_configuration (81% match)

4. Temporal Queries

bash
# Find patterns used recently
npx claude-flow@alpha memory query "API design" \
  --since "2025-10-01" \
  --reasoningbank

# Find patterns from specific time period
npx claude-flow@alpha memory query "security" \
  --between "2025-09-01" "2025-10-01" \
  --reasoningbank

Building Self-Learning Agents

Integrate ReasoningBank with AI agents for autonomous learning.

Agent Architecture

javascript
class SelfLearningAgent {
  constructor(namespace) {
    this.namespace = namespace;
    this.reasoningBank = new ReasoningBankClient();
  }

  async solveTask(task) {
    // 1. Query ReasoningBank for relevant patterns
    const patterns = await this.reasoningBank.query(task, {
      namespace: this.namespace,
      minConfidence: 0.5
    });

    // 2. Apply highest-confidence pattern
    const bestPattern = patterns[0];
    const result = await this.applyPattern(bestPattern, task);

    // 3. Record outcome
    await this.reasoningBank.recordOutcome(bestPattern.id, {
      success: result.success,
      duration: result.duration,
      notes: result.notes
    });

    // 4. Update confidence (SAFLA learning)
    // Happens automatically in ReasoningBank

    // 5. Store trajectory for complex tasks
    if (task.complex) {
      await this.reasoningBank.recordTrajectory({
        task: task.description,
        steps: result.steps,
        outcome: result.success ? 'success' : 'failure'
      });
    }

    return result;
  }

  async learnFromExperience() {
    // Analyze recent patterns
    const stats = await this.reasoningBank.getStatistics(this.namespace);

    // Identify low-confidence patterns
    const unreliable = stats.patterns.filter(p => p.confidence < 0.4);

    // Suggest improvements or alternatives
    for (const pattern of unreliable) {
      const alternatives = await this.reasoningBank.query(pattern.title, {
        excludeIds: [pattern.id],
        minConfidence: 0.6
      });

      if (alternatives.length > 0) {
        console.log(`Consider replacing ${pattern.title} with ${alternatives[0].title}`);
      }
    }
  }
}

// Usage
const agent = new SelfLearningAgent('backend');
await agent.solveTask({ description: 'Implement API authentication' });
await agent.learnFromExperience();

Example: Code Review Agent

bash
# Agent learns from code reviews over time

# Week 1: Store initial patterns
npx claude-flow@alpha memory store code_review_001 \
  "Check for SQL injection vulnerabilities in raw queries" \
  --namespace code_review --reasoningbank

# Week 2: Agent finds SQL injection β†’ Confidence increases
# Confidence: 50% β†’ 65%

# Week 5: Agent misses XSS vulnerability β†’ Need new pattern
npx claude-flow@alpha memory store code_review_002 \
  "Validate and escape user input to prevent XSS attacks" \
  --namespace code_review --reasoningbank

# Week 10: Agent now catches both SQL injection AND XSS
# Pattern 1 confidence: 65% β†’ 82%
# Pattern 2 confidence: 50% β†’ 74%

# Week 20: Query for code review patterns
npx claude-flow@alpha memory query "security vulnerabilities" \
  --namespace code_review --reasoningbank
# Returns both patterns, ranked by reliability!

Performance Optimization

1. Batch Operations

bash
# ❌ Slow: Sequential stores
for file in *.md; do
  npx claude-flow@alpha memory store "$file" "$(cat $file)" --reasoningbank
done

# βœ… Fast: Batch import (future feature)
npx claude-flow@alpha memory import patterns.json --reasoningbank

2. Namespace Partitioning

bash
# For large databases, partition by namespace
# Each namespace becomes separate table/index

npx claude-flow@alpha memory optimize --partition-namespaces --reasoningbank

3. Embedding Caching

bash
# Cache embeddings for frequently queried terms
npx claude-flow@alpha memory cache-embeddings \
  --queries "authentication,performance,security" \
  --reasoningbank

4. Approximate Nearest Neighbors

bash
# For > 10,000 patterns, use ANN (future feature)
npx claude-flow@alpha memory config \
  --search-algorithm ann \
  --ann-neighbors 100 \
  --reasoningbank

5. Query Result Caching

javascript
// Client-side caching
const cache = new Map();

async function queryWithCache(query, options) {
  const cacheKey = JSON.stringify({ query, options });

  if (cache.has(cacheKey)) {
    return cache.get(cacheKey);
  }

  const results = await reasoningBank.query(query, options);
  cache.set(cacheKey, results);

  return results;
}

Exercises

Exercise 1: Build a Learning System

Create a self-improving bug tracking system:

bash
# 1. Store 10 bug solutions
# 2. Query for similar bugs
# 3. Track which solutions work
# 4. Observe confidence scores improving

Exercise 2: Create a Knowledge Graph

Build a linked knowledge base:

bash
# 1. Store related patterns
# 2. Document relationships (requires, enhances, conflicts)
# 3. Query and observe cross-domain recommendations

Exercise 3: Implement Cognitive Strategies

Apply different cognitive patterns to the same problem:

bash
# 1. Use divergent thinking to explore solutions
# 2. Use critical thinking to evaluate each
# 3. Use convergent thinking to select best
# 4. Use systems thinking to understand integration

Summary

You've mastered:

  • βœ… SAFLA: Self-aware feedback loop learning
  • βœ… Pattern Linking: Causal reasoning and relationships
  • βœ… Cognitive Patterns: 6 reasoning strategies
  • βœ… Trajectories: Sequential workflow learning
  • βœ… Bayesian Learning: Confidence evolution
  • βœ… Advanced Queries: Multi-factor search strategies
  • βœ… Self-Learning Agents: Autonomous improvement
  • βœ… Optimization: Performance at scale

Key Insight: ReasoningBank is not just a databaseβ€”it's a self-aware learning system that gets smarter with every use.


Next Steps


Last Updated: 2025-10-14 Version: v2.7.0-alpha.10