ai agent refactoring