-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathmodel_quality_test.go
More file actions
491 lines (404 loc) · 14.7 KB
/
model_quality_test.go
File metadata and controls
491 lines (404 loc) · 14.7 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
package health
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/anthropics/anthropic-sdk-go"
"github.com/steveyegge/vc/internal/ai"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestModelQuality_CruftDetector validates that Haiku performs comparably to Sonnet
// for cruft detection (vc-35 Phase 2: <5% degradation target)
func TestModelQuality_CruftDetector(t *testing.T) {
if testing.Short() {
t.Skip("Skipping model quality test in short mode (requires AI API)")
}
apiKey := os.Getenv("ANTHROPIC_API_KEY")
if apiKey == "" {
t.Skip("ANTHROPIC_API_KEY not set")
}
// Create test directory with known cruft files
tmpDir := t.TempDir()
cruftFiles := map[string]string{
"backup.bak": "old backup",
"temp.tmp": "temporary file",
"notes.old": "old notes",
".DS_Store": "macos metadata",
"file~": "editor backup",
"legitimate.go": "package main",
"important.txt": "important data",
}
for name, content := range cruftFiles {
err := os.WriteFile(filepath.Join(tmpDir, name), []byte(content), 0644)
require.NoError(t, err)
}
// Expected cruft files (ground truth)
expectedCruft := []string{"backup.bak", "temp.tmp", "notes.old", ".DS_Store", "file~"}
// Test with both models
results := make(map[string][]string) // model -> detected cruft files
for _, model := range []string{ai.ModelSonnet, ai.ModelHaiku} {
t.Run(model, func(t *testing.T) {
// Create supervisor with specific model
client := ai.NewAnthropicClient(apiKey)
supervisor := &realAISupervisor{
client: &client,
model: model,
}
// Create detector
detector, err := NewCruftDetector(tmpDir, supervisor)
require.NoError(t, err)
// Run check
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
result, err := detector.Check(ctx, CodebaseContext{})
require.NoError(t, err)
require.NotNil(t, result)
// Extract detected cruft file names from Evidence
var detectedFiles []string
for _, issue := range result.IssuesFound {
// Evidence field is already map[string]interface{}
if cruftArray, ok := issue.Evidence["cruft_to_delete"]; ok {
// Try as []cruftFileAction first
if typedArray, ok := cruftArray.([]cruftFileAction); ok {
for _, cruft := range typedArray {
detectedFiles = append(detectedFiles, filepath.Base(cruft.File))
}
} else if interfaceArray, ok := cruftArray.([]interface{}); ok {
// Fallback to []interface{} for marshaled JSON
for _, item := range interfaceArray {
if cruftFile, ok := item.(map[string]interface{}); ok {
if file, ok := cruftFile["file"].(string); ok {
detectedFiles = append(detectedFiles, filepath.Base(file))
}
}
}
}
}
}
results[model] = detectedFiles
t.Logf("%s detected %d cruft files: %v", model, len(detectedFiles), detectedFiles)
// Verify some cruft was detected
assert.NotEmpty(t, detectedFiles, "Model should detect some cruft files")
})
}
// Compare results
sonnetFiles := results[ai.ModelSonnet]
haikuFiles := results[ai.ModelHaiku]
// Calculate agreement metrics
agreement := calculateSetAgreement(sonnetFiles, haikuFiles)
t.Logf("Model agreement: %.2f%% (Sonnet: %d files, Haiku: %d files)",
agreement*100, len(sonnetFiles), len(haikuFiles))
// Quality criteria: <5% degradation means >95% agreement
// We're lenient here - just checking for reasonable overlap (>80%)
assert.Greater(t, agreement, 0.80,
"Haiku should have >80%% agreement with Sonnet on cruft detection")
// Check that both detected at least some of the expected cruft
sonnetRecall := calculateRecall(expectedCruft, sonnetFiles)
haikuRecall := calculateRecall(expectedCruft, haikuFiles)
t.Logf("Sonnet recall: %.2f%%, Haiku recall: %.2f%%",
sonnetRecall*100, haikuRecall*100)
// Haiku should have similar recall (within 10 percentage points)
recallDiff := absFloat(sonnetRecall - haikuRecall)
assert.Less(t, recallDiff, 0.15,
"Haiku recall should be within 15%% of Sonnet recall")
}
// TestModelQuality_FileSizeMonitor validates Haiku performance on file size evaluation
func TestModelQuality_FileSizeMonitor(t *testing.T) {
if testing.Short() {
t.Skip("Skipping model quality test in short mode (requires AI API)")
}
apiKey := os.Getenv("ANTHROPIC_API_KEY")
if apiKey == "" {
t.Skip("ANTHROPIC_API_KEY not set")
}
// Create test directory with files of various sizes
tmpDir := t.TempDir()
// Create a legitimately large file (e.g., test fixture)
largeContent := strings.Repeat("legitimate test data\n", 50000) // ~1MB
err := os.WriteFile(filepath.Join(tmpDir, "large_test_fixture.txt"), []byte(largeContent), 0644)
require.NoError(t, err)
// Create a suspicious large file (generated output)
generatedContent := strings.Repeat("generated output line\n", 30000) // ~600KB
err = os.WriteFile(filepath.Join(tmpDir, "generated_output.log"), []byte(generatedContent), 0644)
require.NoError(t, err)
// Create normal files
err = os.WriteFile(filepath.Join(tmpDir, "small.txt"), []byte("small file"), 0644)
require.NoError(t, err)
// Test with both models
results := make(map[string]int) // model -> number of issues found
for _, model := range []string{ai.ModelSonnet, ai.ModelHaiku} {
t.Run(model, func(t *testing.T) {
client := ai.NewAnthropicClient(apiKey)
supervisor := &realAISupervisor{
client: &client,
model: model,
}
monitor, err := NewFileSizeMonitor(tmpDir, supervisor)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
result, err := monitor.Check(ctx, CodebaseContext{})
require.NoError(t, err)
require.NotNil(t, result)
issueCount := len(result.IssuesFound)
results[model] = issueCount
t.Logf("%s found %d large file issues", model, issueCount)
// Log details for debugging
for _, issue := range result.IssuesFound {
t.Logf(" - %s: %s", filepath.Base(issue.FilePath), issue.Description)
}
})
}
// Compare results - both should find similar number of issues
sonnetIssues := results[ai.ModelSonnet]
haikuIssues := results[ai.ModelHaiku]
// Allow for some variance - within 1 issue or 50% difference (whichever is larger)
maxDiff := max(1, sonnetIssues/2)
actualDiff := abs(sonnetIssues - haikuIssues)
assert.LessOrEqual(t, actualDiff, maxDiff,
"Haiku should find similar number of issues as Sonnet (Sonnet: %d, Haiku: %d)",
sonnetIssues, haikuIssues)
}
// TestModelQuality_GitignoreDetector validates Haiku performance on gitignore recommendations
func TestModelQuality_GitignoreDetector(t *testing.T) {
if testing.Short() {
t.Skip("Skipping model quality test in short mode (requires AI API)")
}
apiKey := os.Getenv("ANTHROPIC_API_KEY")
if apiKey == "" {
t.Skip("ANTHROPIC_API_KEY not set")
}
// Create test directory with files that should be gitignored
tmpDir := t.TempDir()
// Initialize git repository (required for GitignoreDetector)
initCmd := exec.Command("git", "init")
initCmd.Dir = tmpDir
err := initCmd.Run()
require.NoError(t, err, "Failed to initialize git repository")
// Configure git user for the test repo
configNameCmd := exec.Command("git", "config", "user.name", "Test User")
configNameCmd.Dir = tmpDir
_ = configNameCmd.Run()
configEmailCmd := exec.Command("git", "config", "user.email", "test@example.com")
configEmailCmd.Dir = tmpDir
_ = configEmailCmd.Run()
// Create .gitignore with minimal content
gitignoreContent := `# Minimal gitignore
*.log
`
err = os.WriteFile(filepath.Join(tmpDir, ".gitignore"), []byte(gitignoreContent), 0644)
require.NoError(t, err)
// Create files that should be ignored but aren't
// Use a larger, more diverse dataset to reduce variance
testFiles := map[string]string{
// Secrets (high priority - both models should agree)
"secrets.env": "API_KEY=secret123",
".env.local": "DB_PASSWORD=secret",
"credentials.json": `{"api_key": "secret"}`,
"server.pem": "-----BEGIN CERTIFICATE-----",
// Build artifacts
"build/output.o": "binary data",
"dist/bundle.js": "// compiled",
"target/release/app": "binary",
// Dependencies
"node_modules/pkg/index.js": "// dependency",
"node_modules/lodash/index.js": "// dependency",
"vendor/lib/file.go": "package lib",
// Editor files
".vscode/settings.json": `{"editor.fontSize": 14}`,
".idea/workspace.xml": "<project/>",
"file.swp": "vim swap",
// OS files
".DS_Store": "macos metadata",
"Thumbs.db": "windows thumbnail",
// Legitimate files (should NOT be ignored)
"main.go": "package main",
"README.md": "# Project",
"config.example.yml": "# Example config",
".env.example": "# Example environment variables",
}
for path, content := range testFiles {
fullPath := filepath.Join(tmpDir, path)
dir := filepath.Dir(fullPath)
if dir != tmpDir {
err := os.MkdirAll(dir, 0755)
require.NoError(t, err)
}
err := os.WriteFile(fullPath, []byte(content), 0644)
require.NoError(t, err)
}
// Add files to git tracking (so GitignoreDetector can detect them)
addCmd := exec.Command("git", "add", ".")
addCmd.Dir = tmpDir
err = addCmd.Run()
require.NoError(t, err, "Failed to add files to git")
// Test with both models
results := make(map[string][]string) // model -> recommended patterns
for _, model := range []string{ai.ModelSonnet, ai.ModelHaiku} {
t.Run(model, func(t *testing.T) {
client := ai.NewAnthropicClient(apiKey)
supervisor := &realAISupervisor{
client: &client,
model: model,
}
detector, err := NewGitignoreDetector(tmpDir, supervisor)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
result, err := detector.Check(ctx, CodebaseContext{})
require.NoError(t, err)
require.NotNil(t, result)
// Extract recommended patterns from Evidence field
var patterns []string
for _, issue := range result.IssuesFound {
// Evidence field contains patterns_to_add array
if patternsData, ok := issue.Evidence["patterns_to_add"]; ok {
if patternArray, ok := patternsData.([]string); ok {
patterns = append(patterns, patternArray...)
} else if interfaceArray, ok := patternsData.([]interface{}); ok {
for _, p := range interfaceArray {
if pattern, ok := p.(string); ok {
patterns = append(patterns, pattern)
}
}
}
}
}
results[model] = patterns
t.Logf("%s recommended %d patterns: %v", model, len(patterns), patterns)
// Also log if no issues were found at all
if len(result.IssuesFound) == 0 {
t.Logf("%s found no issues - detector may not have detected violations", model)
}
})
}
// Compare results
sonnetPatterns := results[ai.ModelSonnet]
haikuPatterns := results[ai.ModelHaiku]
// If neither model found violations, skip the test (detector threshold not met)
if len(sonnetPatterns) == 0 && len(haikuPatterns) == 0 {
t.Skip("No gitignore violations detected by either model - detector threshold may not have been met")
}
// Both should recommend some patterns (if we got this far)
assert.NotEmpty(t, sonnetPatterns, "Sonnet should recommend some patterns")
assert.NotEmpty(t, haikuPatterns, "Haiku should recommend some patterns")
// Calculate agreement using Jaccard similarity
agreement := calculateSetAgreement(sonnetPatterns, haikuPatterns)
t.Logf("Pattern agreement (Jaccard): %.2f%% (Sonnet: %d, Haiku: %d)",
agreement*100, len(sonnetPatterns), len(haikuPatterns))
// Also calculate how many of Sonnet's patterns Haiku includes (recall)
recall := calculateRecall(sonnetPatterns, haikuPatterns)
t.Logf("Haiku recall of Sonnet patterns: %.2f%%", recall*100)
// Quality criteria (accounting for AI non-determinism):
// - Either ≥65% Jaccard agreement (both models suggest similar patterns)
// - OR ≥75% recall (Haiku includes most of Sonnet's patterns, even if it suggests more)
//
// These thresholds are lower than ideal (70%/80%) to account for legitimate AI variance
// while still ensuring Haiku captures most core violations Sonnet identifies.
// If agreement/recall drops below these levels consistently, investigate model quality.
passesAgreementThreshold := agreement >= 0.65
passesRecallThreshold := recall >= 0.75
assert.True(t, passesAgreementThreshold || passesRecallThreshold,
"Haiku should have either ≥65%% Jaccard agreement OR ≥75%% recall of Sonnet patterns "+
"(actual Jaccard: %.2f%%, recall: %.2f%%)", agreement*100, recall*100)
}
// Helper: realAISupervisor implements AISupervisor for testing
type realAISupervisor struct {
client *anthropic.Client
model string
}
func (s *realAISupervisor) CallAI(ctx context.Context, prompt string, operation string, model string, maxTokens int) (string, error) {
// Use the supervisor's model (ignore the passed-in model)
actualModel := s.model
resp, err := s.client.Messages.New(ctx, anthropic.MessageNewParams{
Model: anthropic.Model(actualModel),
MaxTokens: int64(maxTokens),
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock(prompt)),
},
})
if err != nil {
return "", fmt.Errorf("AI API call failed: %w", err)
}
var result strings.Builder
for _, block := range resp.Content {
if block.Type == "text" {
result.WriteString(block.Text)
}
}
return result.String(), nil
}
// Helper: calculateSetAgreement returns Jaccard similarity (intersection / union)
func calculateSetAgreement(set1, set2 []string) float64 {
if len(set1) == 0 && len(set2) == 0 {
return 1.0 // Both empty = perfect agreement
}
s1 := make(map[string]bool)
for _, item := range set1 {
s1[item] = true
}
s2 := make(map[string]bool)
for _, item := range set2 {
s2[item] = true
}
// Calculate intersection
intersection := 0
for item := range s1 {
if s2[item] {
intersection++
}
}
// Calculate union
union := len(s1)
for item := range s2 {
if !s1[item] {
union++
}
}
if union == 0 {
return 1.0
}
return float64(intersection) / float64(union)
}
// Helper: calculateRecall returns recall (true positives / actual positives)
func calculateRecall(expected, detected []string) float64 {
if len(expected) == 0 {
return 1.0
}
expectedSet := make(map[string]bool)
for _, item := range expected {
expectedSet[item] = true
}
truePositives := 0
for _, item := range detected {
if expectedSet[item] {
truePositives++
}
}
return float64(truePositives) / float64(len(expected))
}
// Helper functions
func abs(a int) int {
if a < 0 {
return -a
}
return a
}
func absFloat(a float64) float64 {
if a < 0 {
return -a
}
return a
}
func max(a, b int) int {
if a > b {
return a
}
return b
}