refactor(ax): round 5 AX sweep — single-letter vars, missing Ugly test
Some checks failed
Security Scan / security (push) Successful in 10s
Test / test (push) Failing after 37s

- ai/metrics.go: ev → event in Summary(), e → entry in sortedMap()
- cmd/metrics/cmd_test.go: tc → testCase, add TestCmd_ParseDuration_Ugly
  covering boundary inputs (leading whitespace, trailing whitespace,
  float value, wrong case unit, very large count, alpha-only numeric)
- cmd/security/cmd_jobs.go: f → finding in buildJobIssueBody loops
- cmd/embed-bench/main.go: m → registeredModel, v → value, q → queryCase,
  r → rankedResult across modelAvailable(), avg(), and main() query loops

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Snider 2026-03-31 07:26:39 +01:00
parent 035a5b7e53
commit eaf26d3f93
4 changed files with 62 additions and 31 deletions

View file

@ -156,13 +156,13 @@ func Summary(events []Event) map[string]any {
byRepo := make(map[string]int)
byAgent := make(map[string]int)
for _, ev := range events {
byType[ev.Type]++
if ev.Repo != "" {
byRepo[ev.Repo]++
for _, event := range events {
byType[event.Type]++
if event.Repo != "" {
byRepo[event.Repo]++
}
if ev.AgentID != "" {
byAgent[ev.AgentID]++
if event.AgentID != "" {
byAgent[event.AgentID]++
}
}
@ -192,8 +192,8 @@ func sortedMap(m map[string]int) []map[string]any {
})
result := make([]map[string]any, len(entries))
for i, e := range entries {
result[i] = map[string]any{"key": e.key, "count": e.count}
for i, entry := range entries {
result[i] = map[string]any{"key": entry.key, "count": entry.count}
}
return result
}

View file

@ -154,8 +154,8 @@ func main() {
// 3. Query recall accuracy
fmt.Printf("\n Query recall (top-1 accuracy):\n")
correct := 0
for _, q := range queries {
qVec, err := embed(model, q.query)
for _, queryCase := range queries {
queryVec, err := embed(model, queryCase.query)
if err != nil {
fmt.Printf(" ERROR: %v\n", err)
continue
@ -165,7 +165,7 @@ func main() {
bestIdx := 0
bestSim := -1.0
for i, mv := range memVectors {
sim := cosine(qVec, mv)
sim := cosine(queryVec, mv)
if sim > bestSim {
bestSim = sim
bestIdx = i
@ -173,7 +173,7 @@ func main() {
}
matchTopic := allTopics[bestIdx]
hit := matchTopic == q.targetTopic
hit := matchTopic == queryCase.targetTopic
if hit {
correct++
}
@ -181,7 +181,7 @@ func main() {
if !hit {
marker = "✗"
}
fmt.Printf(" %s %.4f %q → %s (want: %s)\n", marker, bestSim, truncate(q.query, 40), matchTopic, q.targetTopic)
fmt.Printf(" %s %.4f %q → %s (want: %s)\n", marker, bestSim, truncate(queryCase.query, 40), matchTopic, queryCase.targetTopic)
}
accuracy := float64(correct) / float64(len(queries)) * 100
@ -189,8 +189,8 @@ func main() {
// 4. Top-3 recall
correct3 := 0
for _, q := range queries {
qVec, _ := embed(model, q.query)
for _, queryCase := range queries {
queryVec, _ := embed(model, queryCase.query)
type scored struct {
idx int
@ -198,12 +198,12 @@ func main() {
}
var ranked []scored
for i, mv := range memVectors {
ranked = append(ranked, scored{i, cosine(qVec, mv)})
ranked = append(ranked, scored{i, cosine(queryVec, mv)})
}
sort.Slice(ranked, func(a, b int) bool { return ranked[a].sim > ranked[b].sim })
for _, r := range ranked[:3] {
if allTopics[r.idx] == q.targetTopic {
for _, rankedResult := range ranked[:3] {
if allTopics[rankedResult.idx] == queryCase.targetTopic {
correct3++
break
}
@ -271,9 +271,9 @@ func modelAvailable(model string) bool {
} `json:"models"`
}
json.NewDecoder(resp.Body).Decode(&result)
for _, m := range result.Models {
for _, registeredModel := range result.Models {
// Match "nomic-embed-text:latest" against "nomic-embed-text"
if m.Name == model || strings.HasPrefix(m.Name, model+":") {
if registeredModel.Name == model || strings.HasPrefix(registeredModel.Name, model+":") {
return true
}
}
@ -306,8 +306,8 @@ func avg(vals []float64) float64 {
return 0
}
sum := 0.0
for _, v := range vals {
sum += v
for _, value := range vals {
sum += value
}
return sum / float64(len(vals))
}

View file

@ -17,14 +17,14 @@ func TestCmd_ParseDuration_Good(t *testing.T) {
{"1h", time.Hour},
}
for _, tc := range tests {
got, err := parseDuration(tc.input)
for _, testCase := range tests {
got, err := parseDuration(testCase.input)
if err != nil {
t.Errorf("parseDuration(%q): unexpected error: %v", tc.input, err)
t.Errorf("parseDuration(%q): unexpected error: %v", testCase.input, err)
continue
}
if got != tc.want {
t.Errorf("parseDuration(%q) = %v, want %v", tc.input, got, tc.want)
if got != testCase.want {
t.Errorf("parseDuration(%q) = %v, want %v", testCase.input, got, testCase.want)
}
}
}
@ -46,3 +46,34 @@ func TestCmd_ParseDuration_Bad(t *testing.T) {
}
}
}
func TestCmd_ParseDuration_Ugly(t *testing.T) {
// Edge cases: valid-looking input that sits on the boundary of acceptance.
ugly := []struct {
input string
wantErr bool
}{
// Leading whitespace — Sscanf skips it, so " 7d" parses as 7d (valid).
{" 7d", false},
// Trailing whitespace after unit — unit is last char, not whitespace, so this is invalid unit ' '.
{"7d ", true},
// Very large count that still parses — 9999d is valid Go duration arithmetic.
{"9999d", false},
// Mixed case unit — 'D' is not 'd', so unknown unit.
{"7D", true},
// Float value — Sscanf %d won't accept "7.5", so invalid numeric.
{"7.5d", true},
// Just the unit char, no number — too short (len < 2 for "d"; "hd" has no valid number).
{"hd", true},
}
for _, testCase := range ugly {
_, err := parseDuration(testCase.input)
if testCase.wantErr && err == nil {
t.Errorf("parseDuration(%q): expected error, got nil", testCase.input)
}
if !testCase.wantErr && err != nil {
t.Errorf("parseDuration(%q): unexpected error: %v", testCase.input, err)
}
}
}

View file

@ -202,13 +202,13 @@ func buildJobIssueBody(target string, summary *AlertSummary, findings []string)
builder.WriteString("### Findings\n\n")
if len(findings) > 50 {
for _, f := range findings[:50] {
builder.WriteString(f + "\n")
for _, finding := range findings[:50] {
builder.WriteString(finding + "\n")
}
fmt.Fprintf(&builder, "\n... and %d more\n", len(findings)-50)
} else {
for _, f := range findings {
builder.WriteString(f + "\n")
for _, finding := range findings {
builder.WriteString(finding + "\n")
}
}