targeted-mutation 0.8.0

Targeted mutation testing for Rust: find the wrong implementations your tests let through
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# targeted-mutation

[![Crates.io](https://img.shields.io/crates/v/targeted-mutation.svg)](https://crates.io/crates/targeted-mutation)
[![CI](https://github.com/t-k/targeted-mutation/actions/workflows/ci.yml/badge.svg)](https://github.com/t-k/targeted-mutation/actions/workflows/ci.yml)
[![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
[![MSRV: 1.88](https://img.shields.io/badge/rustc-1.88%2B-orange.svg)](#quick-start)

**Targeted mutation testing for Rust (`cargo tmt`)**: finds the realistic wrong implementations your tests would let through — and tells you the **exact input or assertion** that catches them.

## Why targeted mutation testing?

Classic mutation testing mutates code blindly (`<` → `<=`), recompiles the crate for every mutant, runs the whole test suite each time, and leaves you with *"mutant survived"* to diagnose by hand.

**`cargo tmt` flips the workflow:**

1. It compiles your crate **once** with runtime-switched probes and traces what values your tests actually feed each site.
2. From those observations, it uses **interval arithmetic** to compute the boundary mutants guaranteed to pass every test (for deterministic tests; re-verified by default) — without needing AI or an SMT solver.
3. For mutants that *do* change an observation, it runs **only the tests affected by that site**, traces what changed downstream, and points straight to the missing input or weak assertion.

![Classic Mutation Testing vs. Targeted Mutation Testing](docs/images/classic-vs-targeted.svg)

| | Classic mutation testing | Targeted mutation testing (`cargo tmt`) |
|---|---|---|
| **Builds** | Rebuilds once per mutant (`O(N)`) | **1 build** for all probe-compatible mutants |
| **Test runs** | Runs the whole suite per mutant | Skips provable survivors; runs **only affected tests** |
| **Output** | `MISSED src/lib.rs:2 replace >= with >` | *"Saw `age` = 17, 20 → add a test with `age` = 18"* |
| **Weak assertions** | Manual debugging | Effect trace (`with the mutant, ...`) + verified `assert_eq!` (`--rebuild`) |
| **Dependencies** | — | Pure Rust, **no AI, no SMT solver** |

---

## Quick start

Building `cargo tmt` requires Rust **1.88 or newer**. The crates it analyses are built with whatever toolchain is active for them.

```sh
cargo install targeted-mutation
cargo new --lib demo && cd demo
```

Put this in `src/lib.rs`:

```rust
pub fn is_eligible(age: u32) -> bool {
    age >= 18
}

#[test]
fn minor_is_rejected() {
    assert!(!is_eligible(17));
}

#[test]
fn adult_is_accepted() {
    assert!(is_eligible(20));
}
```

Run `cargo tmt`:

```text
$ cargo tmt --quiet
INPUT GAP  src/lib.rs:2:5
    age >= 18
  passes every test as: `age >= 19`, `age >= 20`  [re-run: tests pass]
  nearest values the tests passed: `age` = 17, 20
  add a test with `age` = 18 (the original gives true)

demo: 4 sites, 1 input gaps, 0 assertion gaps, 8 mutants run (6 killed), 0 sites unreached
$ echo $?
2
```

Add the exact test it asks for and re-run:

```rust
#[test]
fn eighteen_is_accepted() {
    assert!(is_eligible(18));
}
```

```text
$ cargo tmt --quiet
demo: 4 sites, 0 input gaps, 0 assertion gaps, 7 mutants run (7 killed), 0 sites unreached
$ echo $?
0
```

### Deeper bugs & verified witnesses (`--rebuild`)

Some wrong implementations can only be caught by building source mutants one by one — and with `--rebuild`, `cargo tmt` also synthesizes and verifies concrete distinguishing tests. Append a basis-points fee function and its tests to the same `src/lib.rs`:

```rust
pub fn fee(cents: u32, bps: u32) -> u64 {
    u64::from(cents) * u64::from(bps) / 10_000
}

#[test]
fn round_amounts() {
    assert_eq!(fee(10_000, 125), 125);
    assert_eq!(fee(20_000, 250), 500);
}
```

Plain `cargo tmt` finds nothing; `cargo tmt --rebuild` tries dividing before multiplying, runs a generated test against both versions, and gives the exact assertion to add:

```text
SURVIVOR  src/lib.rs:21:5
    u64::from(cents) * u64::from(bps) / 10_000
  passes every test as: `u64::from(cents) / 10_000 * u64::from(bps)` (divide before multiplying)
  verified on `fee(10080, 125)`: original returned 126, mutant returned 125
  The tests pass with the division by 10000 done first; this `cents` is not a multiple of it.
  add a test with `assert_eq!(fee(10080, 125), 126);`
```

> **Security note:** `cargo tmt` builds and runs the target crate's tests and build scripts, just as `cargo test` does. Run it only on code you trust.

---

## Understanding findings

| Finding | Meaning | What to add |
|---|---|---|
| **`INPUT GAP`** | No test passes a value separating the mutant from the original. | A test with the listed boundary input. |
| **`ASSERTION GAP`** | Tests execute the changed behaviour and still pass. | An assertion in one of the listed tests (guided by `with the mutant, ...`). |
| **`SURVIVOR`** | A source-built mutant (`--rebuild` / `--source-only`) passes all tests. | A test using the verified witness (if shown) or covering the mutated code. |
| **`UNTESTED CHANGE`** | With `--since REV` + rebuild mode, reverting a diff hunk still passes all tests. | A test that fails on the old code (or silence refactorings with `// tmt:ignore`). |
| **`UNREACHED`** | No test executes the site (shown with `--show-unreached`). | A test reaching the branch/code. |
| **`UNSTABLE`** | A trace-predicted survivor failed on re-run. | Fix test nondeterminism first. |

Findings on the same source line are grouped into one block.

### Input gaps & impossible-value filtering

In `fixtures/eligibility`, the tests only pass `17` and `20` to `age >= 18`, so any threshold in `18..=20` behaves identically on every test. `cargo tmt` derives that range directly from the two nearest observations and names `age = 18`.

Values no input can produce are filtered out automatically: `blocks * 8` is always a multiple of 8, `flags & 0xF0` only has bits of the mask, and `n % 10` is strictly below 10. When no reachable value lies between the observed values — or when an expression cannot flip a comparison (`n % 10 < 10`, `flags & 0b1100 == 1`) — no bogus gap is reported. Reachability is derived from the expression's outer integer operation where tests executed it on primitive integers and the invariant holds under wrapping (power-of-two products, bit shifts modulo type width, bit masks, and remainders).

### Assertion gaps & effect tracing

`fixtures/batch-store` has an atomic batch commit whose test checks that a failing batch returns an error, but never inspects store state afterwards:

```text
$ cargo tmt fixtures/batch-store
ASSERTION GAP  src/lib.rs:33:9
    self.check_preconditions(batch)?;
  passes every test as: delete `self.check_preconditions(batch)?;`
  these tests run the changed code and still pass: tests::batch_writes_are_visible_after_commit, tests::commit_fails_when_an_update_target_is_missing
  with the mutant, `tests::commit_fails_when_an_update_target_is_missing` now runs `self.docs.insert(key.clone(), value.clone());` (src/lib.rs:59)
  add an assertion on what this code is responsible for
```

For each assertion gap, `cargo tmt` replays one surviving mutant with tracing on (`effects_of` in JSON) and compares its trace with the original run. When the mutant changes nothing observable in the trace, either the test ignores the mutated value, its assertion holds for both outcomes (`assert_ne!(bucket(3), "none")`), or the mutant is equivalent.

---

## How it works

![How cargo tmt works pipeline](docs/images/how-it-works.svg)

1. **Instrument.** Rewrites comparisons, boolean operators, arithmetic/bitwise operators, ranges, `min`/`max`, `first`/`last`, match arms/guards, struct fields, return exits, and deletable statements into runtime-switched probes inside an isolated working copy. The crate is compiled **once**.
2. **Trace.** Runs the tests once and records per-test observations (for integer comparisons: nearest `lhs - rhs` on each side of zero and whether operands were ever equal).
3. **Plan.** Computes provable input-gap survivor ranges by interval arithmetic, excluding impossible values within the operand types (`x >= 0` for unsigned `x`, masks, remainders, etc.). By default one predicted survivor per gap is re-run to confirm (`--no-verify` skips this).
4. **Verify & Run.** Runs remaining mutants only against the tests whose observations change, stopping at the first failure. Survivors are traced to show downstream effects (`--no-effects` skips this).
5. **Source builds & witnesses (optional `--rebuild` / `--source-only`).** Code that cannot carry runtime probes (`const fn`, `const`/`static` initializers, `impl Iterator`, untyped literal pairs like `60 * 60`, complex compound assignment places like `*map.entry(k).or_insert(0) += 1`, secondary/shared targets, or probes dropped by repair) is compiled and tested per mutant. Without `--rebuild`, the summary tells you how many candidates need it.

### What is mutated

| Code | Mutants | Provable survivor when |
|---|---|---|
| `a < b`, `a >= 18` (integers) | shifted thresholds, negated operator | no test passes a value between old and new boundary |
| `x > 30.5` (floats) | `>=`, thresholds between nearest observed values | operands were never equal in any test |
| comparisons on other `PartialOrd` types | `<` ↔ `<=`, negation | operands were never equal in any test |
| `a == b`, `a != b` | negation, `true` / `false` | result was identical in every test |
| `a && b`, `a \|\| b` | ignored operand (`a && { b; true }`, `{ a; b }`), swapped operator | ignored operand never decided the result |
| `+`, `-`, `*`, `/`, `%` & compound assigns | two alternatives each (`+`→`-`,`*`; `-`→`+`,`/`; `*`→`/`,`+`; `/`→`*`,`%`; `%`→`/`,`+`) | both operators are defined and agree on every observed pair |
| `&`, `\|`, `^`, `<<`, `>>` & compound assigns | bitwise alternatives / `<<` ↔ `>>` | as above |
| `!x` (`bool`), `-x` / `-1` | `x`, `1` | always run |
| function bodies | return-type value replacements (`()`, `0`/`1`/`-1`, `bool`, `String`, `Option`, `Result`, `Vec`/collections, `Box`, tuples/arrays, `Default::default()`, plus `--error EXPR`) | always run |
| `match` arm guards | `true`, `false` | guard had the same value in every test |
| `match` arms before a catch-all | arm removed (falls through) | always run |
| struct literal field with `..base` | field omitted (falls back to `base`) | always run |
| `-> bool` / `-> Option<T>` normal exits | `true` / `false` (`bool`), `None` (`Option`) | function always returned that value |
| `a.max(b)`, `a.min(b)` (numbers) | swapped `min` ↔ `max` | operands were equal in every test |
| `v.first()`, `v.last()` (slice/`Vec`/array/`BTreeSet`) | swapped `first` ↔ `last` | no test passed more than one element |
| `s.starts_with(p)`, `s.ends_with(p)` (`str`/`String`) | relaxed to `s.contains(p)` | no test passed a string containing `p` away from that end |
| `s.replace(a, b)`, `s.replacen(a, b, 1)` (`str`/`String`) | swapped `replace` ↔ `replacen(..., 1)` | no test had `a` occur more than once |
| `x.clamp(lo, hi)` (numbers) | `x.min(hi)` (drop lower), `x.max(lo)` (drop upper) | no test crossed that bound (impure/panicking bounds use `--rebuild`) |
| `x.floor()`, `.ceil()`, `.round()`, `.trunc()` (floats) | two alternative rounding modes each | observed inputs gave the same rounded value under the alternative |
| `iter.all(f)`, `iter.any(f)` (`Iterator` with closure) | swapped `all` ↔ `any` | always run |
| `[lit1, lit2, ...].contains(&x)` (2–8 distinct literals) | one element removed at a time (`--rebuild` / `--source-only`) | source-built |
| `a..b`, `a..=b` (integers) | inclusive ↔ exclusive end | always run |
| `f(x);`, `check()?;`, `x += 1;`, `x = y;` | statement deleted | always run |

- Arithmetic on non-primitive types (`String + &str`, `Duration + Duration`) and `!` on non-`bool` types are left untouched.
- Enclosing sites do not repeat inner findings (if `a > 0` is always true, `a > 0 && b` does not emit a duplicate finding).
- Arguments inside `assert_eq!`, `assert_ne!`, `vec!`, `format!`, `print!`, `println!`, `eprint!`, `eprintln!`, `write!`, `writeln!`, `panic!`, `unreachable!`, `todo!`, `unimplemented!`, `format_args!`, and `matches!` are mutated; `assert!`, `debug_assert!`, and `dbg!` are skipped because they stringify their argument expression.
- Functions, `impl` blocks, and modules marked `#[mutants::skip]` (including inside `cfg_attr`) are skipped.
- Probes that fail to compile are dropped automatically and retried; `--rebuild` evaluates their source mutants individually.

### Verified witnesses (`--rebuild`)

When `--rebuild` (or `--source-only`) finds a surviving mutant in a library file, `cargo tmt` can synthesize and run a concrete distinguishing test in the working copy, reporting both observed outputs and an `assert_eq!` line (exposed as `findings[].witness` in JSON):

- **Slice searches & inclusive ranges**: two-match inputs distinguishing `position` vs `rposition`, and minimal edits to literal test slices for inclusive range bounds.
- **Bitmasks & Unicode length**: partial multi-bit masks (any-bit vs all-bit) and non-ASCII characters (`chars().count()` vs `.len()`).
- **Comparisons & sort keys**: swapping same-type integer parameters/fields across a boundary, and two-record inputs separating sort-key choices.
- **Post-failure state reads**: borrowing a read from a nearby test to check persisted state after a failing batch operation.
- **Integer rounding (`a * b / c`)**: trying `a / c * b` with the smallest non-multiple offset from an existing test call (`fixtures/rounding`).
- **Composite keys (`format!` / tuples)**: omitting each argument to detect map keys that never collide in tests (`fixtures/keyed`).
- **Paired endian codecs**: flipping byte order on both encode and decode to detect round-trip-only codec tests and suggest a fixed-bytes check.
- **Stronger assertions on existing test calls**: replaying unpinned calls (`assert_ne!(bucket(3), "none")`, `assert!(total(&[1, 2]) > 0)`, `let` bindings, including `super::f(...)` in `mod tests`) in place inside a copy of the test to suggest exact `assert_eq!` values (`witness.in_test`, `in_test_line`, `in_test_column`). When both original and mutant return the same multi-field `Debug` struct differing on one field (recursively), `cargo tmt` narrows and compiler-verifies the assertion to that field (`assert_eq!(quote(200, 3).fee, 600);`, recorded in `witness.field`) even without `PartialEq`.

<details>
<summary>Details: witness fixture examples and in-place assertion replay rules</summary>

Try the bundled witness fixtures directly:

```sh
cargo tmt fixtures/shapes --rebuild --lines src/lib.rs:4-9 --lines src/lib.rs:28-29
cargo tmt fixtures/shapes --rebuild --lines src/lib.rs:34-40
cargo tmt fixtures/operators --rebuild --lines src/lib.rs:72-74
cargo tmt fixtures/distinguishing --rebuild
cargo tmt fixtures/batch-store --rebuild --lines src/lib.rs:33
cargo tmt fixtures/rounding --rebuild
cargo tmt fixtures/keyed --rebuild
```

In-place assertion replay runs a copy of the test beside the original and records the value at the exact point of the call (preserving setup before and teardown after):
- Calls already pinned by `assert_eq!`/`assert!` (whose other side calls no function of the file), multi-line calls, closure/branch/loop calls, methods, generic/`async`/`unsafe`/`const` functions, and calls with non-literal arguments are skipped.
- Tests that are `#[ignore]`, `#[should_panic]`, return `Result`, or shadow the function name with a `let` binding are skipped.
- At most 4 distinct calls (deduplicated by normalized argument tokens) are tried per function; not active with `--literal-operators`.
- A test that depends on its own thread name or `line!()`, or holds a lock required by `Debug`, can differ in the copied test.

</details>

### PR-scoped analysis (`--since REV`)

Pass `--since "$(git merge-base origin/main HEAD)"` to scope analysis to lines changed in a pull request plus untracked files.

#### Untested changes (`--since` + `--rebuild` / `--source-only`)

Each diff hunk in production code is reverted to `REV`, built, and tested. Multi-addition hunks are also decomposed into independent AST sub-hunks (added `&&`/`||` conditions, added struct literal fields with `..base`, and added standalone statements), so an untested clause inside an otherwise-tested hunk is still caught. If every test still passes, the tests cannot tell the new code from the old:

```text
$ cargo tmt --rebuild --since "$(git merge-base origin/main HEAD)"
UNTESTED CHANGE  src/lib.rs:9:1
    now - used < 30 && created <= used
  passes every test with the code as it was: `let _ = used; now - created < 30`
  no test tells this change from the code as it was at 91e1dc927360: add one that fails without it
```

#### Weakened tests (`--since`)

When a file's inline tests (`#[cfg(test)]` / `#[test]`) changed since `REV`, `cargo tmt` inspects the whole file and re-runs surviving mutants in unchanged lines against the tests as they were at `REV`. Mutants the old tests caught are reported under `weakened` (and as `weakened-test` in SARIF):

```text
INPUT GAP  src/lib.rs:27:5
    age >= 18
  passes every test as: `age >= 19`, `age >= 20`  [re-run: tests pass]
  nearest values the tests passed: `age` = 17, 20
  add a test with `age` = 18 (the original gives true)
  the tests as they were at e76c0bd70fff found this out: a change to the tests lets it through
```

<details>
<summary>Details: diff-revert and weakened-test edge cases</summary>

- Comment-only, whitespace-only, test-only, and trailing-comma-only hunks (in braces, brackets, and plain calls) are ignored. `#[doc = ".."]` passed to a macro counts as a token; doc comments do not.
- Test code is kept intact when reverting production hunks (mixed hunks with unequal old/new line counts are reported as not evaluated).
- Changes inside `cfg`-disabled code are skipped when the built tokens are identical (accounting for `line!`, `column!`, `#[track_caller]`, `RUSTFLAGS`, and build scripts).
- Hunks that fail to compile alone are retried together per file (and across files if needed).
- Changes are tried even where no probe site exists (e.g. constants), but are not tried with `--shard` or `--list`. In JSON they appear as `kind: "survivor"` with `change_since`.
- Weakened-test detection needs no `--rebuild` (costs two extra builds only when tests changed), skips pre-existing gaps, falls back to changed lines if old tests do not compile/pass on new code, and does not trace standalone `tests/*.rs` files back to library files.

</details>

---

## CLI & workflows

Run `cargo tmt -h` for a compact overview or `cargo tmt --help` for full descriptions, examples, and exit codes.

### Common recipes

```sh
# Current package (fast probe mode)
cargo tmt

# Pull request diff with source builds and verified witnesses
cargo tmt --rebuild --since "$(git merge-base origin/main HEAD)"

# Workspace streaming (preserves completed packages if one fails)
cargo tmt --workspace --format jsonl

# GitHub Code Scanning / SARIF 2.1.0
cargo tmt --format sarif > tmt.sarif

# CI sharding in source-only mode
cargo tmt --workspace --source-only --shard 0/4 --format json

# Migrating a cargo-mutants + nextest CI job
cargo tmt --source-only --literal-operators --test-tool nextest --nextest-profile ci

# Preview source edits without building or running tests
cargo tmt --list --format json
```

### Exit codes

| Code | Meaning |
|---|---|
| **`0`** | No gaps found (also `--help` and `--version`) |
| **`1`** | Analysis or CLI error (invalid arguments, baseline tests failed, build error) |
| **`2`** | Test gaps, unstable predictions, or `--audit` discrepancies found |
| **`3`** | Incomplete evaluation (unconfirmed parallel timeouts, no mutants executed, or `--source-only` timeout/unevaluated) |

### Option reference

Options are grouped into the same six workflow sections shown by `cargo tmt --help`:

#### Target & Scope

| Option | Description |
|---|---|
| `[DIR]` / `-d, --dir <DIR>` | Directory of the Cargo package or workspace (default: `.`) |
| `-p, --package <PACKAGE>` | Workspace member to analyse (repeatable or comma-separated) |
| `--workspace` | Analyse all workspace members |
| `--since <REV>` | Analyse only code changed since `REV`, plus untracked files |
| `--lines <FILE:FROM-TO>` | Analyse only specific lines (`FILE:FROM-TO`, `FILE:LINE`, or `FILE`, relative to each package; repeatable) |

#### Mutation Mode

| Option | Description |
|---|---|
| `--rebuild` | Also build source-only mutants and synthesize/verify witnesses |
| `--source-only` | Build every supported mutant directly from source (use when tests clear env vars via `Command::env_clear()`) |
| `--list` | Preview source mutation candidates without building or running tests |
| `--error <EXPR>` | Also replace `Result` returns with `Err(EXPR)` (repeatable; e.g. `--error '"mutated".into()'`) |
| `--literal-operators` | Replace only the operator token without parenthesizing operands (`--source-only`) |
| `--shard <K/N>` | Select zero-based shard `K/N` (`--source-only`) |
| `--sharding <slice\|round-robin>` | Candidate distribution across shards (default: `slice`) |
| `--no-verify` | Trust the trace without re-running tests against predicted survivors |
| `--no-effects` | Skip effect tracing on surviving mutants |
| `--audit <N>` | Self-check: re-run up to `N` survivors against all tests, rebuild & compare up to `N` probe mutants from source, and re-record traces to catch nondeterminism |

#### Test Execution & Selection

| Option | Description |
|---|---|
| `--also-test <PACKAGE>` | Also run tests from another workspace package (repeatable) |
| `--dependents` | Also run tests of every workspace package depending on the analysed one |
| `--test-workspace[=<BOOL>]` | Run tests from all workspace members against every mutation |
| `--doc` | Also run doc tests (recompiled per reached mutant; slower) |
| `--test-tool <cargo\|nextest>` | Select runner (`nextest` requires `--source-only`, Unix) |
| `--nextest-profile <PROFILE>` | Nextest profile to use with `--test-tool nextest` |
| `--isolate-tests` | Run one selected libtest test per process (standard libtest executables; no doc tests) |
| `--test-target <TARGET>` | Select Cargo test artifact names to run and, where possible, narrow builds (`analysis.test_selection.build_scope`) |
| `--include-test <NAME>` / `--exclude-test <NAME>` | Include/exclude exact libtest names (repeatable; enables isolation) |
| `--include-ignored` | Also allow `#[ignore]` tests |
| `--test-wrapper <PATH>` | Workspace-relative wrapper executable invoked with each test binary and its args (preserves status and `TMT_*` env) |
| `--wrapper-grace <SECONDS>` | Unix timeout SIGTERM grace period for `--test-wrapper` (default: `2`, `0` kills immediately) |

#### Output & Baselines

| Option | Description |
|---|---|
| `--format <text\|json\|jsonl\|sarif>` | Output format (default: `text`). `--json`, `--jsonl`, and `--sarif` remain as shorthand flags. |
| `--show-unreached` | Also list mutation sites that no test executed |
| `-q, --quiet` | Suppress progress output on stderr (explicit timeout warnings still print) |
| `--baseline <FILE>` | Suppress findings already recorded in `FILE` |
| `--write-baseline <FILE>` | Write this run's findings to `FILE` for future `--baseline` runs |

- **JSON schema & terminal safety:** Single-package JSON, `--list --format json`, `--workspace --format json`, and every `--format jsonl` record include a top-level `"schema_version": 1`. Terminal text output and stderr diagnostics sanitize ANSI escape sequences, control characters, BiDi overrides, and embedded newlines from untrusted project strings while keeping JSON/JSONL/SARIF lossless.
- **Inline suppressions:** Add `// tmt:ignore <reason>` on the line itself (applies to that line only) or on a standalone comment line directly above (applies to the next line).
- **Stable baseline IDs:** Baseline v2 identifies findings by `(kind, file, item, code, occurrence)` without line numbers or mutant wording, so findings survive edits and moves (legacy fingerprint files are still read). Suppressed findings do not affect exit status.
- **SARIF 2.1.0:** Emits repo-root-relative URIs, `partialFingerprints`, `properties.analysis` / `properties.audit`, and rules for input gaps, assertion gaps, survivors, unstable predictions, weakened tests, and audit disagreements. With `--workspace`, emits one run per package (`executionSuccessful: false` on member errors).

#### Performance & Cargo Build

| Option | Description |
|---|---|
| `-j, --jobs <N>` | Mutants to run concurrently (default: `1`; raise only if tests use no fixed ports or shared files) |
| `--baseline-jobs <N>` | Concurrent isolated baseline tests (default: `1`) |
| `--timeout <SECONDS>` | Per-runner timeout under a mutant (default: `5s + 5x` measured baseline; warns on stderr if baseline meets/exceeds an explicit limit) |
| `--timeout-recheck <serial\|none>` | Recheck parallel worker timeouts serially (`serial`, default) or leave unconfirmed (`none`, exits `3`) |
| `--checkpoint` | Append and resume completed outcomes via `checkpoint.jsonl` in the work dir (reuses results only when source, selection, tools, and environment match) |
| `--opt-level <0..=3>` | Override optimisation level for test builds (greatly reduces probe overhead in tight loops) |
| `-F, --features <FEATURES>` / `--all-features` / `--no-default-features` / `--locked` | Forwarded to every Cargo command in the run |

#### Work Directory & Fixtures

| Option | Description |
|---|---|
| `--work-dir <DIR>` | Isolated working copy and build directory (default: `<target-dir>/tmt`; locked throughout execution and kept between runs for incremental rebuilds) |
| `--all-files` | Also copy gitignored files (still excludes `.git`, `node_modules`, and build caches) |
| `--git-index` | Create an independent Git index in the working copy for commands like `git ls-files` |
| `--copy-fixture <PATH>` | Copy a specific workspace-relative gitignored file/directory (e.g. `sdk/node_modules`; repeatable) |
| `--copy-fixture-exclude <PATH>` | Omit an entry from `--copy-fixture` (repeatable) |
| `--copy-output-fixture <FROM=TO>` | Stage a workspace file/directory under isolated Cargo output (e.g. `tools/runner-node=debug/runner-node`), protecting unmanaged files and rejecting case-only collisions |

> Your source tree is never modified. In a Git repository, only tracked/unignored files are copied (symlinks to regular files are materialized; directory symlinks are followed only inside the workspace), Cargo intermediates stay inside the work directory even if `build.build-dir` is configured, and external path dependencies are rewritten to absolute paths.

---

## Soundness, speed & prior work

### What "guaranteed to survive" rests on

- **Deterministic tests.** Predictions come from one traced run; by default one survivor per gap is re-run and mismatches report `UNSTABLE` (exit `2`).
- **Side-effect preservation.** Probes evaluate every operand once in original order and drop temporaries where the original drops them (`fixtures/semantics` tests this). `a && { b; true }` still evaluates `b` when `a` is true.
- **Consistent `PartialOrd`.** Comparisons call `partial_cmp` once and derive `<`/`<=`/`>`/`>=` from it.
- **Mathematical integer thresholds.** `a >= b - 1` (reported as `(without overflow)`) compares mathematical values; overflow and division by zero are never assumed equal.
- **Float precision.** Float thresholds are computed in `f64` (may differ in the last ULP for `f32`).
- **Machine-checked invariants.** Core planner soundness, crash recovery, checkpointing, and process-cleanup protocols are verified with Lean 4, PICT, `proptest`, and TLA+ — see [formal/README.md](formal/README.md).

### Why it is fast

- One instrumented build carries all probe-compatible mutants, toggled by environment variables.
- Provable input-gap survivors are computed analytically instead of tested one by one.
- Remaining mutants run only against tests whose observations changed, stopping at the first failure.
- Loop-heavy probes batch narrowing distances per thread (flushed on thread exit and every 20 ms) and skip recorder locks when observations are unchanged. Using `--opt-level 1` eliminates almost all probe overhead in tight loops (measured 13× → ~1× unrecorded, 170× → 13× while recording).
- Incremental working copy rewrites only changed instrumented files, and `--since` / `--lines` narrow work to code under review.

### Compared with `cargo-mutants` & prior work

`tests/parity.rs` checks `cargo tmt` against stored `cargo-mutants` 27.1.0 mutation lists across eight fixture crates (plus any crate on request), and adds trace-driven mutations (shifted boundaries, ignored `&&`/`||` operands, post-body `bool`/`Option` exits, deleted statements, `min`/`max`, `first`/`last`, and range ends).

The idea of generating mutants using test observations is known as *test-aware mutant generation*. Related lines of work include SEMu (symbolic execution to kill surviving mutants), DSpot (assertion amplification), the W-method (complete test suites for finite state machines), and Meta's ACH (LLM-generated mutants with tests that kill them). `cargo tmt` occupies the **solver-free, AI-free** corner of that space: read the trace and do interval arithmetic. Rather than comparing raw mutation scores across tools with different mutant sets, this project optimizes for catching real bugs per minute of CI time.

---

## Limits

### Scope, syntax & types

- **Module discovery & targets:** Follows explicit `mod` declarations and literal `include!` paths. Secondary targets and modules shared by multiple targets need `--rebuild` or `--source-only` (unbuilt files are reported as unevaluated at file granularity).
- **`i128` and `u128`:** Outside the probe runtime's integer specialization (`i8`..`i64`/`isize`, `u8`..`u64`/`usize`). Comparisons fall back to general ordering checks (`<` vs `<=` and negation without shifted boundaries), and `i128`/`u128` arithmetic, bitwise, range, and `min`/`max` probes pass through unmutated. Use `--source-only` to mutate `i128`/`u128` operations directly.
- **Floats & non-numeric types:** Float gaps are reported only while no test compares equal operands (rounding-distance thresholds after that are not reported). Non-numeric `PartialOrd` types check `<` vs `<=` and negation only.
- **Compile-time & unsupported syntax:** Inline `const` blocks, array lengths, enum discriminants, const generics, `const fn`, and constant initializers require `--rebuild` or `--source-only`. Files starting with a BOM or `#!` line are not instrumented. Casts are not mutated. Functions returning `impl Trait` (other than `impl Iterator`) cannot have synthetic body replacements. Result probes for `bool`/`Option` require observable exits (omitted when `return`, `?`, or an unexpanded macro could bypass them). Macro shadowing is recognized only for top-level definitions in the same file.
- **Ranges, `min`/`max`, and `first`/`last`:** Range probes apply only directly in `for` loops (other ranges and literal pairs like `0..3` need `--rebuild`); `a..b` at `T::MAX` cannot be probed as `a..=b` and is reported as not evaluated (handled by `--rebuild`). `a.max(b)`/`a.min(b)` borrow `a` (non-`Copy` by-value types like `String` are left untouched). `v.first()`/`v.last()` target slices, `Vec`, arrays of length ≥ 2, and `BTreeSet` (by-value iterators and `&mut` receivers need `--rebuild`).
- **Duplicate/equivalent mutants:** A body replaced by `true` and an exit replaced by `true` can be reported together on the same line. Equivalent mutants (`a >= b` in `max(a, b)`, compile-time-constant operands) are not always eliminated.

### Tracing, threads & determinism

- **Worker thread pools (`tokio`, `rayon`) & spawned threads:** Observations on threads whose names do not match libtest test names — including shared worker pools such as `tokio` and `rayon`, spawned threads, and all doc tests — cannot be attributed to one test and fall back to running the mutant against the whole test binary. Use `--isolate-tests` when per-test attribution or process isolation is needed.
- **Thread name collisions:** A user-spawned thread named identically to another test in the same binary is attributed to that test (`--audit` detects this).
- **Unnamed / long-lived threads:** Unnamed threads and the main thread flush immediately (slower in loops). Named threads that are still running when the process exits may omit their last 20 ms of observations (re-run verification surfaces any resulting misprediction as `UNSTABLE`).
- **Environment clearing & `fork`:** Subprocesses clearing environment variables (`Command::env_clear()`) bypass probes (use `--source-only`). Code running in a forked child before `exec` (`pre_exec`) is not traced (to avoid deadlocking on inherited runtime locks), though active mutants still apply there.

### Evaluation semantics & probes

- **Constant replacement & struct base evaluation:** Probed constant replacements still evaluate operands/guards, and probed struct-field omission evaluates `..base` twice (`--source-only` removes the field and evaluates `base` once).
- **Drop order & `DerefMut`:** Two edge cases differ from uninstrumented code: a temporary with a `Drop` impl inside a struct field with `..base` drops before the next field, and `*p += f()` evaluates `*p` before `f()`.
- **`unused_parens` & columns:** Source operator replacements parenthesize operands when precedence changes (`8 - (2 + 3)`), which crates denying `unused_parens` may reject as non-compiling. Probes and source mutants preserve line numbers (`line!()`) but not column numbers (`column!()`).

### Process cleanup & timeouts

- **Leftover processes:** Each test run inherits a locked file; processes still holding it after the run ends are stopped (requiring `/proc` on Linux or `lsof` elsewhere). Set `TMT_LEAVE_PROCESSES=1` to opt out. Daemons that close inherited FDs (e.g. Python `subprocess` defaults) are not tracked.
- **Parallel timeouts:** With `--jobs N`, each test binary shares cores (at least 2 test threads) and parallel timeouts are rechecked serially by default (`--timeout-recheck serial`). Heavy external machine load can still cause slow mutants to time out (counted as killed in probe mode, or exit `3` in `--source-only`).

---

## Changelog & contributing

- Release history: [CHANGELOG.md](CHANGELOG.md)
- Building, testing, and releasing: [CONTRIBUTING.md](CONTRIBUTING.md)
- Formal verification models: [formal/README.md](formal/README.md)

## License

Apache License, Version 2.0. See [LICENSE](LICENSE).