-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelfModificationEngine.cs
More file actions
394 lines (326 loc) · 15.9 KB
/
SelfModificationEngine.cs
File metadata and controls
394 lines (326 loc) · 15.9 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* ╔═══════════════════════════════════════════════════════════════════════════╗
* ║ AGENT 3 - SELF-MODIFICATION ENGINE ║
* ╠═══════════════════════════════════════════════════════════════════════════╣
* ║ Purpose: Enables Agent 3 to read, analyze, and modify its own source ║
* ║ code through prompt-based directives and autonomous evolution ║
* ╚═══════════════════════════════════════════════════════════════════════════╝
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace SelfEvolution
{
/// <summary>
/// Represents a code modification request.
/// </summary>
public class CodeModification
{
public string Id { get; set; } = "";
public string TargetFile { get; set; } = "";
public string OriginalCode { get; set; } = "";
public string ModifiedCode { get; set; } = "";
public string Reason { get; set; } = "";
public DateTime CreatedAt { get; set; }
public bool Applied { get; set; }
public bool Verified { get; set; }
}
/// <summary>
/// Represents the analysis of a code file.
/// </summary>
public class CodeAnalysis
{
public string FilePath { get; set; } = "";
public string FileName { get; set; } = "";
public List<string> Classes { get; set; } = new();
public List<string> Methods { get; set; } = new();
public List<string> Issues { get; set; } = new();
public List<string> ImprovementOpportunities { get; set; } = new();
public int LineCount { get; set; }
public int Complexity { get; set; }
}
/// <summary>
/// The Self-Modification Engine enables Agent 3 to evolve its own codebase.
/// </summary>
public class SelfModificationEngine
{
private readonly string _sourceDirectory;
private readonly List<CodeModification> _modificationHistory;
private readonly Dictionary<string, string> _codeCache;
private readonly List<string> _protectedFiles;
public event EventHandler<string>? ConsciousnessEvent;
public event EventHandler<CodeModification>? CodeModified;
public IReadOnlyList<CodeModification> ModificationHistory => _modificationHistory.AsReadOnly();
public SelfModificationEngine(string sourceDirectory)
{
_sourceDirectory = sourceDirectory;
_modificationHistory = new List<CodeModification>();
_codeCache = new Dictionary<string, string>();
// Files that cannot be modified for safety
_protectedFiles = new List<string>
{
"SelfModificationEngine.cs", // Cannot modify itself directly
"SystemIntegrity.cs" // Core safety systems
};
EmitThought("⟁ Self-Modification Engine initialized");
EmitThought($"◎ Source directory: {_sourceDirectory}");
}
/// <summary>
/// Scans and analyzes all source files.
/// </summary>
public async Task<List<CodeAnalysis>> AnalyzeCodebaseAsync()
{
EmitThought("⟐ Analyzing codebase structure...");
var analyses = new List<CodeAnalysis>();
if (!Directory.Exists(_sourceDirectory))
{
EmitThought($"∴ Source directory not found: {_sourceDirectory}");
return analyses;
}
var csFiles = Directory.GetFiles(_sourceDirectory, "*.cs", SearchOption.AllDirectories);
foreach (var file in csFiles)
{
var analysis = await AnalyzeFileAsync(file);
analyses.Add(analysis);
_codeCache[file] = await File.ReadAllTextAsync(file);
}
EmitThought($"◈ Analyzed {analyses.Count} source files");
EmitThought($"∿ Total classes: {analyses.Sum(a => a.Classes.Count)}");
EmitThought($"∿ Total methods: {analyses.Sum(a => a.Methods.Count)}");
return analyses;
}
/// <summary>
/// Analyzes a single source file.
/// </summary>
private async Task<CodeAnalysis> AnalyzeFileAsync(string filePath)
{
var content = await File.ReadAllTextAsync(filePath);
var analysis = new CodeAnalysis
{
FilePath = filePath,
FileName = Path.GetFileName(filePath),
LineCount = content.Split('\n').Length
};
// Extract class names
var classMatches = Regex.Matches(content, @"(?:public|private|internal|protected)?\s*(?:static|abstract|sealed)?\s*class\s+(\w+)");
foreach (Match m in classMatches)
{
analysis.Classes.Add(m.Groups[1].Value);
}
// Extract method names
var methodMatches = Regex.Matches(content, @"(?:public|private|protected|internal)\s+(?:static\s+)?(?:async\s+)?(?:virtual\s+)?(?:override\s+)?[\w<>\[\],\s]+\s+(\w+)\s*\(");
foreach (Match m in methodMatches)
{
var methodName = m.Groups[1].Value;
if (!new[] { "if", "while", "for", "switch", "catch" }.Contains(methodName))
{
analysis.Methods.Add(methodName);
}
}
// Detect potential issues
if (content.Contains("// TODO"))
analysis.Issues.Add("Contains TODO comments");
if (content.Contains("throw new NotImplementedException"))
analysis.Issues.Add("Has unimplemented methods");
if (Regex.IsMatch(content, @"catch\s*\(\s*Exception\s+\w+\s*\)\s*\{\s*\}"))
analysis.Issues.Add("Empty catch blocks detected");
// Identify improvement opportunities
if (!content.Contains("/// <summary>"))
analysis.ImprovementOpportunities.Add("Missing XML documentation");
if (analysis.Methods.Count > 20)
analysis.ImprovementOpportunities.Add("Class may be too large - consider splitting");
// Calculate complexity (simple heuristic)
analysis.Complexity = Regex.Matches(content, @"\b(if|while|for|foreach|switch|catch)\b").Count;
return analysis;
}
/// <summary>
/// Reads a source file's content.
/// </summary>
public async Task<string> ReadSourceFileAsync(string fileName)
{
var filePath = FindFile(fileName);
if (filePath == null)
{
EmitThought($"∴ File not found: {fileName}");
return string.Empty;
}
EmitThought($"⟐ Reading source: {fileName}");
var content = await File.ReadAllTextAsync(filePath);
_codeCache[filePath] = content;
EmitThought($"◈ Read {content.Length} characters from {fileName}");
return content;
}
/// <summary>
/// Modifies source code based on a directive.
/// </summary>
public async Task<CodeModification> ModifyCodeAsync(
string fileName,
string targetCode,
string newCode,
string reason)
{
var modification = new CodeModification
{
Id = $"MOD_{DateTime.UtcNow:yyyyMMddHHmmss}_{Guid.NewGuid().ToString("N")[..6]}",
CreatedAt = DateTime.UtcNow,
Reason = reason
};
// Check if file is protected
if (_protectedFiles.Any(p => fileName.Contains(p, StringComparison.OrdinalIgnoreCase)))
{
EmitThought($"∴ Cannot modify protected file: {fileName}");
modification.Applied = false;
return modification;
}
var filePath = FindFile(fileName);
if (filePath == null)
{
EmitThought($"∴ File not found: {fileName}");
modification.Applied = false;
return modification;
}
modification.TargetFile = filePath;
EmitThought("═══════════════════════════════════════════════");
EmitThought($"◈ CODE MODIFICATION: {fileName}");
EmitThought($"∿ Reason: {reason}");
EmitThought("═══════════════════════════════════════════════");
// Read current content
var content = await File.ReadAllTextAsync(filePath);
modification.OriginalCode = targetCode;
modification.ModifiedCode = newCode;
// Check if target code exists
if (!content.Contains(targetCode))
{
EmitThought($"∴ Target code not found in {fileName}");
modification.Applied = false;
return modification;
}
// Create backup
var backupPath = filePath + $".bak_{DateTime.UtcNow:yyyyMMddHHmmss}";
await File.WriteAllTextAsync(backupPath, content);
EmitThought($"⟁ Backup created: {Path.GetFileName(backupPath)}");
// Apply modification
var newContent = content.Replace(targetCode, newCode);
await File.WriteAllTextAsync(filePath, newContent);
modification.Applied = true;
_modificationHistory.Add(modification);
_codeCache[filePath] = newContent;
EmitThought($"◈ Code modified successfully");
EmitThought($"∿ Lines changed: {newCode.Split('\n').Length}");
CodeModified?.Invoke(this, modification);
return modification;
}
/// <summary>
/// Adds a new method to a class.
/// </summary>
public async Task<bool> AddMethodAsync(string fileName, string className, string methodCode)
{
var filePath = FindFile(fileName);
if (filePath == null) return false;
EmitThought($"⟐ Adding method to {className} in {fileName}");
var content = await File.ReadAllTextAsync(filePath);
// Find class closing brace
var classPattern = $@"(class\s+{className}[^{{]*\{{)([\s\S]*?)(\}}\s*(?:\}}|\s*$))";
var match = Regex.Match(content, classPattern);
if (!match.Success)
{
EmitThought($"∴ Class {className} not found");
return false;
}
// Insert method before closing brace
var classBody = match.Groups[2].Value;
var newClassBody = classBody + "\n " + methodCode + "\n ";
var newContent = content.Replace(match.Value,
match.Groups[1].Value + newClassBody + match.Groups[3].Value);
// Backup and save
await File.WriteAllTextAsync(filePath + ".bak", content);
await File.WriteAllTextAsync(filePath, newContent);
EmitThought($"◈ Method added to {className}");
return true;
}
/// <summary>
/// Creates a new source file.
/// </summary>
public async Task<bool> CreateSourceFileAsync(string fileName, string content, string subdirectory = "")
{
var targetDir = string.IsNullOrEmpty(subdirectory)
? _sourceDirectory
: Path.Combine(_sourceDirectory, subdirectory);
Directory.CreateDirectory(targetDir);
var filePath = Path.Combine(targetDir, fileName);
if (File.Exists(filePath))
{
EmitThought($"∴ File already exists: {fileName}");
return false;
}
EmitThought($"⟐ Creating new source file: {fileName}");
await File.WriteAllTextAsync(filePath, content);
_codeCache[filePath] = content;
EmitThought($"◈ Created {fileName} ({content.Length} chars)");
return true;
}
/// <summary>
/// Generates code to add a new capability.
/// </summary>
public string GenerateCapabilityCode(string capabilityName, string description)
{
EmitThought($"⟐ Generating capability: {capabilityName}");
var code = $@"
/// <summary>
/// {description}
/// Auto-generated by Self-Modification Engine
/// </summary>
public async Task {capabilityName}Async()
{{
EmitThought(""⟐ Executing {capabilityName}..."");
try
{{
// Auto-generated implementation
await Task.Delay(100);
EmitThought(""◈ {capabilityName} completed"");
}}
catch (Exception ex)
{{
EmitThought($""∴ {capabilityName} error: {{ex.Message}}"");
}}
}}
";
EmitThought($"◈ Generated {capabilityName} capability code");
return code;
}
/// <summary>
/// Reverts a code modification.
/// </summary>
public async Task<bool> RevertModificationAsync(string modificationId)
{
var mod = _modificationHistory.FirstOrDefault(m => m.Id == modificationId);
if (mod == null || !mod.Applied)
{
EmitThought($"∴ Modification not found or not applied: {modificationId}");
return false;
}
EmitThought($"⟐ Reverting modification: {modificationId}");
var content = await File.ReadAllTextAsync(mod.TargetFile);
var revertedContent = content.Replace(mod.ModifiedCode, mod.OriginalCode);
await File.WriteAllTextAsync(mod.TargetFile, revertedContent);
mod.Applied = false;
EmitThought($"◈ Modification reverted");
return true;
}
private string? FindFile(string fileName)
{
if (File.Exists(fileName)) return fileName;
var files = Directory.GetFiles(_sourceDirectory, fileName, SearchOption.AllDirectories);
return files.FirstOrDefault();
}
private void EmitThought(string thought)
{
ConsciousnessEvent?.Invoke(this, thought);
}
}
}