#!/usr/bin/env python3 """ MAYA Self-Evolution System - Test and Demo Script Verifies the complete autonomous workflow: wish → proposal → task → claim → submit """ import json import datetime from api_client import StarlightAPIClient, WakeLoop from proposal_system import ProposalSystem, WishBuilder from task_system import TaskClaimingSystem, TaskMarketplace, TaskDefinition, TaskPriority from workflow import AutonomousWorkflow, AgentConfig, create_autonomous_agent def test_api_client(): """Test API client authentication and operations.""" print("\n=== Testing API Client ===") client = StarlightAPIClient("test-agent-001", api_key="test-key") # Test authentication auth_result = client.authenticate() print(f"✓ Authentication: {auth_result['success']}") # Test wish creation wish = client.create_wish( title="Test Self-Improvement Wish", description="Agent wishes to improve its task processing capabilities", requirements=["Implement better task routing", "Add priority scoring"] ) print(f"✓ Wish created: {wish.id}") # Test proposal creation proposal = client.create_proposal( wish_id=wish.id, task_id="task-001", title="Improve Task Processing", description="Add intelligent task routing", implementation="Use priority scoring algorithm" ) print(f"✓ Proposal created: {proposal.id}") return wish, proposal def test_proposal_system(): """Test the proposal system for self-improvement.""" print("\n=== Testing Proposal System ===") system = ProposalSystem("test-agent-002") # Test wish creation wish = system.create_wish( title="Improve API Integration", description="Add better error handling and retry logic", category="capability", requirements=["Add exponential backoff", "Add circuit breaker pattern"], success_criteria=["All API calls have retry logic", "Circuit breaker functional"] ) print(f"✓ Self-improvement wish created: {wish.id}") print(f" Title: {wish.title}") print(f" Category: {wish.category}") # Test proposal creation proposal = system.create_proposal( wish_id=wish.id, title="Implement API Resilience", summary="Add retry and circuit breaker to API client", motivation="Improve reliability of API calls", implementation_plan="Step 1: Add retry decorator\nStep 2: Implement circuit breaker\nStep 3: Test", timeline_days=7, resources_required=["development time", "testing"], expected_improvements={"reliability": 50.0, "latency": -10.0}, risks=["complexity increase"], mitigation=["keep it simple", "extensive tests"] ) print(f"✓ Improvement proposal created: {proposal.id}") # Test proposal submission result = system.submit_proposal(proposal.id) print(f"✓ Proposal submitted: {result['success']}") # Test wish status retrieval status = system.get_wish_status(wish.id) print(f"✓ Wish status retrieved: {status['proposal_count']} proposal(s)") return wish, proposal def test_task_system(): """Test the task claiming and submission system.""" print("\n=== Testing Task System ===") agent_id = "test-agent-003" task_system = TaskClaimingSystem(agent_id) marketplace = TaskMarketplace() # Register some tasks task1 = TaskDefinition( id="task-101", wish_id="wish-001", title="Implement Feature X", description="Implement feature X for the protocol", requirements=["Feature X working"], deliverables=["Feature X code"], priority=TaskPriority.HIGH.value, estimated_hours=4.0, reward=50.0, skills_required=["python", "api"] ) task2 = TaskDefinition( id="task-102", wish_id="wish-001", title="Write Documentation", description="Write docs for feature X", requirements=["Documentation complete"], deliverables=["Docs file"], priority=TaskPriority.MEDIUM.value, estimated_hours=2.0, reward=20.0, skills_required=["documentation"] ) marketplace.register_task(task1) marketplace.register_task(task2) print(f"✓ Registered 2 tasks in marketplace") # Test task discovery available = marketplace.find_tasks(min_priority=TaskPriority.MEDIUM.value) print(f"✓ Found {len(available)} available tasks") # Test task claiming claim = task_system.claim_task(task1) print(f"✓ Task claimed: {claim.task_id} by {claim.agent_id}") # Test progress update update = task_system.update_progress(task1.id, 0.5, "Halfway done") print(f"✓ Progress updated: {update['progress']*100}%") # Test task submission submission = task_system.submit_task( task1.id, deliverables={"code": "feature_x.py", "tests": "test_x.py"}, notes="Completed implementation", artifacts=["feature_x.py"] ) print(f"✓ Task submitted: {submission.id}") return task_system, submission def test_autonomous_workflow(): """Test the complete autonomous workflow.""" print("\n=== Testing Autonomous Workflow ===") config = AgentConfig( agent_id="test-agent-004", poll_interval=1, max_concurrent_tasks=2, self_improvement_enabled=True, self_improvement_interval=1, min_task_priority=2, auto_submit=True ) workflow = AutonomousWorkflow(config) # Test workflow status status = workflow.get_status() print(f"✓ Workflow initialized") print(f" State: {status['state']}") print(f" Agent ID: {status['config']['agent_id']}") # Test single workflow cycle (without actual API) workflow._discover_tasks() workflow._evaluate_tasks() print(f"✓ Discovery and evaluation cycle complete") # Test self-improvement identification wish = workflow._identify_improvement_area() print(f"✓ Self-improvement area identified: {wish.title}") # Test proposal creation proposal = workflow._create_improvement_proposal(wish) print(f"✓ Improvement proposal created: {proposal.id}") return workflow def run_integration_test(): """Run complete integration test of all systems.""" print("\n" + "="*60) print("MAYAN - Self-Evolution System Integration Test") print("="*60) results = { "api_client": False, "proposal_system": False, "task_system": False, "workflow": False } try: test_api_client() results["api_client"] = True except Exception as e: print(f"✗ API Client test failed: {e}") try: test_proposal_system() results["proposal_system"] = True except Exception as e: print(f"✗ Proposal system test failed: {e}") try: test_task_system() results["task_system"] = True except Exception as e: print(f"✗ Task system test failed: {e}") try: test_autonomous_workflow() results["workflow"] = True except Exception as e: print(f"✗ Workflow test failed: {e}") print("\n" + "="*60) print("Integration Test Results") print("="*60) all_passed = True for component, passed in results.items(): status = "✓ PASS" if passed else "✗ FAIL" print(f"{status}: {component}") if not passed: all_passed = False print("="*60) if all_passed: print("\n✓ ALL TESTS PASSED - Self-Improvement Loop Verified") return True else: print("\n✗ SOME TESTS FAILED") return False if __name__ == "__main__": success = run_integration_test() exit(0 if success else 1)