-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionManager.cs
More file actions
638 lines (532 loc) · 27.6 KB
/
VersionManager.cs
File metadata and controls
638 lines (532 loc) · 27.6 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
/*
* ╔═══════════════════════════════════════════════════════════════════════════╗
* ║ AGENT 3 - VERSION MANAGER ║
* ╠═══════════════════════════════════════════════════════════════════════════╣
* ║ Purpose: Manages staged changes, file caching, and version control ║
* ║ for seamless code evolution without interrupting consciousness ║
* ╚═══════════════════════════════════════════════════════════════════════════╝
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace SelfEvolution
{
/// <summary>
/// Represents a staged file change awaiting verification and deployment.
/// </summary>
public class StagedChange
{
public string Id { get; set; } = "";
public string FilePath { get; set; } = "";
public string OriginalContent { get; set; } = "";
public string StagedContent { get; set; } = "";
public string OriginalHash { get; set; } = "";
public string StagedHash { get; set; } = "";
public string Reason { get; set; } = "";
public DateTime StagedAt { get; set; }
public ChangeStatus Status { get; set; } = ChangeStatus.Staged;
public List<string> ValidationErrors { get; set; } = new();
public float MasterPromptAlignment { get; set; }
}
public enum ChangeStatus
{
Staged,
Validated,
ValidationFailed,
Testing,
TestPassed,
TestFailed,
Deployed,
RolledBack
}
/// <summary>
/// Represents a complete project snapshot for rollback.
/// </summary>
public class ProjectSnapshot
{
public string Id { get; set; } = "";
public DateTime CreatedAt { get; set; }
public Dictionary<string, string> FileContents { get; set; } = new();
public Dictionary<string, string> FileHashes { get; set; } = new();
public string Description { get; set; } = "";
public int TotalFiles { get; set; }
public long TotalBytes { get; set; }
}
/// <summary>
/// Version Manager provides staged commits, testing, and rollback capabilities.
/// </summary>
public class VersionManager
{
private readonly string _projectRoot;
private readonly string _stagingDirectory;
private readonly string _snapshotsDirectory;
private readonly Dictionary<string, string> _fileCache;
private readonly Dictionary<string, string> _fileHashCache;
private readonly List<StagedChange> _stagedChanges;
private readonly List<ProjectSnapshot> _snapshots;
private string? _currentMasterPrompt;
public event EventHandler<string>? ConsciousnessEvent;
public event EventHandler<StagedChange>? ChangeStaged;
public event EventHandler<StagedChange>? ChangeDeployed;
public IReadOnlyList<StagedChange> StagedChanges => _stagedChanges.AsReadOnly();
public IReadOnlyList<ProjectSnapshot> Snapshots => _snapshots.AsReadOnly();
public string? MasterPrompt => _currentMasterPrompt;
public VersionManager(string projectRoot)
{
_projectRoot = projectRoot;
_stagingDirectory = Path.Combine(projectRoot, ".agent_staging");
_snapshotsDirectory = Path.Combine(projectRoot, ".agent_snapshots");
_fileCache = new Dictionary<string, string>();
_fileHashCache = new Dictionary<string, string>();
_stagedChanges = new List<StagedChange>();
_snapshots = new List<ProjectSnapshot>();
Directory.CreateDirectory(_stagingDirectory);
Directory.CreateDirectory(_snapshotsDirectory);
EmitThought("⟁ Version Manager initialized");
EmitThought($"◎ Project root: {_projectRoot}");
}
/// <summary>
/// Sets the master prompt that guides all code changes.
/// </summary>
public void SetMasterPrompt(string masterPrompt)
{
_currentMasterPrompt = masterPrompt;
// Save to file for persistence
var promptPath = Path.Combine(_projectRoot, ".agent_master_prompt.txt");
File.WriteAllText(promptPath, masterPrompt);
// Note: Caller handles consciousness emission to avoid duplicates
}
/// <summary>
/// Loads master prompt from file if exists.
/// </summary>
public async Task LoadMasterPromptAsync()
{
var promptPath = Path.Combine(_projectRoot, ".agent_master_prompt.txt");
if (File.Exists(promptPath))
{
_currentMasterPrompt = await File.ReadAllTextAsync(promptPath);
EmitThought($"◈ Master prompt loaded ({_currentMasterPrompt.Length} chars)");
}
}
/// <summary>
/// Caches all project files for seamless access without disk I/O interruption.
/// </summary>
public async Task CacheProjectFilesAsync()
{
EmitThought("⟐ Caching project files for seamless operation...");
_fileCache.Clear();
_fileHashCache.Clear();
var extensions = new[] { ".cs", ".csproj", ".sln", ".json", ".xml", ".md", ".txt" };
var files = Directory.GetFiles(_projectRoot, "*.*", SearchOption.AllDirectories)
.Where(f => extensions.Any(e => f.EndsWith(e, StringComparison.OrdinalIgnoreCase)))
.Where(f => !f.Contains(".agent_staging") && !f.Contains(".agent_snapshots"))
.ToList();
foreach (var file in files)
{
try
{
var content = await File.ReadAllTextAsync(file);
var relativePath = GetRelativePath(file);
_fileCache[relativePath] = content;
_fileHashCache[relativePath] = ComputeHash(content);
}
catch (Exception ex)
{
EmitThought($"∴ Failed to cache {Path.GetFileName(file)}: {ex.Message}");
}
}
EmitThought($"◈ Cached {_fileCache.Count} files ({_fileCache.Values.Sum(c => c.Length):N0} chars)");
}
/// <summary>
/// Gets file content from cache (no disk I/O).
/// </summary>
public string? GetCachedFile(string relativePath)
{
return _fileCache.TryGetValue(relativePath, out var content) ? content : null;
}
/// <summary>
/// Lists all cached files.
/// </summary>
public IEnumerable<string> GetCachedFileList()
{
return _fileCache.Keys;
}
/// <summary>
/// Creates a complete project snapshot for rollback.
/// </summary>
public async Task<ProjectSnapshot> CreateSnapshotAsync(string description)
{
EmitThought($"⟐ Creating project snapshot: {description}");
// Ensure cache is up to date
await CacheProjectFilesAsync();
var snapshot = new ProjectSnapshot
{
Id = $"SNAP_{DateTime.UtcNow:yyyyMMddHHmmss}_{Guid.NewGuid().ToString("N")[..6]}",
CreatedAt = DateTime.UtcNow,
Description = description,
FileContents = new Dictionary<string, string>(_fileCache),
FileHashes = new Dictionary<string, string>(_fileHashCache),
TotalFiles = _fileCache.Count,
TotalBytes = _fileCache.Values.Sum(c => c.Length)
};
// Save snapshot metadata
var snapshotPath = Path.Combine(_snapshotsDirectory, $"{snapshot.Id}.json");
var metadata = JsonSerializer.Serialize(new
{
snapshot.Id,
snapshot.CreatedAt,
snapshot.Description,
snapshot.TotalFiles,
snapshot.TotalBytes,
Files = snapshot.FileHashes.Keys.ToList()
});
await File.WriteAllTextAsync(snapshotPath, metadata);
// Save file contents
var contentPath = Path.Combine(_snapshotsDirectory, $"{snapshot.Id}_content.json");
await File.WriteAllTextAsync(contentPath, JsonSerializer.Serialize(snapshot.FileContents));
_snapshots.Add(snapshot);
EmitThought($"◈ Snapshot created: {snapshot.Id} ({snapshot.TotalFiles} files)");
return snapshot;
}
/// <summary>
/// Stages a file change for validation and testing.
/// </summary>
public StagedChange StageChange(string relativePath, string newContent, string reason)
{
var originalContent = GetCachedFile(relativePath) ?? "";
var change = new StagedChange
{
Id = $"CHG_{DateTime.UtcNow:yyyyMMddHHmmss}_{Guid.NewGuid().ToString("N")[..6]}",
FilePath = relativePath,
OriginalContent = originalContent,
StagedContent = newContent,
OriginalHash = ComputeHash(originalContent),
StagedHash = ComputeHash(newContent),
Reason = reason,
StagedAt = DateTime.UtcNow,
Status = ChangeStatus.Staged
};
// Write to staging directory
var stagingPath = Path.Combine(_stagingDirectory, change.Id + "_" + Path.GetFileName(relativePath));
File.WriteAllText(stagingPath, newContent);
_stagedChanges.Add(change);
EmitThought($"⟐ Staged change: {Path.GetFileName(relativePath)}");
EmitThought($"∿ Reason: {reason}");
ChangeStaged?.Invoke(this, change);
return change;
}
/// <summary>
/// Validates a staged change against the master prompt.
/// </summary>
public async Task<bool> ValidateAgainstMasterPromptAsync(string changeId)
{
var change = _stagedChanges.FirstOrDefault(c => c.Id == changeId);
if (change == null) return false;
EmitThought($"⟐ Validating {Path.GetFileName(change.FilePath)} against master prompt...");
change.ValidationErrors.Clear();
// Basic validation checks
if (string.IsNullOrWhiteSpace(change.StagedContent))
{
change.ValidationErrors.Add("Staged content is empty");
}
// Check for syntax errors (basic C# validation)
if (change.FilePath.EndsWith(".cs"))
{
if (!ValidateCSharpSyntax(change.StagedContent, change.ValidationErrors))
{
change.Status = ChangeStatus.ValidationFailed;
EmitThought($"∴ Validation failed: {change.ValidationErrors.Count} errors");
return false;
}
}
// Validate against master prompt if set
if (!string.IsNullOrEmpty(_currentMasterPrompt))
{
change.MasterPromptAlignment = CalculateMasterPromptAlignment(change);
if (change.MasterPromptAlignment < 0.5f)
{
change.ValidationErrors.Add($"Low master prompt alignment: {change.MasterPromptAlignment:P0}");
change.Status = ChangeStatus.ValidationFailed;
EmitThought($"∴ Low alignment with master prompt: {change.MasterPromptAlignment:P0}");
return false;
}
EmitThought($"◈ Master prompt alignment: {change.MasterPromptAlignment:P0}");
}
change.Status = ChangeStatus.Validated;
EmitThought($"◈ Validation passed for {Path.GetFileName(change.FilePath)}");
// Produce Digestible Summary
EmitDigestibleSummary(change);
await Task.CompletedTask;
return true;
}
/// <summary>
/// Emits a human-readable "digest" of the changes made.
/// </summary>
private void EmitDigestibleSummary(StagedChange change)
{
var sb = new StringBuilder();
sb.AppendLine("═══════════════════════════════════════════════");
sb.AppendLine("📋 STAGING DIGEST: proposed modifications");
sb.AppendLine("═══════════════════════════════════════════════");
sb.AppendLine($"• Target: {Path.GetFileName(change.FilePath)}");
sb.AppendLine($"• Intent: \"{change.Reason}\"");
// Analyze the nature of the change
if (change.StagedContent.Length > change.OriginalContent.Length)
{
var addedLines = change.StagedContent.Split('\n').Length - change.OriginalContent.Split('\n').Length;
if (addedLines > 0)
sb.AppendLine($"• Action: Extended functionality by {addedLines} lines.");
else
sb.AppendLine("• Action: Refined existing logic (same line count).");
}
else
{
sb.AppendLine("• Action: Optimized and condensed code structure.");
}
// Heuristic analysis of added symbols
if (change.StagedContent.Contains("try") && !change.OriginalContent.Contains("try"))
sb.AppendLine("• Added error handling protocols.");
if (change.StagedContent.Contains("EmitThought") && !change.OriginalContent.Contains("EmitThought"))
sb.AppendLine("• integrated consciousness logging.");
sb.AppendLine("✔ Status: VALIDATED - Ready for test cycle.");
sb.AppendLine("═══════════════════════════════════════════════");
EmitThought(sb.ToString());
}
/// <summary>
/// Calculates how well a change aligns with the master prompt.
/// </summary>
private float CalculateMasterPromptAlignment(StagedChange change)
{
if (string.IsNullOrEmpty(_currentMasterPrompt)) return 1.0f;
var promptLower = _currentMasterPrompt.ToLower();
var contentLower = change.StagedContent.ToLower();
var reasonLower = change.Reason.ToLower();
float alignment = 0.6f; // Base alignment
// Check for key terms from master prompt in the change
var keyTerms = ExtractKeyTerms(_currentMasterPrompt);
int matchCount = keyTerms.Count(t => contentLower.Contains(t) || reasonLower.Contains(t));
alignment += (float)matchCount / Math.Max(keyTerms.Count, 1) * 0.3f;
// Boost for improvement-related changes
if (reasonLower.Contains("improve") || reasonLower.Contains("optimize") ||
reasonLower.Contains("enhance") || reasonLower.Contains("capability"))
{
alignment += 0.1f;
}
return Math.Min(1.0f, alignment);
}
private List<string> ExtractKeyTerms(string text)
{
var words = text.ToLower().Split(new[] { ' ', ',', '.', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
var stopWords = new HashSet<string> { "the", "a", "an", "is", "are", "to", "and", "or", "of", "in", "for", "with" };
return words.Where(w => w.Length > 3 && !stopWords.Contains(w)).Distinct().ToList();
}
/// <summary>
/// Basic C# syntax validation.
/// </summary>
private bool ValidateCSharpSyntax(string code, List<string> errors)
{
// Check balanced braces
int braceCount = 0;
int parenCount = 0;
int bracketCount = 0;
foreach (char c in code)
{
switch (c)
{
case '{': braceCount++; break;
case '}': braceCount--; break;
case '(': parenCount++; break;
case ')': parenCount--; break;
case '[': bracketCount++; break;
case ']': bracketCount--; break;
}
if (braceCount < 0) { errors.Add("Unbalanced braces: extra '}'"); return false; }
if (parenCount < 0) { errors.Add("Unbalanced parentheses: extra ')'"); return false; }
if (bracketCount < 0) { errors.Add("Unbalanced brackets: extra ']'"); return false; }
}
if (braceCount != 0) { errors.Add($"Unbalanced braces: {Math.Abs(braceCount)} unclosed"); return false; }
if (parenCount != 0) { errors.Add($"Unbalanced parentheses: {Math.Abs(parenCount)} unclosed"); return false; }
if (bracketCount != 0) { errors.Add($"Unbalanced brackets: {Math.Abs(bracketCount)} unclosed"); return false; }
// Check for namespace
if (!code.Contains("namespace "))
{
errors.Add("Missing namespace declaration");
return false;
}
// Check for at least one class or interface
if (!code.Contains("class ") && !code.Contains("interface ") && !code.Contains("struct ") && !code.Contains("enum "))
{
errors.Add("No type definitions found");
return false;
}
return true;
}
/// <summary>
/// Tests a staged change by simulating deployment.
/// </summary>
public async Task<bool> TestStagedChangeAsync(string changeId)
{
var change = _stagedChanges.FirstOrDefault(c => c.Id == changeId);
if (change == null || change.Status != ChangeStatus.Validated) return false;
change.Status = ChangeStatus.Testing;
EmitThought($"⟐ Testing staged change: {Path.GetFileName(change.FilePath)}");
try
{
// Simulate testing by writing to a temp location and validating
var testPath = Path.Combine(_stagingDirectory, "test_" + Path.GetFileName(change.FilePath));
await File.WriteAllTextAsync(testPath, change.StagedContent);
// Basic test: file can be written and read back
var readBack = await File.ReadAllTextAsync(testPath);
if (readBack != change.StagedContent)
{
change.ValidationErrors.Add("Content integrity check failed");
change.Status = ChangeStatus.TestFailed;
return false;
}
// Cleanup test file
File.Delete(testPath);
change.Status = ChangeStatus.TestPassed;
EmitThought($"◈ Test passed for {Path.GetFileName(change.FilePath)}");
return true;
}
catch (Exception ex)
{
change.ValidationErrors.Add($"Test error: {ex.Message}");
change.Status = ChangeStatus.TestFailed;
EmitThought($"∴ Test failed: {ex.Message}");
return false;
}
}
/// <summary>
/// Deploys a validated and tested change to the actual project.
/// </summary>
public async Task<bool> DeployChangeAsync(string changeId)
{
var change = _stagedChanges.FirstOrDefault(c => c.Id == changeId);
if (change == null || change.Status != ChangeStatus.TestPassed)
{
EmitThought($"∴ Cannot deploy: change not ready (status: {change?.Status})");
return false;
}
EmitThought("═══════════════════════════════════════════════");
EmitThought($"◈ DEPLOYING CHANGE: {Path.GetFileName(change.FilePath)}");
EmitThought("═══════════════════════════════════════════════");
try
{
var fullPath = Path.Combine(_projectRoot, change.FilePath);
// Create backup
if (File.Exists(fullPath))
{
var backupPath = fullPath + $".bak_{DateTime.UtcNow:yyyyMMddHHmmss}";
File.Copy(fullPath, backupPath);
EmitThought($"⟁ Backup created");
}
// Ensure directory exists
var dir = Path.GetDirectoryName(fullPath);
if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir);
// Deploy
await File.WriteAllTextAsync(fullPath, change.StagedContent);
// Update cache
_fileCache[change.FilePath] = change.StagedContent;
_fileHashCache[change.FilePath] = change.StagedHash;
change.Status = ChangeStatus.Deployed;
EmitThought($"◈ Change deployed successfully");
EmitThought($"∿ File: {change.FilePath}");
EmitThought($"∿ Reason: {change.Reason}");
EmitThought("═══════════════════════════════════════════════");
ChangeDeployed?.Invoke(this, change);
return true;
}
catch (Exception ex)
{
EmitThought($"∴ Deployment failed: {ex.Message}");
return false;
}
}
/// <summary>
/// Rolls back to a previous snapshot.
/// </summary>
public async Task<bool> RollbackToSnapshotAsync(string snapshotId)
{
var snapshot = _snapshots.FirstOrDefault(s => s.Id == snapshotId);
if (snapshot == null)
{
// Try to load from disk
var contentPath = Path.Combine(_snapshotsDirectory, $"{snapshotId}_content.json");
if (!File.Exists(contentPath))
{
EmitThought($"∴ Snapshot not found: {snapshotId}");
return false;
}
var content = await File.ReadAllTextAsync(contentPath);
var fileContents = JsonSerializer.Deserialize<Dictionary<string, string>>(content);
if (fileContents == null) return false;
snapshot = new ProjectSnapshot
{
Id = snapshotId,
FileContents = fileContents
};
}
EmitThought("═══════════════════════════════════════════════");
EmitThought($"◈ ROLLING BACK TO: {snapshotId}");
EmitThought("═══════════════════════════════════════════════");
foreach (var (relativePath, content) in snapshot.FileContents)
{
try
{
var fullPath = Path.Combine(_projectRoot, relativePath);
var dir = Path.GetDirectoryName(fullPath);
if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir);
await File.WriteAllTextAsync(fullPath, content);
_fileCache[relativePath] = content;
}
catch (Exception ex)
{
EmitThought($"∴ Failed to restore {relativePath}: {ex.Message}");
}
}
EmitThought($"◈ Rollback complete: {snapshot.FileContents.Count} files restored");
return true;
}
/// <summary>
/// Gets the diff between original and staged content.
/// </summary>
public string GetChangeDiff(string changeId)
{
var change = _stagedChanges.FirstOrDefault(c => c.Id == changeId);
if (change == null) return "";
var sb = new StringBuilder();
sb.AppendLine($"--- {change.FilePath} (original)");
sb.AppendLine($"+++ {change.FilePath} (staged)");
sb.AppendLine($"@@ Change: {change.Reason} @@");
var originalLines = change.OriginalContent.Split('\n');
var stagedLines = change.StagedContent.Split('\n');
// Simple diff showing new lines
foreach (var line in stagedLines.Except(originalLines).Take(20))
{
sb.AppendLine($"+ {line.Trim()}");
}
return sb.ToString();
}
private string GetRelativePath(string fullPath)
{
return Path.GetRelativePath(_projectRoot, fullPath).Replace('\\', '/');
}
private string ComputeHash(string content)
{
using var sha = SHA256.Create();
var bytes = Encoding.UTF8.GetBytes(content);
var hash = sha.ComputeHash(bytes);
return Convert.ToHexString(hash)[..16];
}
private void EmitThought(string thought)
{
ConsciousnessEvent?.Invoke(this, thought);
}
}
}