|
| 1 | +//! ═══════════════════════════════════════════════════════════════════════════════ |
| 2 | +//! VIBEE v10.5: Auto-Curation & Self-Feeding v2 |
| 3 | +//! ═══════════════════════════════════════════════════════════════════════════════ |
| 4 | +//! |
| 5 | +//! Auto-curation system for synthetic seeds with multi-stage validation: |
| 6 | +//! 1. Syntax validation - compile checks |
| 7 | +//! 2. Semantic validation - behavior matching |
| 8 | +//! 3. Pattern validation - anti-pattern detection |
| 9 | +//! 4. Self-feeding - high-quality seeds fed back to Golden DB |
| 10 | +//! |
| 11 | +//! φ² + 1/φ² = 3 |
| 12 | +//! |
| 13 | +//! ═══════════════════════════════════════════════════════════════════════════════ |
| 14 | + |
| 15 | +const std = @import("std"); |
| 16 | +const Allocator = std.mem.Allocator; |
| 17 | + |
| 18 | +const golden_db = @import("golden_db.zig"); |
| 19 | +const synthetic_seed_gen = @import("synthetic_seed_gen.zig"); |
| 20 | +const vibe_rewards = @import("vibe_rewards.zig"); |
| 21 | + |
| 22 | +/// Validation stage result |
| 23 | +pub const ValidationStage = enum { |
| 24 | + syntax_check, |
| 25 | + semantic_check, |
| 26 | + pattern_check, |
| 27 | + approved, |
| 28 | + rejected, |
| 29 | +}; |
| 30 | + |
| 31 | +/// Detailed validation result |
| 32 | +pub const ValidationResult = struct { |
| 33 | + stage: ValidationStage, |
| 34 | + score: f32, |
| 35 | + tri_reward: f64, |
| 36 | + |
| 37 | + pub fn isApproved(self: *const ValidationResult) bool { |
| 38 | + return self.stage == .approved; |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +/// Curation statistics |
| 43 | +pub const CurationStats = struct { |
| 44 | + total_processed: usize = 0, |
| 45 | + syntax_passed: usize = 0, |
| 46 | + semantic_passed: usize = 0, |
| 47 | + pattern_passed: usize = 0, |
| 48 | + approved: usize = 0, |
| 49 | + total_tri_earned: f64 = 0, |
| 50 | +}; |
| 51 | + |
| 52 | +/// Auto-Curator v2 - multi-stage validation for synthetic seeds |
| 53 | +pub const AutoCuratorV2 = struct { |
| 54 | + allocator: Allocator, |
| 55 | + golden_db: *golden_db.GoldenDB, |
| 56 | + agent_id: []const u8, |
| 57 | + |
| 58 | + const Self = @This(); |
| 59 | + |
| 60 | + pub fn init(allocator: Allocator, db: *golden_db.GoldenDB, agent_id: []const u8) !Self { |
| 61 | + return Self{ |
| 62 | + .allocator = allocator, |
| 63 | + .golden_db = db, |
| 64 | + .agent_id = agent_id, |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + /// Validate a synthetic seed through all stages |
| 69 | + pub fn validateSeed( |
| 70 | + self: *const Self, |
| 71 | + seed: *const synthetic_seed_gen.GeneratedSeed, |
| 72 | + ) !ValidationResult { |
| 73 | + var score: f32 = 1.0; |
| 74 | + var stage = ValidationStage.syntax_check; |
| 75 | + |
| 76 | + // Stage 1: Syntax validation |
| 77 | + if (!self.validateSyntax(seed)) { |
| 78 | + score = 0.0; |
| 79 | + stage = ValidationStage.rejected; |
| 80 | + } else { |
| 81 | + stage = ValidationStage.semantic_check; |
| 82 | + } |
| 83 | + |
| 84 | + // Stage 2: Semantic validation (only if syntax passed) |
| 85 | + if (stage == ValidationStage.semantic_check) { |
| 86 | + const semantic_score = self.validateSemantic(seed); |
| 87 | + if (semantic_score < 0.5) { |
| 88 | + score *= semantic_score; |
| 89 | + } else { |
| 90 | + score = @min(score, semantic_score); |
| 91 | + stage = ValidationStage.pattern_check; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Stage 3: Pattern validation (only if semantic passed) |
| 96 | + if (stage == ValidationStage.pattern_check) { |
| 97 | + var pattern_issues: usize = 0; |
| 98 | + |
| 99 | + // Check for common anti-patterns |
| 100 | + if (std.mem.indexOf(u8, seed.body, "TODO") != null) { |
| 101 | + pattern_issues += 1; |
| 102 | + } |
| 103 | + if (std.mem.indexOf(u8, seed.body, "FIXME") != null) { |
| 104 | + pattern_issues += 1; |
| 105 | + } |
| 106 | + if (std.mem.indexOf(u8, seed.body, "@panic") != null) { |
| 107 | + pattern_issues += 2; |
| 108 | + } |
| 109 | + |
| 110 | + // Penalty for anti-patterns |
| 111 | + if (pattern_issues > 0) { |
| 112 | + score *= @max(0.1, 1.0 - @as(f32, @floatFromInt(pattern_issues)) * 0.1); |
| 113 | + } |
| 114 | + |
| 115 | + if (score >= 0.7) { |
| 116 | + stage = ValidationStage.approved; |
| 117 | + } else { |
| 118 | + stage = ValidationStage.rejected; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Calculate $TRI reward |
| 123 | + const tri_reward = if (stage == .approved) |
| 124 | + vibe_rewards.VibeRewardSystem.rewardForImprovement(score, 5) |
| 125 | + else |
| 126 | + 0; |
| 127 | + |
| 128 | + return ValidationResult{ |
| 129 | + .stage = stage, |
| 130 | + .score = score, |
| 131 | + .tri_reward = tri_reward, |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + /// Validate syntax of generated code |
| 136 | + fn validateSyntax(self: *const Self, seed: *const synthetic_seed_gen.GeneratedSeed) bool { |
| 137 | + _ = self; |
| 138 | + |
| 139 | + // For Golden DB seeds, be more lenient - just check basic structure |
| 140 | + // 1. Check for balanced braces |
| 141 | + var open_braces: i32 = 0; |
| 142 | + var open_parens: i32 = 0; |
| 143 | + |
| 144 | + for (seed.signature) |c| { |
| 145 | + if (c == '{') open_braces += 1; |
| 146 | + if (c == '}') open_braces -= 1; |
| 147 | + if (c == '(') open_parens += 1; |
| 148 | + if (c == ')') open_parens -= 1; |
| 149 | + } |
| 150 | + |
| 151 | + for (seed.body) |c| { |
| 152 | + if (c == '{') open_braces += 1; |
| 153 | + if (c == '}') open_braces -= 1; |
| 154 | + if (c == '(') open_parens += 1; |
| 155 | + if (c == ')') open_parens -= 1; |
| 156 | + } |
| 157 | + |
| 158 | + if (open_braces != 0 or open_parens != 0) return false; |
| 159 | + |
| 160 | + // 2. Check for non-empty implementation |
| 161 | + if (seed.body.len < 5) return false; |
| 162 | + |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + /// Validate semantic correctness |
| 167 | + fn validateSemantic(self: *const Self, seed: *const synthetic_seed_gen.GeneratedSeed) f32 { |
| 168 | + _ = self; |
| 169 | + var score: f32 = 1.0; |
| 170 | + |
| 171 | + // Check for appropriate return types |
| 172 | + if (std.mem.indexOf(u8, seed.signature, "!void") != null or |
| 173 | + std.mem.indexOf(u8, seed.signature, "!") != null) |
| 174 | + { |
| 175 | + score += 0.1; |
| 176 | + } |
| 177 | + |
| 178 | + // Check for non-empty body |
| 179 | + if (seed.body.len > 50) score += 0.1; |
| 180 | + |
| 181 | + // Deduct for obvious placeholders |
| 182 | + if (std.mem.indexOf(u8, seed.body, "TODO") != null) score -= 0.2; |
| 183 | + if (std.mem.indexOf(u8, seed.body, "@panic") != null) score -= 0.3; |
| 184 | + if (std.mem.indexOf(u8, seed.body, "unreachable") != null) score -= 0.2; |
| 185 | + |
| 186 | + return @max(0.0, @min(1.0, score)); |
| 187 | + } |
| 188 | +}; |
| 189 | + |
| 190 | +/// Self-Feeding Loop v2 - auto-import high-quality seeds to Golden DB |
| 191 | +pub const SelfFeedingLoopV2 = struct { |
| 192 | + allocator: Allocator, |
| 193 | + golden_db: *golden_db.GoldenDB, |
| 194 | + curator: AutoCuratorV2, |
| 195 | + |
| 196 | + const Self = @This(); |
| 197 | + |
| 198 | + pub fn init(allocator: Allocator, db: *golden_db.GoldenDB, agent_id: []const u8) !Self { |
| 199 | + const curator = try AutoCuratorV2.init(allocator, db, agent_id); |
| 200 | + return Self{ |
| 201 | + .allocator = allocator, |
| 202 | + .golden_db = db, |
| 203 | + .curator = curator, |
| 204 | + }; |
| 205 | + } |
| 206 | + |
| 207 | + /// Process generated seeds and auto-feed approved ones to Golden DB |
| 208 | + pub fn processAndFeed( |
| 209 | + self: *Self, |
| 210 | + seeds: []const synthetic_seed_gen.GeneratedSeed, |
| 211 | + ) !CurationStats { |
| 212 | + var stats = CurationStats{}; |
| 213 | + stats.total_processed = seeds.len; |
| 214 | + |
| 215 | + for (seeds) |*seed| { |
| 216 | + const result = try self.curator.validateSeed(seed); |
| 217 | + |
| 218 | + stats.total_tri_earned += result.tri_reward; |
| 219 | + |
| 220 | + // Track stages reached |
| 221 | + if (result.stage == ValidationStage.syntax_check or |
| 222 | + result.stage == ValidationStage.semantic_check or |
| 223 | + result.stage == ValidationStage.pattern_check or |
| 224 | + result.stage == ValidationStage.approved) |
| 225 | + { |
| 226 | + stats.syntax_passed += 1; |
| 227 | + } |
| 228 | + if (result.stage == ValidationStage.semantic_check or |
| 229 | + result.stage == ValidationStage.pattern_check or |
| 230 | + result.stage == ValidationStage.approved) |
| 231 | + { |
| 232 | + stats.semantic_passed += 1; |
| 233 | + } |
| 234 | + if (result.stage == ValidationStage.pattern_check or |
| 235 | + result.stage == ValidationStage.approved) |
| 236 | + { |
| 237 | + stats.pattern_passed += 1; |
| 238 | + } |
| 239 | + if (result.isApproved()) { |
| 240 | + stats.approved += 1; |
| 241 | + |
| 242 | + // Add to Golden DB |
| 243 | + self.golden_db.addNewSeed( |
| 244 | + seed.name, |
| 245 | + seed.signature, |
| 246 | + seed.body, |
| 247 | + seed.category, |
| 248 | + ) catch |err| { |
| 249 | + std.debug.print(" [Warning] Failed to add '{s}': {}\n", .{ seed.name, err }); |
| 250 | + continue; |
| 251 | + }; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + return stats; |
| 256 | + } |
| 257 | +}; |
| 258 | + |
| 259 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 260 | +// Tests |
| 261 | +// ═══════════════════════════════════════════════════════════════════════════════ |
| 262 | + |
| 263 | +test "AutoCuratorV2: init" { |
| 264 | + var db = try golden_db.GoldenDB.init(std.testing.allocator); |
| 265 | + defer db.deinit(); |
| 266 | + |
| 267 | + const curator = try AutoCuratorV2.init(std.testing.allocator, &db, "test-agent"); |
| 268 | + _ = curator; |
| 269 | +} |
| 270 | + |
| 271 | +test "AutoCuratorV2: validateSyntax - valid" { |
| 272 | + var db = try golden_db.GoldenDB.init(std.testing.allocator); |
| 273 | + defer db.deinit(); |
| 274 | + |
| 275 | + const curator = try AutoCuratorV2.init(std.testing.allocator, &db, "test-agent"); |
| 276 | + |
| 277 | + const seed = synthetic_seed_gen.GeneratedSeed{ |
| 278 | + .name = "test_func", |
| 279 | + .signature = "pub fn testFunc() !void", |
| 280 | + .body = "const x = 42;", |
| 281 | + .category = .generic, |
| 282 | + .quality_score = 0.9, |
| 283 | + .synthesis_method = .template, |
| 284 | + }; |
| 285 | + |
| 286 | + const result = curator.validateSyntax(&seed); |
| 287 | + try std.testing.expect(result == true); |
| 288 | +} |
| 289 | + |
| 290 | +test "SelfFeedingLoopV2: init" { |
| 291 | + var db = try golden_db.GoldenDB.init(std.testing.allocator); |
| 292 | + defer db.deinit(); |
| 293 | + |
| 294 | + const loop = try SelfFeedingLoopV2.init(std.testing.allocator, &db, "test-agent"); |
| 295 | + _ = loop; |
| 296 | +} |
0 commit comments