Bug复现程序编写
write-reproducer
为任何项目编写最小化、可靠的 bug 复现程序(repro)。使用时机:(1) bug 报告或 issue 需要一个能够可靠演示失败的测试;(2) 将描述的失败场景转换为可运行代码;(3) 将现有复杂测试最小化为必要的触发条件。涵盖完整的复现工作流:失败特征化、范围选择、编写复现程序、验证其因正确的原因失败,以及最小化到尽可能小的触发条件。
- License 许可
- Apache-2.0
- 所属仓库
- apache/cassandra
- 最近更新
- 2026-11-14
- Stars
- ★ 1.0w
- Forks
- 4.1k
01 前言 Front Matter
- name
- write-reproducer
- version
- 1.0.0
- description
- Write minimal, reliable bug reproducers (repros) for any project. Use when: (1) a bug report or issue needs a test that reliably demonstrates the failure, (2) converting a described failure scenario into runnable code, (3) minimizing an existing complex test to its essential trigger conditions. Covers the full repro workflow: failure characterization, scope selection, writing the repro, verifying it fails for the right reason, and minimizing to the smallest possible trigger.
02 资源文件 14
- 参考文档references/bug-taxonomy.md5.3 KB
- 参考文档references/concurrency.md5.7 KB
- 参考文档references/distributed-systems.md6.2 KB
- 参考文档references/failure-classification.md4.1 KB
- 参考文档references/fuzzing.md6.9 KB
- 参考文档references/minimality-lattice.md4.8 KB
- 参考文档references/minimization.md5.4 KB
- 参考文档references/output-contract.md3.6 KB
- 参考文档references/property-based-testing.md4.8 KB
- 参考文档references/shrinking-checklist.md3.2 KB
- 参考文档references/test-patterns.md6.4 KB
- 脚本scripts/detect_context.sh4.3 KB
- 脚本scripts/shrink_text.py3.7 KB
- 脚本scripts/validate_repro.sh3.0 KB
03 SKILL.md 正文
Write Bug Reproducer
Your goal is to turn a bug description into a minimal, self-contained, runnable reproduction that triggers a specific, machine-checkable failure criterion.
A reproducer has three parts — keep them distinct in your head and in your code:
- Trigger: the minimum input, state, schedule, or environment that causes the bug
- Harness: how the trigger is executed (test runner, script, container, cluster test)
- Oracle: the machine-checkable failure criterion (assertion, exit code, log pattern, invariant violation)
Follow the five phases below in order. Do not skip ahead. Write the failure criterion before writing any repro code.
Phase 1: Understand and classify
Restate the bug in one sentence: "The bug is..."
Classify along three axes:
- Bug class: crash, incorrect output, hang/deadlock, race condition, assertion/invariant violation, performance regression, resource leak, serialization bug, consistency anomaly. See
references/bug-taxonomy.mdfor the full taxonomy when classification is unclear. - Scope: pure function, module, single process, multiple processes, cluster.
- Determinism: deterministic, flaky (state what makes it flaky), requires rare schedule or timing.
If the user gave only symptoms with no code, list the 2-4 most likely causes before proceeding. Do not pick one yet — write hypotheses, not conclusions.
If the user included code or pointed at a codebase, read the suspect region and identify the likely code path before moving on.
Phase 2: Define the failure criterion
This is the most important phase. Before writing any repro code, write out the oracle in one of these machine-checkable forms:
- Assertion with concrete values:
assert expected == actualwith both spelled out - Exit code:
expected: 0, actual: 139 (SIGSEGV) - Regex against stdout/stderr: exact string fragment to match
- Log grep: file and pattern
- Exception type and message fragment: the specific error and location
- Linearizability/isolation check result: checker verdict (e.g., linearizability violation, cycle detected)
- Invariant violation: state the invariant in one line; state what breaks it
Record the failure criterion verbatim. It will be used in Phase 4 (to write the assertion) and Phase 5 (to prevent shrinking into a different bug).
Without an explicit oracle, shrinking can produce something that "fails" but not in the described way, and reviewers cannot tell whether the repro is successful.
Phase 3: Detect context and choose repro form
Inspect the working directory for build-system markers and prefer the project's existing test framework and conventions. Explore existing tests to understand helpers, fixtures, naming patterns, and how integration tests are structured.
Pick the lowest tier on the minimality lattice that can reliably exhibit the failure. See references/minimality-lattice.md for tier definitions and decision rules.
- Tier 1: expression / REPL snippet
- Tier 2: single-file snippet with
main - Tier 3: single test in the project's existing framework
- Tier 4: multi-file project fixture
- Tier 5: containerized environment + test script
- Tier 6: ephemeral cluster
- Tier 7: deterministic simulation
Write one sentence naming the tier and why, based on what you found in the project.
For concurrency bugs: references/concurrency.md
For distributed-systems bugs: references/distributed-systems.md
For invariant bugs where a property test is the natural form: references/property-based-testing.md
For bugs found by fuzzing or where a fuzz harness is the natural repro: references/fuzzing.md
Phase 4: Draft the repro
Structure
Label the three parts in comments:
// TRIGGER: <what causes the bug>
// HARNESS: <how we run it>
// ORACLE: <what we check>
Use the project's test framework and conventions. Match existing test style. Do not introduce a new framework unless the existing one cannot express the bug.
The pass-then-invert strategy
For subtle assertion bugs where the correct behavior is easy to state but the failing test is hard to get right, use pass-then-invert: first write a test that passes on the current buggy behavior (mirroring the observed wrong output in its assertions), then invert the assertions so they encode the correct expectation and therefore fail on the bug. This reduces hallucinated APIs and setup errors compared to writing a failing test directly.
Use pass-then-invert when:
- The bug produces a wrong value (not a crash or exception)
- The correct expected value is known
- Direct "write a failing test" attempts keep failing for setup reasons
Do not use pass-then-invert when:
- The bug is a crash, exception, or hang — just trigger it directly
- The oracle is an absence check (no exception, no crash) — inversion doesn't apply
Verification before presenting
The repro must compile or parse cleanly. Resolve imports against the project's actual dependencies. Do not hallucinate APIs — if unsure of a method signature, read the library source or ask.
If execution is available, run the repro and confirm the failure criterion triggers. If not, trace through the code mentally and state your confidence level.
Generating multiple candidates
When the bug is unclear or the first attempt fails, generate 2-5 candidate repro strategies:
- Smallest direct unit test
- Integration test if state/config matters
- Standalone script if no test harness is obvious
- Property-based test if the bug is about an invariant
- Fuzz seed / deterministic scenario for correctness bugs
Execute each candidate with a timeout. Classify each result using references/failure-classification.md. Refine until one produces an issue-relevant failure.
Documentation
The repro must include a comment or docstring stating:
- Issue reference and one-line bug title
- Expected (correct) behavior
- Actual (buggy) behavior
- Failure criterion (verbatim from Phase 2)
Phase 5: Shrink and validate
Validate first
Run the repro (or trace through it mentally) and confirm:
- The failure criterion from Phase 2 triggers
- The failure is the described bug, not a test setup error
- The failure message matches what Phase 2 specified
See references/failure-classification.md for how to classify test results and distinguish issue-relevant failures from setup errors.
Then shrink
Use the subtraction method from references/minimization.md. Work through dimensions in order (cheapest first):
- Reduce infrastructure scale (instances, partitions, replication)
- Reduce data volume (items, size, batches)
- Remove non-default configuration
- Remove setup steps
- Replace real sleeps with controlled time or polling
- Simplify trigger sequence
See references/shrinking-checklist.md for the guardrail checklist.
Over-minimization guardrail
After each shrinking step, re-check that the failure is still the same failure — same assertion, same exception type, same message fragment from Phase 2. If the failure changed, revert that step. This prevents shrinking into a different bug.
For nondeterministic bugs
Wrap the trigger in an N-iteration harness with a retry budget. Report the observed failure rate (e.g., "fails 23/1000 iterations"). Do not use sleep-based synchronization — it makes tests flaky on different hardware. Prefer event-based scheduling, explicit synchronization primitives, or deterministic simulation.
Always print and record seeds for randomized tests so failing scenarios can be replayed.
Output
Always produce:
- Repro file(s) with
// TRIGGER,// HARNESS,// ORACLEcomments. Correct file placement matching the project's test layout. - Status: one of
REPRODUCED/CANDIDATE/BLOCKED. Seereferences/output-contract.mdfor what each status means and requires. - Run command: the exact command to execute the repro.
- Failure summary: "This test should fail on the current code with:
<expected failure message>" - For flaky bugs: failure rate and seed.
- For blocked repros: what's missing, what was attempted, next best direction.
Hard rules
-
Write the failure criterion before the repro code. Without an explicit oracle, you cannot distinguish a successful repro from a coincidental failure. This is the single most common failure mode.
-
Do not hallucinate APIs. If unsure whether a method, class, or flag exists, read the source or ask. Hallucinated APIs are the most frequent reason LLM-generated repros fail to compile.
-
Assert expected behavior, not current broken behavior. The oracle encodes what should happen. The test fails because reality doesn't match.
-
Do not shrink past the bug. After every shrinking step, re-verify the failure matches Phase 2. See
references/shrinking-checklist.md. -
Flaky repros must declare their flakiness. A repro that reproduces 23/1000 times must say so and wrap itself in a retry loop.
-
Match the project's conventions. Use the test framework, style, and helpers that already exist. Do not introduce a new framework unless the existing one cannot express the bug.
-
Do not fix the bug. Create tests/scripts/harnesses only. Do not edit production code unless explicitly asked.
-
Do not use
:latestimage tags or unpinned dependency versions. A repro is not reproducible if its dependencies drift. -
Do not confuse setup errors with reproduced bugs. An error in test setup is not the same as the error caused by the bug. See
references/failure-classification.md.
When to escalate to higher tiers
Escalate to Tier 5+ (containerized, cluster, deterministic simulation) when:
- The bug involves two or more processes (client + server, replica + leader)
- The bug requires a specific network condition (partition, delay, drop)
- The bug requires clock skew or node failure scenarios
- The bug is about isolation, linearizability, or consensus
- The user mentions Cassandra, Kafka, or any distributed system
For these cases read references/distributed-systems.md before drafting.
When to use a property-based test
Use a property-based test when:
- The bug is about an invariant (round-trip, idempotence, commutativity, ordering, bounded sums)
- Shrinking comes for free in the property framework
- You want the test to serve as a regression test with seed persistence
See references/property-based-testing.md.
When to use a fuzz harness
Use a fuzz harness when:
- The bug was originally found by fuzzing and a crashing input exists
- The input is a byte stream or structured binary format
- A single example fails but many nearby inputs are interesting for regression
See references/fuzzing.md.
04 同仓库更多 Skills
TLA+形式化验证
tla-plus
创建、运行和验证 TLA+ 与 PlusCal 形式化规约。用于对分布式系统、协议、并发算法和状态机进行建模。可以从代码编写规约、发现规约与实现之间的分歧、识别并发 bug 和不变量违规。当用户要求“write a TLA+ spec”、“model check”、“verify protocol”、“find race conditions”或“formal verification”时使用。
定向代码审查
targeted-review
发现驱动的定向代码审查。通过 patch-explainer 理解补丁,通过 codebase-analysis 理解周边代码,然后查阅 bug 模式目录(references/categories/ 下约 11 个类别)。仅挑选 diff 信号与补丁匹配的类别,以及“look for”提示与实际代码形态匹配的条目,按审查重点(函数/文件/特性)分组,并生成并行子代理——每个子代理都有紧凑的范围和经过挑选的清单。适用于中大型补丁(50-1000 行代码),可作为整补丁集成式审查或整文件深度审查的聚焦替代方案。触发条件包括:“review this patch/diff/change”、“find bugs in this change”、“scoped review”、“what could go wrong here”、“review using findings”,或主动进行 PR 审查。
快速并行代码审查
shallow-review
使用 6 个专家代理并行开展浅层(快速)集成式 bug 查找审查。每个专家通过不同视角审查同一补丁:逻辑与类型、边界与 I/O、并发与状态、资源与序列化、缺失分析以及 API 完整性。发现的问题会被合并并去重。最适合:补丁的快速初审、diff 分诊、广泛的表层 bug 扫描。如需以文件为中心、包含完整模式目录和代码库调查的更深入审查,请改用 deep-review 技能。
代码补丁可视化解析
patch-explainer
深度代码分析,通过 ASCII 可视化展示代码结构、流程和状态转换。适用于分析补丁/diff、解释类或子系统、理解代码架构、审查变更中的不一致之处,或被要求可视化代码的工作原理时。提供前后对比图、数据/控制流、并发分析、假设条件和失败模式。触发词:explain this patch/code/class、how does X work、show me the flow、visualize this change、代码审查请求,或在 PR 审查期间主动分析。
大型补丁多轮深度代码审查
mega-review
针对大型补丁(1000+ 行代码)的多轮深度代码审查,最大化真实 Bug 的检出率。并行编排 targeted-review、shallow-review 和 deep-review,覆盖所有高风险(HIGH)和中风险(MEDIUM)文件与提交:从整体上理解特性,按文件和提交拆分,对每个 HIGH/MEDIUM 文件运行深度审查,在同一范围内运行定向审查,并对每个提交执行浅层审查,随后进行交叉一致性检查。适用场景:审查大型补丁(特性分支、多提交或单个大型 diff,1000+ 行代码);进行彻底的合并前审查;或当浅层审查因补丁规模过大而漏掉 Bug 时。触发词:"review this branch"、"review these commits"、"review this feature"、"mega review"、"thorough review"、"full review of X commits",或当用户指定提交范围或特性进行审查时。
Git 代码热力图分析
heatmap
使用 Git 热力图分析识别高变动量的文件和代码行,作为深入审查或 Bug 排查的候选对象。适用于 PR 审查、安全审计、Bug 排查或任何代码分析任务。