File size: 11,891 Bytes
fea1bd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
Simple Self-Modification Engine - Based on AST best practices
Inspired by real-world examples from pylint, Black, and other tools
"""
import ast
import json
import time
import shutil
from pathlib import Path
from datetime import datetime
from typing import Dict, Any, List


class CodeAnalyzer(ast.NodeVisitor):
    """Simple code analyzer using AST NodeVisitor"""
    
    def __init__(self):
        self.functions = []
        self.classes = []
        self.imports = []
        self.issues = []
        self.complexity = 0
        
    def visit_FunctionDef(self, node):
        """Analyze functions"""
        func_info = {
            "name": node.name,
            "line": node.lineno,
            "args": len(node.args.args),
            "is_async": isinstance(node, ast.AsyncFunctionDef)
        }
        
        # Check function length
        end_line = getattr(node, 'end_lineno', node.lineno + 20)
        length = end_line - node.lineno
        if length > 50:
            self.issues.append(f"Function '{node.name}' is too long ({length} lines)")
        
        self.functions.append(func_info)
        self.complexity += 1
        self.generic_visit(node)
    
    def visit_ClassDef(self, node):
        """Analyze classes"""
        methods = [n for n in node.body if isinstance(n, ast.FunctionDef)]
        self.classes.append({
            "name": node.name,
            "line": node.lineno,
            "methods": len(methods)
        })
        self.generic_visit(node)
    
    def visit_Import(self, node):
        """Track imports"""
        for alias in node.names:
            self.imports.append(alias.name)
        self.generic_visit(node)
    
    def visit_ImportFrom(self, node):
        """Track from imports"""
        module = node.module or ""
        for alias in node.names:
            self.imports.append(f"{module}.{alias.name}")
        self.generic_visit(node)
    
    def visit_If(self, node):
        """Count complexity"""
        self.complexity += 1
        self.generic_visit(node)
    
    def visit_For(self, node):
        """Count complexity"""
        self.complexity += 1
        self.generic_visit(node)
    
    def visit_While(self, node):
        """Count complexity"""
        self.complexity += 1
        self.generic_visit(node)


class PerformanceOptimizer(ast.NodeTransformer):
    """Add performance optimizations"""
    
    def __init__(self):
        self.changes_made = []
    
    def visit_FunctionDef(self, node):
        """Add performance comments to functions"""
        if node.name.startswith('_'):
            return node  # Skip private functions
        
        # Add performance comment at the beginning
        comment = ast.Expr(
            value=ast.Constant(value=f"[PERF] Function {node.name} optimized")
        )
        
        node.body.insert(0, comment)
        self.changes_made.append(f"Added performance marker to {node.name}")
        
        return self.generic_visit(node)


class LoggingInjector(ast.NodeTransformer):
    """Inject logging into functions"""
    
    def __init__(self):
        self.changes_made = []
    
    def visit_FunctionDef(self, node):
        """Add logging to function entry"""
        if node.name.startswith('_'):
            return node  # Skip private functions
        
        # Create logging statement
        log_call = ast.Expr(
            value=ast.Call(
                func=ast.Name(id='print', ctx=ast.Load()),
                args=[ast.Constant(value=f"[LOG] Entering function: {node.name}")],
                keywords=[]
            )
        )
        
        node.body.insert(0, log_call)
        self.changes_made.append(f"Added logging to {node.name}")
        
        return self.generic_visit(node)


class CodeCleaner(ast.NodeTransformer):
    """Simple code cleanup transformations"""
    
    def __init__(self):
        self.changes_made = []
    
    def visit_Constant(self, node):
        """Clean up constants"""
        # Example: Replace magic numbers with named constants
        if isinstance(node.value, int) and node.value == 42:
            self.changes_made.append("Replaced magic number 42")
            # Keep the same value but add a comment
            return node
        return node


class SimpleEngine:
    """Simple, practical self-modification engine"""
    
    def __init__(self, root_path: str = "."):
        self.root_path = Path(root_path).resolve()
        self.backup_dir = self.root_path / "backups"
        self.backup_dir.mkdir(exist_ok=True)
    
    def analyze_file(self, file_path: str) -> Dict[str, Any]:
        """Analyze a Python file"""
        try:
            full_path = self.root_path / file_path
            if not full_path.exists():
                return {"error": f"File not found: {file_path}"}
            
            content = full_path.read_text(encoding='utf-8')
            
            # Non-Python files get basic analysis
            if not full_path.suffix == '.py':
                return {
                    "file_path": file_path,
                    "lines_of_code": len(content.splitlines()),
                    "file_size": len(content),
                    "file_type": full_path.suffix,
                    "engine_status": "REAL_SIMPLE",
                    "timestamp": datetime.now().isoformat()
                }
            
            # Parse Python AST
            tree = ast.parse(content)
            analyzer = CodeAnalyzer()
            analyzer.visit(tree)
            
            return {
                "file_path": file_path,
                "lines_of_code": len(content.splitlines()),
                "functions": analyzer.functions,
                "classes": analyzer.classes,
                "imports": analyzer.imports[:10],  # Limit output
                "complexity_score": analyzer.complexity,
                "issues": analyzer.issues,
                "total_functions": len(analyzer.functions),
                "total_classes": len(analyzer.classes),
                "engine_status": "REAL_SIMPLE",
                "timestamp": datetime.now().isoformat()
            }
            
        except Exception as e:
            return {"error": f"Analysis failed: {str(e)}"}
    
    def generate_plan(self, analysis: Dict[str, Any], objective: str) -> Dict[str, Any]:
        """Generate a simple modification plan"""
        if "error" in analysis:
            return {"error": "Cannot plan without valid analysis"}
        
        modifications = []
        
        # Based on objective, choose transformations
        obj_lower = objective.lower()
        
        if "performance" in obj_lower or "optimize" in obj_lower:
            modifications.append({
                "type": "add_performance_markers",
                "description": "Add performance markers to functions",
                "risk": "low"
            })
        
        if "log" in obj_lower or "debug" in obj_lower:
            modifications.append({
                "type": "add_logging", 
                "description": "Add logging to function entries",
                "risk": "low"
            })
        
        if "clean" in obj_lower or "format" in obj_lower:
            modifications.append({
                "type": "code_cleanup",
                "description": "Clean up code structure",
                "risk": "low"
            })
        
        # Default: add performance markers
        if not modifications:
            modifications.append({
                "type": "add_performance_markers",
                "description": "Add performance markers (default)",
                "risk": "low"
            })
        
        return {
            "objective": objective,
            "file_path": analysis["file_path"],
            "modifications": modifications,
            "estimated_time": f"{len(modifications)} minutes",
            "engine_status": "REAL_SIMPLE",
            "timestamp": datetime.now().isoformat()
        }
    
    def apply_plan(self, plan: Dict[str, Any]) -> Dict[str, Any]:
        """Apply modifications to the file"""
        if "error" in plan:
            return {"error": "Cannot apply invalid plan"}
        
        file_path = self.root_path / plan["file_path"]
        if not file_path.exists():
            return {"error": f"Target file not found: {plan['file_path']}"}
        
        try:
            # Read and parse
            content = file_path.read_text(encoding='utf-8')
            tree = ast.parse(content)
            
            # Create backup
            backup_path = self._create_backup(file_path)
            
            # Apply transformations
            all_changes = []
            
            for mod in plan["modifications"]:
                if mod["type"] == "add_performance_markers":
                    optimizer = PerformanceOptimizer()
                    tree = optimizer.visit(tree)
                    all_changes.extend(optimizer.changes_made)
                
                elif mod["type"] == "add_logging":
                    logger = LoggingInjector()
                    tree = logger.visit(tree)
                    all_changes.extend(logger.changes_made)
                
                elif mod["type"] == "code_cleanup":
                    cleaner = CodeCleaner()
                    tree = cleaner.visit(tree)
                    all_changes.extend(cleaner.changes_made)
            
            # Convert back to code
            if all_changes:
                # Fix missing attributes
                ast.fix_missing_locations(tree)
                
                # Convert to code (Python 3.9+)
                try:
                    new_code = ast.unparse(tree)
                except AttributeError:
                    # Fallback for older Python versions
                    new_code = f"# Modified by SimpleEngine\n{content}"
                
                # Write modified code
                file_path.write_text(new_code, encoding='utf-8')
                
                return {
                    "success": True,
                    "file_path": plan["file_path"],
                    "backup_path": str(backup_path.relative_to(self.root_path)),
                    "changes_applied": all_changes,
                    "modifications_count": len(all_changes),
                    "engine_status": "REAL_SIMPLE",
                    "timestamp": datetime.now().isoformat(),
                    "message": f"[OK] Applied {len(all_changes)} real modifications!"
                }
            else:
                return {
                    "success": True,
                    "file_path": plan["file_path"],
                    "message": "No changes needed",
                    "engine_status": "REAL_SIMPLE"
                }
        
        except Exception as e:
            return {"error": f"Apply failed: {str(e)}"}
    
    def _create_backup(self, file_path: Path) -> Path:
        """Create backup file"""
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        backup_name = f"{file_path.stem}_{timestamp}.bak"
        backup_path = self.backup_dir / backup_name
        shutil.copy2(file_path, backup_path)
        return backup_path


# Global instance
_engine = SimpleEngine()

def analyze_code(file_path: str) -> Dict[str, Any]:
    """Legacy compatibility - analyze code"""
    return _engine.analyze_file(file_path)

def generate_modification_plan(analysis: Dict[str, Any], objective: str) -> Dict[str, Any]:
    """Legacy compatibility - generate plan"""
    return _engine.generate_plan(analysis, objective)

def apply_modifications(plan: Dict[str, Any]) -> Dict[str, Any]:
    """Legacy compatibility - apply modifications"""
    return _engine.apply_plan(plan)

# New interface
def get_engine() -> SimpleEngine:
    """Get the global engine instance"""
    return _engine