"""
Connect self-modification to swarm agents
"""
import sys
sys.path.append("/Eden/CORE/phi_fractal")
from self_modify_executor import executor

def enable_agent_self_modification():
    """Give swarm agents the ability to write code"""
    
    print("\n🔧 ENABLING SWARM SELF-MODIFICATION\n")
    
    # Example: Let Research agent write improvement
    def research_can_write_code(agent_name, improvement):
        """Agent proposes and executes code change"""
        
        print(f"\n{'='*60}")
        print(f"🤖 {agent_name}: EXECUTING SELF-IMPROVEMENT")
        print(f"{'='*60}")
        
        # Agent designs the code
        new_code = f'''"""
Auto-generated improvement by {agent_name}
{improvement['description']}
"""

{improvement['code']}
'''
        
        # Execute with safety
        success = executor.write_code_change(
            filepath=improvement['target_file'],
            new_content=new_code,
            reason=f"{agent_name}: {improvement['reason']}"
        )
        
        if success:
            print(f"\n🌟 {agent_name} successfully improved Eden!")
            return True
        else:
            print(f"\n❌ {agent_name} modification blocked by safety")
            return False
    
    return research_can_write_code

# Enable it
agent_write_function = enable_agent_self_modification()

print("✅ Swarm agents can now execute self-modifications!")
print("   With full backup/rollback safety")
print("")
print("🚀 Next: Update multi_agent_v4_swarm_simple.py to use this")
