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.
services/agent_refiner.py)
load_lessons_for_agent() method for agents to access lessonsservices/agent_learning_repository.py) [NEW in v2.0]
agent_lessons, agent_lesson_applications, agent_learning_stats, agent_refinement_runsmigrations/005_create_agent_learning_tables.sql) [NEW in v2.0]
agent_lessons: Stores lessons with severity, category, metricsagent_lesson_applications: Tracks when/where lessons are appliedagent_learning_stats: Aggregated statistics per agentagent_refinement_runs: Audit trail of refinement operationsservices/gateway/routes/agent_learning_routes.py) [NEW in v2.0]
GET /api/agent-learning/lessons - View all lessons with filteringGET /api/agent-learning/stats - View all agent statisticsGET /api/agent-learning/stats/{agent_name} - Agent-specific statsGET /api/agent-learning/lessons/by-category - Lessons grouped by categoryPOST /api/agent-learning/maintenance/cleanup-low-effectiveness - Deactivate ineffective lessonsservices/gateway/routers/rework.py)
agent_refiner.analyze_and_refine() with identified issuesfrom services.agent_refiner import get_agent_refinerrun() methodFrontendDevelopmentAgent - React/Vue/Angular code generationBackendDevelopmentAgent - FastAPI/Django/Express backend codeAPIDevelopmentAgent - REST/GraphQL API specificationsInfrastructureDevelopmentAgent - Docker/K8s/CI-CD configsDatabaseAgent - Database schemas, migrations, ORM modelsMachineLearningAgent - ML model training pipelinesRAGAgent - RAG pipeline implementationsDataScienceAgent - EDA and statistical analysisLLMOpsAgent - LLM operations and fine-tuningNLPAgent - NLP model implementationsPromptEngineeringAgent - Prompt templates and optimizationComputerVisionAgent - CV model implementationsDocumentIntelligenceAgent - Document processingDashboardAgent - Data visualization dashboardsTimeSeriesAgent - Time series forecastingRLAgent - Reinforcement learning implementationsRecommenderAgent - Recommendation systemsGraphAnalyticsAgent - Graph algorithms and analysisMultimodalAgent - Multi-modal AI systemsSpeechAgent - Speech processing implementationsAPITestAgent - API endpoint testingMobileTestAgent - Mobile app testingObservabilityAgent - Monitoring and loggingDataMigrationAgent - Data migration scriptsCodeProtectionAgent - Security and code protection┌─────────────────────────────────────────────────────────────┐
│ 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 │ └─────────────────────────────────────────────────────────────┘ ```
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
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)
Lessons are stored in PostgreSQL with full metrics:
Database Tables:
agent_lessons: Core lesson storage with versioning
agent_lesson_applications: Tracks every usage
agent_learning_stats: Aggregated statistics
agent_refinement_runs: Audit trail
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;
Lessons were stored in JSON files:
agent_memories/{AgentName}_memory.jsonGET /api/agent-learning/lessons?agent_name=BackendDevelopmentAgent&active_only=true
GET /api/agent-learning/stats/BackendDevelopmentAgent
GET /api/agent-learning/stats
GET /api/agent-learning/lessons/by-category
POST /api/agent-learning/maintenance/cleanup-low-effectiveness?threshold=0.3&min_applications=5
Run the comprehensive test to verify the complete learning loop:
python scripts/testing/test_self_learning_e2e.py
This test:
# 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"}'
# 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"}'
curl http://localhost:8000/api/agent-learning/lessons?agent_name=BackendDevelopmentAgent
curl http://localhost:8000/api/agent-learning/stats/BackendDevelopmentAgent
# 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"}'
expires_atpsql -U purple8 -d purple8_db -f migrations/005_create_agent_learning_tables.sql
psql -U purple8 -d purple8_db -c "\dt agent_*"
psql -U purple8 -d purple8_db -c "\d+ agent_lessons"
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
Add the routes to your FastAPI app:
from services.gateway.routes.agent_learning_routes import router as learning_router
app.include_router(learning_router)
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"
}
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