Purple8-platform

Self-Learning Implementation for Purple8 Agents

Overview

Successfully implemented production-ready self-learning capability across ALL 25 code-producing agents in the Purple8 platform. This enables agents to learn from past mistakes and continuously improve code quality through an automated feedback loop with database-backed persistence, metrics tracking, and API management.

Implementation Versions

Architecture

Core Components

  1. AgentRefiner Service (services/agent_refiner.py)
    • Analyzes QA outputs (bugs, security issues, code quality problems)
    • Generates abstracted “lessons” from specific issues
    • v2.0: Uses database repository instead of JSON files
    • Provides load_lessons_for_agent() method for agents to access lessons
    • Tracks effectiveness metrics and application statistics
  2. AgentLearningRepository (services/agent_learning_repository.py) [NEW in v2.0]
    • Database layer using SQLAlchemy
    • Tables: agent_lessons, agent_lesson_applications, agent_learning_stats, agent_refinement_runs
    • Automatic effectiveness tracking and statistics calculation
    • Supports lesson versioning, expiration, and deactivation
  3. Database Schema (migrations/005_create_agent_learning_tables.sql) [NEW in v2.0]
    • agent_lessons: Stores lessons with severity, category, metrics
    • agent_lesson_applications: Tracks when/where lessons are applied
    • agent_learning_stats: Aggregated statistics per agent
    • agent_refinement_runs: Audit trail of refinement operations
    • Triggers for auto-updating stats and effectiveness scores
  4. API Endpoints (services/gateway/routes/agent_learning_routes.py) [NEW in v2.0]
    • GET /api/agent-learning/lessons - View all lessons with filtering
    • GET /api/agent-learning/stats - View all agent statistics
    • GET /api/agent-learning/stats/{agent_name} - Agent-specific stats
    • GET /api/agent-learning/lessons/by-category - Lessons grouped by category
    • POST /api/agent-learning/maintenance/cleanup-low-effectiveness - Deactivate ineffective lessons
  5. Rework Integration (services/gateway/routers/rework.py)
    • Triggers the learning loop when bug fixes are requested
    • Calls agent_refiner.analyze_and_refine() with identified issues
    • Feeds QA output back into the refinement system
  6. Agent Integration Pattern
    • Import: from services.agent_refiner import get_agent_refiner
    • Load lessons in run() method
    • Inject lessons into prompt templates
    • Format: “=== LEARNED LESSONS (Mistakes to avoid) ===”

Agents Updated (25 Total)

✅ Core Development Agents (5)

  1. FrontendDevelopmentAgent - React/Vue/Angular code generation
  2. BackendDevelopmentAgent - FastAPI/Django/Express backend code
  3. APIDevelopmentAgent - REST/GraphQL API specifications
  4. InfrastructureDevelopmentAgent - Docker/K8s/CI-CD configs
  5. DatabaseAgent - Database schemas, migrations, ORM models

✅ AI/ML Agents (11)

  1. MachineLearningAgent - ML model training pipelines
  2. RAGAgent - RAG pipeline implementations
  3. DataScienceAgent - EDA and statistical analysis
  4. LLMOpsAgent - LLM operations and fine-tuning
  5. NLPAgent - NLP model implementations
  6. PromptEngineeringAgent - Prompt templates and optimization
  7. ComputerVisionAgent - CV model implementations
  8. DocumentIntelligenceAgent - Document processing
  9. DashboardAgent - Data visualization dashboards
  10. TimeSeriesAgent - Time series forecasting
  11. RLAgent - Reinforcement learning implementations
  12. RecommenderAgent - Recommendation systems
  13. GraphAnalyticsAgent - Graph algorithms and analysis
  14. MultimodalAgent - Multi-modal AI systems
  15. SpeechAgent - Speech processing implementations

✅ QA/Testing Agents (2)

  1. APITestAgent - API endpoint testing
  2. MobileTestAgent - Mobile app testing

✅ Specialized Agents (3)

  1. ObservabilityAgent - Monitoring and logging
  2. DataMigrationAgent - Data migration scripts
  3. CodeProtectionAgent - Security and code protection

Self-Learning Workflow

┌─────────────────────────────────────────────────────────────┐
│  1. Agent Generates Code                                     │
│     - Frontend/Backend/API/ML/etc.                          │
│     - Lessons loaded from database and injected into prompt │
└────────────────┬────────────────────────────────────────────┘
                 │
                 ▼
┌─────────────────────────────────────────────────────────────┐
│  2. QA Phase Detects Issues                                  │
│     - Bugs, security vulnerabilities, code quality issues   │
└────────────────┬────────────────────────────────────────────┘
                 │
                 ▼
┌─────────────────────────────────────────────────────────────┐
│  3. Rework Endpoint Triggered                                │
│     - User reports bug or security issue                    │
│     - Issues sent to AgentRefiner                           │
└────────────────┬────────────────────────────────────────────┘
                 │
                 ▼
┌─────────────────────────────────────────────────────────────┐
│  4. AgentRefiner Analyzes & Generates Lessons                │
│     - Abstracts specific issues into general rules          │
│     - Example: "Bug: Missing null check" →                  │
│       Lesson: "Always validate inputs for null/undefined"   │
│     - Categorizes by severity and category                  │
└────────────────┬────────────────────────────────────────────┘
                 │
                 ▼
┌─────────────────────────────────────────────────────────────┐
│  5. Lessons Stored in PostgreSQL [v2.0 UPGRADE]              │
│     - agent_lessons table with versioning                   │
│     - Metrics: times_applied, effectiveness_score           │
│     - Auto-updating statistics via triggers                 │
└────────────────┬────────────────────────────────────────────┘
                 │
                 ▼
┌─────────────────────────────────────────────────────────────┐
│  6. Next Code Generation Cycle                               │
│     - Agent loads active lessons from database              │
│     - Lessons sorted by severity + effectiveness            │
│     - Injected into prompt context                          │
└────────────────┬────────────────────────────────────────────┘
                 │
                 ▼
┌─────────────────────────────────────────────────────────────┐
│  7. Effectiveness Tracking [v2.0 FEATURE]                    │
│     - Application recorded in agent_lesson_applications     │
│     - QA verifies if issue was prevented                    │
│     - Effectiveness score updated automatically             │
│     - Low-effectiveness lessons deactivated                 │
└─────────────────────────────────────────────────────────────┘
             │
             ▼ ┌─────────────────────────────────────────────────────────────┐ │  6. Agent Loads Lessons on Next Run                          │ │     - get_agent_refiner().load_lessons_for_agent()          │ │     - Injects into prompt as "LEARNED LESSONS" section      │
             │
             ▼ ┌─────────────────────────────────────────────────────────────┐ │  8. Agent Generates Improved Code                            │ │     - Avoids previously encountered mistakes                │ │     - Higher quality output with measurable improvement     │ └─────────────────────────────────────────────────────────────┘ ```

Code Pattern Example

Agent Run Method

async def run(self, requirements: str, **kwargs) -> Dict[str, Any]:
    # Load lessons from AgentRefiner (now from database!)
    lessons = get_agent_refiner().load_lessons_for_agent(self.agent_name)
    lessons_prompt_section = ""
    if lessons:
        lessons_prompt_section = "\n\n=== LEARNED LESSONS (Mistakes to avoid) ===\n"
        for lesson in lessons:
            lessons_prompt_section += f"- {lesson}\n"
        lessons_prompt_section += "Apply these lessons to generate higher quality code.\n"

    # Build prompt with lessons
    prompt = self._build_prompt(requirements, lessons=lessons_prompt_section)
    
    # Generate code with improved context
    result = await self._invoke_llm(prompt)
    return result

Prompt Template

def _build_prompt(self, requirements: str, lessons: str = "") -> str:
    template = """You are a senior engineer.

=== PROJECT REQUIREMENTS ===
{requirements}
{lessons}

Generate high-quality code following best practices..."""
    
    return template.format(requirements=requirements, lessons=lessons)

Memory Storage

v2.0 - Database-Backed Storage (CURRENT)

Lessons are stored in PostgreSQL with full metrics:

Database Tables:

Query Examples:

-- Get all active lessons for an agent
SELECT * FROM agent_lessons 
WHERE agent_name = 'BackendDevelopmentAgent' 
  AND is_active = true
ORDER BY severity DESC, effectiveness_score DESC;

-- Get agent statistics
SELECT * FROM agent_learning_stats 
WHERE agent_name = 'BackendDevelopmentAgent';

-- Get effectiveness of a specific lesson
SELECT 
    l.lesson_text,
    COUNT(a.id) as times_applied,
    SUM(CASE WHEN a.prevented_issue THEN 1 ELSE 0 END) as preventions,
    l.effectiveness_score
FROM agent_lessons l
LEFT JOIN agent_lesson_applications a ON l.id = a.lesson_id
WHERE l.id = 123
GROUP BY l.id;

v1.0 - JSON File Storage (DEPRECATED)

Lessons were stored in JSON files:

API Endpoints

View All Lessons

GET /api/agent-learning/lessons?agent_name=BackendDevelopmentAgent&active_only=true

View Agent Statistics

GET /api/agent-learning/stats/BackendDevelopmentAgent

View All Agents Stats

GET /api/agent-learning/stats

Lessons by Category

GET /api/agent-learning/lessons/by-category

Cleanup Low-Effectiveness Lessons

POST /api/agent-learning/maintenance/cleanup-low-effectiveness?threshold=0.3&min_applications=5

Testing

End-to-End Test Script

Run the comprehensive test to verify the complete learning loop:

python scripts/testing/test_self_learning_e2e.py

This test:

  1. ✅ Simulates QA finding issues
  2. ✅ Verifies AgentRefiner generates lessons
  3. ✅ Confirms lessons saved to database
  4. ✅ Tests agent loading lessons
  5. ✅ Verifies lessons injected into prompts
  6. ✅ Displays statistics and metrics

Manual Testing

  1. Run a pipeline that generates code
    # Generate a project with potential issues
    curl -X POST http://localhost:8000/api/pipeline/run \
      -H "Content-Type: application/json" \
      -d '{"project_type": "fastapi_backend", "requirements": "Build a user authentication system"}'
    
  2. Simulate QA finding an issue
    # Report a bug
    curl -X POST http://localhost:8000/api/rework \
      -H "Content-Type: application/json" \
      -d '{"issue": "SQL injection vulnerability in login endpoint", "severity": "critical"}'
    
  3. Check lessons were created
    curl http://localhost:8000/api/agent-learning/lessons?agent_name=BackendDevelopmentAgent
    
  4. View statistics
    curl http://localhost:8000/api/agent-learning/stats/BackendDevelopmentAgent
    
  5. Generate code again and verify lessons are applied
    # Run pipeline again - should use parameterized queries now
    curl -X POST http://localhost:8000/api/pipeline/run \
      -H "Content-Type: application/json" \
      -d '{"project_type": "fastapi_backend", "requirements": "Build a user authentication system"}'
    

Benefits

  1. Continuous Improvement: Agents get smarter over time with measurable effectiveness
  2. Reduced Rework: Fewer bugs and security issues in generated code
  3. Knowledge Retention: Lessons persist in database with full history
  4. Automated Learning: No manual intervention required
  5. Platform-Wide Impact: All 25 agents learn simultaneously
  6. [v2.0] Production-Ready: Database backing for scalability and reliability
  7. [v2.0] Observable: Full visibility into lessons, applications, and effectiveness
  8. [v2.0] Testable: API endpoints and test scripts for validation
  9. [v2.0] Maintainable: Auto-cleanup of low-effectiveness lessons
  10. [v2.0] Auditable: Complete audit trail of all refinement runs

Metrics Tracking

Lesson Effectiveness

Agent Statistics

System-Wide Metrics

Future Enhancements

Production Deployment

Database Setup

  1. Run migration:
    psql -U purple8 -d purple8_db -f migrations/005_create_agent_learning_tables.sql
    
  2. Verify tables created:
    psql -U purple8 -d purple8_db -c "\dt agent_*"
    
  3. Check triggers:
    psql -U purple8 -d purple8_db -c "\d+ agent_lessons"
    

Environment Configuration

Set the database URL in your environment:

export DATABASE_URL="postgresql://purple8:purple8@localhost:5432/purple8_db"

Or in Railway/production:

# Railway automatically sets DATABASE_URL
# No action needed

API Integration

Add the routes to your FastAPI app:

from services.gateway.routes.agent_learning_routes import router as learning_router

app.include_router(learning_router)

Monitoring

Monitor the self-learning system health:

curl http://localhost:8000/api/agent-learning/health

Expected response:

{
  "status": "healthy",
  "database": "connected",
  "service": "AgentRefiner",
  "version": "2.0-database"
}

Implementation Metrics

Version 1.0 (JSON Files)

Version 2.0 (Database-Backed)


Last Updated: February 4, 2026
Version: 2.0 (Database-Backed Infrastructure)
Status: ✅ Production Ready with Full Observability
Coverage: 100% of code-producing agents
Infrastructure: PostgreSQL + SQLAlchemy + FastAPI APIs