Skip to content

feat(scripts): add redundant test detection to mutation-diff - #1265

Merged
kyasbal merged 1 commit into
mainfrom
push-kmrvzyuvunmy
Nov 12, 2026
Merged

kyasbal merged 1 commit into
mainfrom
push-kmrvzyuvunmy

Conversation

@kyasbal

@kyasbal kyasbal commented Nov 12, 2026

Copy link
Copy Markdown
Member

Summary

Follows up on #1264 by adding Kill-Matrix & Branch-Polarity redundant test detection (--check-redundant-tests, enabled by default) to scripts/mutation-diff.

While differential coverage (coverage-diff) and surviving mutant detection (mutation-diff) guard against missing or weak tests, AI-generated PRs frequently err in the opposite direction by adding duplicate equivalence-class subtests or assertion-free smoke tests. This change reuses the Super-Mutant Schemata compile and execution pass to automatically flag redundant tests added or modified in the PR diff.

Key Changes

  • Test Diff Extraction (scripts/gotestutil):
    • Added IsTargetGoTestFile, ParseUnifiedDiffSplit, and DiffResult.ChangedTestLines so redundant-test reporting is scoped strictly to newly added or modified Test* functions and subtests in *_test.go (preventing noise on untouched legacy tests).
  • Directed Branch-Polarity Runtime Trace (scripts/mutation-diff/schema.go):
    • Extended zzz_khi_mutant_runtime.go with lock-free [N][3]atomic.Bool slots (<mutantID>:T, <mutantID>:F, <mutantID>:S) and ParsePolarityTraceFile to record exact branch directions per leaf test.
  • Kill-Matrix & AST Redundancy Analyzer (scripts/mutation-diff/redundancy.go, runner.go, report.go):
    • Combines per-leaf baseline polarity traces with -test.v per-subtest failure parsing (VerboseTestResult) to surface two high-precision categories:
      1. DUPLICATE: A newly added/changed test has Kills(cand) != ∅, exercises identical branch polarities (equalStringSet — preventing multi-call integration tests from subsuming focused unit tests), kills a subset of another test's mutants (Kills(cand) ⊆ Kills(dom)), and did not touch any budget-skipped mutant (MutantSkippedBudget).
      2. WEAK_TEST: A newly added/changed test has 0 mutant kills, contains only if err != nil checks (or no assertions) in its AST body, and exercises a branch-polarity subset already covered by a mutant-killing test (preserving valid fixed-point boundary value assertions).
    • Emits CLI summaries, GitHub Actions ::warning annotations, and a collapsible $GITHUB_STEP_SUMMARY table.
  • coverage-diff Script-Package Fix (scripts/coverage-diff, scripts/make/testing.mk):
    • Propagated FilterOptions into PlanTestExecution and added COVERAGE_INCLUDE_SCRIPTS / COVERAGE_VCS so make coverage-diff and make mutation-diff share symmetric flags when analyzing scripts/.

Verification

  • make pre-commit (0 issues / 0 error(s))
  • make build-go & make test-go
  • make coverage-diff COVERAGE_INCLUDE_SCRIPTS=true: 87.8% (568 / 647 executable changed lines covered)
  • make mutation-diff MUTATION_INCLUDE_SCRIPTS=true: 77.5% (155 / 200 killed, 0 redundant tests in diff)
  • Validated against recent KHI main commits (origin/main~4)
@kyasbal
kyasbal enabled auto-merge November 12, 2026 04:01
@kyasbal kyasbal added the review:done Maintainer review is complete (LGTM) label Nov 12, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces functionality to detect and report potentially redundant unit tests and subtests (categorized as DUPLICATE or WEAK_TEST) during differential mutation testing. It adds a new redundancy analysis engine in scripts/mutation-diff/redundancy.go, integrates it with the mutation runner and reporting systems, and updates the documentation and Makefiles accordingly. Feedback on the changes highlights that the subtest name normalization logic in normalizeGoSubtestName should be updated to match Go's standard library sanitization rules to prevent silent mismatches when subtest names contain special characters.

Comment on lines +229 to +240
func normalizeGoSubtestName(raw string) string {
var sb strings.Builder
sb.Grow(len(raw))
for _, r := range raw {
if unicode.IsSpace(r) {
sb.WriteByte('_')
} else {
sb.WriteRune(r)
}
}
return sb.String()
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In Go's testing package, subtest names are sanitized using a safeName function that replaces any character other than alphanumeric, _, -, and . with an underscore (_). The current implementation of normalizeGoSubtestName only replaces spaces, which can lead to silent mismatches and failure to detect redundant tests when subtest names contain parentheses, slashes, or other non-safe characters (e.g., name: "should return error (invalid input)"). Updating this function to match Go's exact sanitization logic ensures robust subtest matching.

Suggested change
func normalizeGoSubtestName(raw string) string {
var sb strings.Builder
sb.Grow(len(raw))
for _, r := range raw {
if unicode.IsSpace(r) {
sb.WriteByte('_')
} else {
sb.WriteRune(r)
}
}
return sb.String()
}
func normalizeGoSubtestName(raw string) string {
var sb strings.Builder
sb.Grow(len(raw))
for i := 0; i < len(raw); i++ {
c := raw[i]
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' || c == '.' {
sb.WriteByte(c)
} else {
sb.WriteByte('_')
}
}
return sb.String()
}
@kyasbal
kyasbal added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 3474bb9 Nov 12, 2026
21 checks passed
@kyasbal
kyasbal deleted the push-kmrvzyuvunmy branch November 12, 2026 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review:done Maintainer review is complete (LGTM)

1 participant