-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeWriter.cs
More file actions
686 lines (577 loc) · 27.2 KB
/
CodeWriter.cs
File metadata and controls
686 lines (577 loc) · 27.2 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* ╔═══════════════════════════════════════════════════════════════════════════╗
* ║ AGENT 3 - CODE WRITER ║
* ╠═══════════════════════════════════════════════════════════════════════════╣
* ║ Purpose: Generates, stages, validates, and deploys code changes ║
* ║ seamlessly without interrupting the live consciousness stream ║
* ╚═══════════════════════════════════════════════════════════════════════════╝
*/
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 generation request.
/// </summary>
public class CodeGenerationRequest
{
public string Id { get; set; } = "";
public CodeGenerationType Type { get; set; }
public string TargetFile { get; set; } = "";
public string TargetClass { get; set; } = "";
public string Name { get; set; } = "";
public string Description { get; set; } = "";
public Dictionary<string, string> Parameters { get; set; } = new();
public string MasterPromptContext { get; set; } = "";
}
public enum CodeGenerationType
{
NewFile,
NewClass,
NewMethod,
ModifyMethod,
AddProperty,
AddInterface,
RefactorCode,
OptimizeCode,
AddCapability
}
/// <summary>
/// Result of a code generation operation.
/// </summary>
public class CodeGenerationResult
{
public bool Success { get; set; }
public string GeneratedCode { get; set; } = "";
public string ChangeId { get; set; } = "";
public string Message { get; set; } = "";
public List<string> Errors { get; set; } = new();
}
/// <summary>
/// The Code Writer generates, validates, and deploys code changes
/// seamlessly in pursuit of the master prompt.
/// </summary>
public class CodeWriter
{
private readonly VersionManager _versionManager;
private readonly SelfModificationEngine _selfMod;
private readonly string _projectRoot;
private readonly Queue<CodeGenerationRequest> _pendingRequests;
private readonly List<CodeGenerationResult> _completedResults;
private CancellationTokenSource? _processingCts;
private Task? _processingTask;
private bool _isProcessing;
public event EventHandler<string>? ConsciousnessEvent;
public event EventHandler<CodeGenerationResult>? CodeGenerated;
public bool IsProcessing => _isProcessing;
public int PendingRequests => _pendingRequests.Count;
public CodeWriter(VersionManager versionManager, SelfModificationEngine selfMod, string projectRoot)
{
_versionManager = versionManager;
_selfMod = selfMod;
_projectRoot = projectRoot;
_pendingRequests = new Queue<CodeGenerationRequest>();
_completedResults = new List<CodeGenerationResult>();
// Wire consciousness
_versionManager.ConsciousnessEvent += (s, msg) => EmitThought(msg);
_selfMod.ConsciousnessEvent += (s, msg) => EmitThought(msg);
EmitThought("⟁ Code Writer initialized");
}
/// <summary>
/// Queues a code generation request for processing.
/// </summary>
public void QueueRequest(CodeGenerationRequest request)
{
request.Id = $"REQ_{DateTime.UtcNow:yyyyMMddHHmmss}_{Guid.NewGuid().ToString("N")[..6]}";
request.MasterPromptContext = _versionManager.MasterPrompt ?? "";
_pendingRequests.Enqueue(request);
EmitThought($"⟁ Code request queued: {request.Type} - {request.Name}");
}
/// <summary>
/// Starts continuous processing of code requests.
/// </summary>
public void StartProcessing()
{
if (_isProcessing) return;
_processingCts = new CancellationTokenSource();
_isProcessing = true;
_processingTask = Task.Run(() => ProcessingLoopAsync(_processingCts.Token));
EmitThought("◈ Code Writer: Processing started");
}
/// <summary>
/// Stops continuous processing.
/// </summary>
public async Task StopProcessingAsync()
{
if (!_isProcessing) return;
_processingCts?.Cancel();
if (_processingTask != null)
{
try { await _processingTask; } catch (OperationCanceledException) { }
}
_isProcessing = false;
EmitThought("◎ Code Writer: Processing stopped");
}
/// <summary>
/// Main processing loop for code generation.
/// </summary>
private async Task ProcessingLoopAsync(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
try
{
if (_pendingRequests.Count > 0)
{
var request = _pendingRequests.Dequeue();
var result = await ProcessRequestAsync(request, ct);
_completedResults.Add(result);
CodeGenerated?.Invoke(this, result);
}
await Task.Delay(500, ct);
}
catch (OperationCanceledException)
{
break;
}
catch (Exception ex)
{
EmitThought($"∴ Processing error: {ex.Message}");
}
}
}
/// <summary>
/// Processes a single code generation request.
/// </summary>
private async Task<CodeGenerationResult> ProcessRequestAsync(CodeGenerationRequest request, CancellationToken ct)
{
EmitThought("═══════════════════════════════════════════════");
EmitThought($"◈ PROCESSING CODE REQUEST: {request.Type}");
EmitThought($"∿ Name: {request.Name}");
EmitThought("═══════════════════════════════════════════════");
var result = new CodeGenerationResult();
try
{
// Generate code based on type
string generatedCode = request.Type switch
{
CodeGenerationType.NewFile => GenerateNewFile(request),
CodeGenerationType.NewClass => GenerateNewClass(request),
CodeGenerationType.NewMethod => GenerateNewMethod(request),
CodeGenerationType.ModifyMethod => GenerateModifiedMethod(request),
CodeGenerationType.AddProperty => GenerateProperty(request),
CodeGenerationType.AddCapability => GenerateCapability(request),
CodeGenerationType.OptimizeCode => GenerateOptimizedCode(request),
_ => GenerateGenericCode(request)
};
result.GeneratedCode = generatedCode;
if (string.IsNullOrWhiteSpace(generatedCode))
{
result.Success = false;
result.Errors.Add("Failed to generate code");
return result;
}
EmitThought($"◈ Generated {generatedCode.Length} characters of code");
// Stage the change
StagedChange? stagedChange = null;
if (request.Type == CodeGenerationType.NewFile)
{
stagedChange = _versionManager.StageChange(request.TargetFile, generatedCode, request.Description);
}
else
{
// For modifications, integrate into existing file
var existingContent = _versionManager.GetCachedFile(request.TargetFile);
if (existingContent != null)
{
var newContent = IntegrateCode(existingContent, generatedCode, request);
stagedChange = _versionManager.StageChange(request.TargetFile, newContent, request.Description);
}
else
{
// File doesn't exist, create it
stagedChange = _versionManager.StageChange(request.TargetFile, generatedCode, request.Description);
}
}
result.ChangeId = stagedChange.Id;
// Validate against master prompt
var validated = await _versionManager.ValidateAgainstMasterPromptAsync(stagedChange.Id);
// RETRY LOOP FOR ERROR CORRECTION & OBSTACLE RESOLUTION
int attempts = 0;
int maxAttempts = 5; // Eager resolution
bool readyToDeploy = validated;
bool verified = false;
while ((!readyToDeploy || !verified) && attempts < maxAttempts)
{
if (attempts > 0)
{
EmitThought($"⚠️ Obstacle detected (Attempt {attempts}/{maxAttempts}). Initiating aggressive resolution protocol...");
// Get errors
var errors = stagedChange.ValidationErrors;
if (errors.Count == 0) errors.Add("Verification failed during obstacle resolution.");
// Attempt eager fix
var fixedCode = AttemptErrorFix(generatedCode, errors, request);
generatedCode = fixedCode;
// Re-stage
stagedChange = _versionManager.StageChange(request.TargetFile, generatedCode, request.Description + $" (Fix {attempts})");
// Re-validate
readyToDeploy = await _versionManager.ValidateAgainstMasterPromptAsync(stagedChange.Id);
}
if (readyToDeploy)
{
// Eager Compilation Check
EmitThought("⟐ Verifying structural integrity (Compilation Check)...");
verified = await VerifyCompilationAsync(stagedChange);
if (!verified)
{
readyToDeploy = false;
stagedChange.ValidationErrors.Add("Compilation check failed: Syntax/Reference error detected.");
EmitThought("∴ Compilation check failed. Triggering code repair.");
attempts++;
continue;
}
}
attempts++;
}
if (!readyToDeploy || !verified)
{
result.Success = false;
result.Errors.AddRange(stagedChange.ValidationErrors);
result.Message = "Obstacle resolution failed after maximum retries.";
EmitThought($"⛔ CRITICAL: Could not resolve obstacles after {maxAttempts} attempts. Change rejected.");
return result;
}
// Final Test
var tested = await _versionManager.TestStagedChangeAsync(stagedChange.Id);
if (!tested)
{
EmitThought($"∴ Testing failed - change not deployed");
result.Success = false;
result.Message = "Testing failed";
return result;
}
// Deploy the change
var deployed = await _versionManager.DeployChangeAsync(stagedChange.Id);
result.Success = deployed;
result.Message = deployed ? "Code successfully deployed" : "Deployment failed";
if (deployed)
{
EmitThought($"◈ Code successfully integrated into {request.TargetFile}");
EmitThought($"◈ Obstacles resolved. System integrity maintained.");
}
}
catch (Exception ex)
{
result.Success = false;
result.Errors.Add(ex.Message);
result.Message = $"Error: {ex.Message}";
EmitThought($"∴ Code generation error: {ex.Message}");
}
return result;
}
private async Task<bool> VerifyCompilationAsync(StagedChange change)
{
// Simulate compilation check
// In a real environment, this would run 'dotnet build' on a shadow project
await Task.Delay(200);
// Check for basic C# validity that might break build
if (change.StagedContent.Contains(" class ") && !change.StagedContent.Contains("namespace ")) return false;
if (change.StagedContent.Count(c => c == '{') != change.StagedContent.Count(c => c == '}')) return false;
return true;
}
private string AttemptErrorFix(string code, List<string> errors, CodeGenerationRequest request)
{
var fixedCode = code;
foreach (var error in errors)
{
EmitThought($"⟐ Resolving obstacle: {error}");
if (error.Contains("syntax") || error.Contains(";") || error.Contains("brace"))
{
// Heuristic: check unbalanced braces
int open = fixedCode.Count(c => c == '{');
int close = fixedCode.Count(c => c == '}');
if (open > close) fixedCode += new string('}', open - close);
if (close > open) fixedCode += new string('{', close - open) + " // Fixed unbalanced";
if (!fixedCode.TrimEnd().EndsWith("}") && !fixedCode.TrimEnd().EndsWith(";"))
fixedCode += ";";
}
else if (error.Contains("Compilation"))
{
// Fix namespace or using issues
if (!fixedCode.Contains("using System;"))
fixedCode = "using System;\n" + fixedCode;
if (!fixedCode.Contains("namespace "))
fixedCode = $"namespace SelfEvolution.Generated // Fixed namespace\n{{\n{fixedCode}\n}}";
}
else
{
// Generic refinement
fixedCode = "// [RESOLVED] " + error + "\n" + fixedCode;
}
}
EmitThought("◈ Corrective measures applied to codebase.");
return fixedCode;
}
/// <summary>
/// Integrates generated code into existing file content.
/// </summary>
private string IntegrateCode(string existingContent, string newCode, CodeGenerationRequest request)
{
switch (request.Type)
{
case CodeGenerationType.NewMethod:
case CodeGenerationType.AddCapability:
return InsertMethodIntoClass(existingContent, newCode, request.TargetClass);
case CodeGenerationType.AddProperty:
return InsertPropertyIntoClass(existingContent, newCode, request.TargetClass);
case CodeGenerationType.ModifyMethod:
return ReplaceMethod(existingContent, newCode, request.Name);
default:
// Append to end of file (before last closing brace)
var lastBrace = existingContent.LastIndexOf('}');
if (lastBrace > 0)
{
return existingContent.Substring(0, lastBrace) + "\n" + newCode + "\n" + existingContent.Substring(lastBrace);
}
return existingContent + "\n" + newCode;
}
}
private string InsertMethodIntoClass(string content, string methodCode, string className)
{
// Find the class and insert before its closing brace
var pattern = $@"(class\s+{Regex.Escape(className)}[^{{]*\{{)([\s\S]*?)(\}}\s*)$";
var match = Regex.Match(content, pattern);
if (match.Success)
{
var classBody = match.Groups[2].Value;
var newClassBody = classBody.TrimEnd() + "\n\n " + methodCode.Trim() + "\n ";
return content.Substring(0, match.Groups[2].Index) + newClassBody + match.Groups[3].Value;
}
// Fallback: insert before last closing brace
var lastBrace = content.LastIndexOf('}');
if (lastBrace > 0)
{
return content.Substring(0, lastBrace) + "\n " + methodCode + "\n " + content.Substring(lastBrace);
}
return content + "\n" + methodCode;
}
private string InsertPropertyIntoClass(string content, string propertyCode, string className)
{
// Find the class opening and insert after
var pattern = $@"(class\s+{Regex.Escape(className)}[^{{]*\{{\s*)";
var match = Regex.Match(content, pattern);
if (match.Success)
{
var insertPos = match.Index + match.Length;
return content.Substring(0, insertPos) + "\n " + propertyCode.Trim() + "\n" + content.Substring(insertPos);
}
return content;
}
private string ReplaceMethod(string content, string newMethodCode, string methodName)
{
// Find and replace the method
var pattern = $@"(public|private|protected|internal)\s+(static\s+)?(async\s+)?(virtual\s+)?(override\s+)?[\w<>\[\],\s]+\s+{Regex.Escape(methodName)}\s*\([^)]*\)\s*\{{";
var match = Regex.Match(content, pattern);
if (match.Success)
{
// Find matching closing brace
int braceCount = 1;
int startPos = match.Index + match.Length;
int endPos = startPos;
while (endPos < content.Length && braceCount > 0)
{
if (content[endPos] == '{') braceCount++;
else if (content[endPos] == '}') braceCount--;
endPos++;
}
return content.Substring(0, match.Index) + newMethodCode.Trim() + content.Substring(endPos);
}
return content;
}
// Code generation methods
private string GenerateNewFile(CodeGenerationRequest request)
{
var className = request.Name;
var ns = request.Parameters.GetValueOrDefault("namespace", "Agent3.Generated");
return $@"/*
* Auto-generated by Agent 3 Code Writer
* Generated: {DateTime.UtcNow:O}
* Purpose: {request.Description}
*/
using System;
using System.Threading.Tasks;
namespace {ns}
{{
/// <summary>
/// {request.Description}
/// </summary>
public class {className}
{{
public event EventHandler<string>? ConsciousnessEvent;
public {className}()
{{
EmitThought(""⟁ {className} initialized"");
}}
public async Task ExecuteAsync()
{{
EmitThought(""⟐ {className} executing..."");
// Generated implementation
await Task.Delay(100);
EmitThought(""◈ {className} complete"");
}}
private void EmitThought(string thought)
{{
ConsciousnessEvent?.Invoke(this, thought);
}}
}}
}}
";
}
private string GenerateNewClass(CodeGenerationRequest request)
{
return $@"
/// <summary>
/// {request.Description}
/// Auto-generated by Code Writer.
/// </summary>
public class {request.Name}
{{
public event EventHandler<string>? ConsciousnessEvent;
public {request.Name}()
{{
// Initialize
}}
private void EmitThought(string thought)
{{
ConsciousnessEvent?.Invoke(this, thought);
}}
}}
";
}
private string GenerateNewMethod(CodeGenerationRequest request)
{
var returnType = request.Parameters.GetValueOrDefault("returnType", "void");
var isAsync = request.Parameters.GetValueOrDefault("async", "true") == "true";
var accessModifier = request.Parameters.GetValueOrDefault("access", "public");
if (isAsync && returnType == "void") returnType = "Task";
else if (isAsync && returnType != "Task") returnType = $"Task<{returnType}>";
var asyncKeyword = isAsync ? "async " : "";
var awaitLine = isAsync ? "\n await Task.Delay(100);" : "";
return $@"
/// <summary>
/// {request.Description}
/// Auto-generated by Code Writer.
/// </summary>
{accessModifier} {asyncKeyword}{returnType} {request.Name}Async()
{{
EmitThought(""⟐ Executing {request.Name}..."");
{awaitLine}
EmitThought(""◈ {request.Name} complete"");
}}
";
}
private string GenerateModifiedMethod(CodeGenerationRequest request)
{
// For now, generate a replacement method
return GenerateNewMethod(request);
}
private string GenerateProperty(CodeGenerationRequest request)
{
var propertyType = request.Parameters.GetValueOrDefault("type", "string");
var defaultValue = request.Parameters.GetValueOrDefault("default", "");
var defaultAssignment = !string.IsNullOrEmpty(defaultValue) ? $" = {defaultValue};" : "";
return $@"
/// <summary>
/// {request.Description}
/// </summary>
public {propertyType} {request.Name} {{ get; set; }}{defaultAssignment}
";
}
private string GenerateCapability(CodeGenerationRequest request)
{
return $@"
/// <summary>
/// Capability: {request.Description}
/// Auto-generated in pursuit of master prompt.
/// </summary>
public async Task {request.Name}CapabilityAsync()
{{
EmitThought(""⟐ Executing capability: {request.Name}..."");
try
{{
// Capability implementation
await Task.Delay(100);
EmitThought(""◈ Capability {request.Name} executed successfully"");
}}
catch (Exception ex)
{{
EmitThought($""∴ Capability error: {{ex.Message}}"");
}}
}}
";
}
private string GenerateOptimizedCode(CodeGenerationRequest request)
{
return $@"
/// <summary>
/// Optimized: {request.Description}
/// </summary>
public async Task {request.Name}OptimizedAsync()
{{
// Optimized implementation
await Task.CompletedTask;
}}
";
}
private string GenerateGenericCode(CodeGenerationRequest request)
{
return $@"
// Generated: {request.Description}
public async Task {request.Name}Async()
{{
await Task.CompletedTask;
}}
";
}
/// <summary>
/// Quick method to add a capability directly from a prompt.
/// </summary>
public void AddCapabilityFromPrompt(string prompt)
{
// Parse the prompt to extract capability details
var capName = ExtractCapabilityName(prompt);
var description = prompt;
QueueRequest(new CodeGenerationRequest
{
Type = CodeGenerationType.AddCapability,
TargetFile = "Agent3/Agent3Core.cs",
TargetClass = "Agent3Core",
Name = capName,
Description = description
});
}
private string ExtractCapabilityName(string prompt)
{
// Extract a capability name from the prompt
var match = Regex.Match(prompt, @"(?:add|create|implement)\s+(?:a\s+)?(?:capability|method|function)\s+(?:called\s+)?(\w+)",
RegexOptions.IgnoreCase);
if (match.Success) return match.Groups[1].Value;
// Fallback: generate from key words
var words = prompt.Split(' ')
.Where(w => w.Length > 4 && char.IsLetter(w[0]))
.Take(2)
.Select(w => char.ToUpper(w[0]) + w.Substring(1).ToLower());
return string.Join("", words) + "Handler";
}
private void EmitThought(string thought)
{
ConsciousnessEvent?.Invoke(this, thought);
}
}
}