Teaching an AI Agent to Debug Flaky Tests

Igor Kulakov ยท article ยท 2026-05-09 source โ†— Reader ๐Ÿ–ฅ๏ธ

Agent-Native DesignAgent SkillsCoding Agents
Flaky tests can be debugged deterministically by diffing code-coverage reports from passing and failing runs to locate where execution paths diverge, and this entire procedure can be packaged as a reusable AI Agent Skill so that an agent follows it without human guidance.

practical ยท tutorial-narrative โ€” A walkthrough that builds from intuition to tooling to a reusable AI Skill, using a concrete TOCTOU race condition as the running example. Part tutorial, part methodology argument.

Structure

PartRole
Problem statement: flaky tests undermine CIEstablishes the pain โ€” non-deterministic pass/fail with no code changes wastes human and infrastructure resources
Example project: TOCTOU race in a Spring Boot webshopProvides a concrete, reproducible flaky test (concurrent checkout producing duplicate invoice numbers)
The intuition: different results imply different code pathsDerives the key insight โ€” if we record and diff the code paths of passing vs. failing runs, the divergence point reveals the root cause
Building the tools: coverage agent + text reporter + diff scriptShows how to (1) run IntelliJ coverage from CLI with hit counting, (2) convert binary reports to text, (3) collect multiple runs and summarize varying lines
Interpreting the diff: hit-count variation pinpoints the raceDemonstrates that the varying lines point exactly to the lazy-init block where the TOCTOU race lives
Packaging as an AI Agent SkillConverts the manual procedure into a SKILL.md that an agent can discover and execute autonomously
Testing the Skill in a fresh agent sessionValidates that a naive prompt ('find and fix the cause of flakiness') triggers skill discovery and correct execution

Key terms

Propositions

Arguments

Coverage-diff is a sound diagnostic for flaky tests strength: medium
Explicit premises:
Implicit premises:
Inferences:
Packaging the procedure as an AI Skill makes debugging deterministic strength: medium
Explicit premises:
Implicit premises:
Inferences:

Narratives

Questions addressed

Gaps

Related documents