Skip to content

feat(ci,scripts): add Go differential mutation testing tool and CI reporting - #1264

Merged
kyasbal merged 2 commits into
mainfrom
push-zyypprtyxrvk
Nov 12, 2026
Merged

kyasbal merged 2 commits into
mainfrom
push-zyypprtyxrvk

Conversation

@kyasbal

@kyasbal kyasbal commented Nov 12, 2026

Copy link
Copy Markdown
Member

Summary

Adds a zero-dependency Go differential mutation testing tool (scripts/mutation-diff / make mutation-diff) and integrates soft-start PR reporting into .github/workflows/pullrequest.yaml.

Line coverage (coverage-diff) verifies whether changed lines were executed, whereas mutation-diff verifies whether unit tests meaningfully assert the behavior of changed lines and exposes redundant/dead production branches — without forcing brittle mock tests.

Key Changes

  • Shared Diff & Coverage Utilities (scripts/gotestutil):
    • Extracted unified diff parsing (jj & git), target Go file filtering, coverprofile loading (CoveredLineSet), and GitHub Actions workflow escaping from scripts/coverage-diff into scripts/gotestutil so both tools share a single implementation.
  • Super-Mutant Schemata Engine (scripts/mutation-diff):
    • Uses Go's standard go/parser, go/ast, go/token, and go test -overlay to synthesize candidate mutations on changed lines into a single temporary overlay per package (1 go test -c compile per modified package, zero working-tree mutation).
    • Runs a baseline reachability trace (KHI_MUTANT_ID=0 + lock-free atomic.Bool trace) to skip unreached sites and executes reached mutants in parallel workers with -test.failfast.
    • Extensible ExclusionRule Pipeline: Automatically excludes boilerplate err != nil guards, slog/log/klog/fmt.Print*/fmt.Errorf/errors.New/otel calls, make capacity hints / .Grow, defer/panic, and pure constant/type declarations to avoid encouraging noisy mock tests.
    • Categorized // NOMUTANTS(<category>): <reason> Directives: Requires an explicit SuppressionCategory (equivalent, perf, observability, external) and rejects invariant/unreachable branch suppressions with guidance to simplify or delete redundant code instead.
  • Makefile, CI & Documentation:
    • Added make mutation-diff in scripts/make/testing.mk.
    • Added Report Backend Differential Mutation Testing step (soft-start, exit 0, inline ::warning annotations + $GITHUB_STEP_SUMMARY Markdown report with anti-bloat triage guide) in .github/workflows/pullrequest.yaml.
    • Updated .agents/rules/go-coding-rule.md and docs/{en,ja}/development-contribution/development-guide.md.

Verification

  • make pre-commit (0 issues / 0 error(s))
  • make build-go & make test-go
  • Dogfooded make mutation-diff against both scripts/ and recent KHI main commits (pkg/...)

@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 a new differential mutation testing tool (make mutation-diff) under scripts/mutation-diff/ to verify that unit tests meaningfully assert changed Go logic. It also refactors scripts/coverage-diff/ to extract shared Go test utilities into a new package scripts/gotestutil/, and updates the development guides and Go coding rules accordingly. Feedback on the changes points out a potential concurrency issue in the generated runtime helper's trace recording function (__khiMutRecord), suggesting the addition of a sync.Mutex to prevent race conditions and file sharing violations during concurrent test execution.

Comment on lines +76 to +95
var __khiMutSeen [%d]atomic.Bool

func __khiMutRecord(id int) {
if id <= 0 || id >= len(__khiMutSeen) {
return
}
if __khiMutSeen[id].Load() || __khiMutSeen[id].Swap(true) {
return
}
p := __khiMutTracePath()
if p == "" {
return
}
f, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
if err != nil {
return
}
_, _ = f.WriteString(strconv.Itoa(id) + "\n")
_ = f.Close()
}

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.

medium

When running tests concurrently, multiple goroutines might call __khiMutRecord and attempt to open and write to the same trace file concurrently. On some operating systems (especially Windows), concurrent file opens for writing can fail with sharing violations or cause race conditions. Adding a package-level sync.Mutex to protect the file open and write operations ensures thread safety and prevents test flakiness.

var __khiMutMu sync.Mutex
var __khiMutSeen [%d]atomic.Bool

func __khiMutRecord(id int) {
	if id <= 0 || id >= len(__khiMutSeen) {
		return
	}
	if __khiMutSeen[id].Load() || __khiMutSeen[id].Swap(true) {
		return
	}
	p := __khiMutTracePath()
	if p == "" {
		return
	}
	__khiMutMu.Lock()
	defer __khiMutMu.Unlock()
	f, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
	if err != nil {
		return
	}
	_, _ = f.WriteString(strconv.Itoa(id) + "\n")
	_ = f.Close()
}
@kyasbal
kyasbal added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 2e1adb0 Nov 12, 2026
21 checks passed
@kyasbal
kyasbal deleted the push-zyypprtyxrvk branch November 12, 2026 03:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant