skip to content

A Rust proptest run reports `minimal failing input: capacity = 1, refill_ms = 1, idle_ms = 1` and writes `proptest-regressions/lib.txt`: how did it shrink, and what does that file hold?

level: seniorimportance: should knowfreq 22%

answer

  1. simplify while it still fails
  2. binary search clamped to the range
  3. a seed, not the value
  4. replayed before any new case
  5. commit it, then pin a test

basics

~20 s

proptest shrank by simplifying each input's value tree, binary-searching integers toward each range's low end while the case kept failing. The file stores that case's RNG seed, replayed before new cases on every run, so commit it.

solid answer

~40 s

proptest does not re-roll smaller random inputs. Each strategy produces a value tree that can `simplify()` and `complicate()`: integer ranges binary-search toward zero but never below the range's low end, and a `Vec` first deletes elements and then shrinks the ones left. The runner keeps a simplification only while the body still fails, which is how `capacity`, `refill_ms` and `idle_ms` all reached 1, the planted cap bug's smallest witness. The `cc <hex>` line in `proptest-regressions/lib.txt` is the RNG seed of that case, with the shrunk value as a comment; every later run replays saved seeds before generating new cases, so commit the file. A seed regenerates whatever the current strategy makes of it, so copy the minimal values into an ordinary `#[test]` as well.

code

plaintext · 10 lines
plaintext
proptest: Saving this and future failures in .../limiter/proptest-regressions/lib.txt
proptest: If this test was run on a CI system, you may wish to add the following line to your copy of the file. (You may need to create it.)
cc 21705668f69c0c2e984c84edbb32d73663cb5482c6ce0de214bae05c9cb77536

thread 'tests::burst_never_exceeds_capacity' panicked at src/lib.rs:121:5:
Test failed: burst 2 > capacity 1 at src/lib.rs:135.
minimal failing input: capacity = 1, refill_ms = 1, idle_ms = 1
	successes: 0
	local rejects: 0
	global rejects: 0

go deeper

for a junior

Recall that proptest reports a minimal failing input after shrinking, and that it saves failures in a proptest-regressions file that later runs replay.

for a middle

Explain that strategies produce value trees that simplify toward each range's low end, and that the file holds RNG seeds replayed before new cases.

for a senior

Act on a CI failure: recover the printed seed, commit the regression file, reproduce locally, and pin the shrunk values in an ordinary test because a seed breaks when the strategy changes.

for a principal

Decide how the team treats regression files and case counts in CI, and when a nightly deep run with more cases and shrink iterations is worth its time.

## Generation and shrinking are one structure In the **proptest** crate a strategy does not hand back a bare value. It hands back a **value tree**, an object implementing the `ValueTree` trait with three methods: `current()` returns the value under test, `simplify()` moves to a simpler candidate, and `complicate()` partially undoes the last simplification, stepping back toward the last value that failed. Shrinking is a search over that tree: 1. Run the body on `current()`. It failed, so remember this value. 2. Call `simplify()` and run the body again. 3. If the body still fails, keep the simpler value and go back to step 2. 4. If it passes, call `complicate()` to back off part of the way and try a different simplification. 5. Stop when nothing simpler fails, or when a limit is hit, and report the last failing value as the `minimal failing input`. Because the tree comes from the strategy, shrinking respects the strategy's constraints. A value drawn from `5u32..=20` can shrink to 5 but never to 0; when the same planted bug was re-run with that range, proptest reported `capacity = 5`. ## How each strategy simplifies | Strategy | Simplification | |---|---| | non-negative integer range such as `1u64..=1_000` | binary search toward zero, clamped to the range's low end | | `any::<T>()` for an integer | binary search toward zero | | `prop::collection::vec(s, 1..50)` | delete elements down to the minimum length, then shrink each one | | tuple, or `prop_map` over a tuple | shrink each component; `prop_map` re-applies its function | | `prop_filter` | only to values that still pass the filter | The search is bounded. `max_shrink_iters` defaults to four times the case count, 1,024 iterations at the default 256 cases, and `max_shrink_time` defaults to 0, meaning no time limit. The search is also greedy, so the reported input is a local minimum: nothing proptest tried from there was simpler and still failing, which usually, but not always, matches what a person would call the smallest case. ## What the regression file holds When a case fails, proptest's default persistence, `FileFailurePersistence::SourceParallel("proptest-regressions")`, walks up from the test's source file to the directory holding `lib.rs` or `main.rs` and writes a parallel file beside it: a test in `src/lib.rs` gets `proptest-regressions/lib.txt`. A property in `tests/limits.rs` has no `lib.rs` above it, so proptest prints a warning and falls back to `tests/limits.proptest-regressions`. - Each line is `cc <hex seed> # shrinks to ...`. Only the seed is read back; the comment is there for people. - On every later run the runner **replays the saved seeds first**, before it generates any new case. That turns a lucky one-off failure into one that reproduces on every machine. - The file's own header says it is recommended to check it in to source control so that everyone benefits from the saved cases. Ignoring it in version control throws that protection away. - On an ephemeral CI runner the file is lost with the machine, which is why proptest also prints the `cc` line with a note to add it to your copy of the file. ## A seed is not a value The seed replays the random number generator, not the input. Change the strategy and the same seed generates something else. When the capacity strategy changed from `1u32..=20` to `5u32..=20`, the replayed seed generated a different case, which shrank to `capacity = 5`; it still failed only because almost every input triggers this bug. Two habits follow: - Commit the regression file so that the exact failure replays while the strategy is unchanged. - Copy the minimal input into an ordinary `#[test]` with the values written out, which keeps working after any strategy edit. That habit belongs to property testing in general; what is particular here is that proptest's file alone cannot guarantee it. ## quickcheck, in one comparison The **quickcheck** crate shrinks by type: a failing value is passed to its type's `Arbitrary::shrink`, which returns an iterator of smaller values and defaults to an empty one. A newtype that bounds a capacity to 1..=20 therefore does not shrink at all unless you write `shrink` yourself, and quickcheck 1.1 then reported `[quickcheck] TEST FAILED. No Arguments Provided`, because it records arguments only for shrunk failures. A plain `u32` does shrink, but by its type alone, ignoring any bound the property applies to it. proptest's per-strategy trees avoid both problems; its README names the price, generation of complex values that can be up to an order of magnitude slower because the intermediate states must be kept.

  • Why might the input proptest calls minimal not be the smallest failing input?
    Shrinking is a greedy search with limits. It accepts each simplification that keeps failing and never revisits earlier choices, so it can stop at a local minimum, and it gives up after `max_shrink_iters`, by default four times the case count. Raising the limit, or reshaping the strategy so the simple cases are easy to reach, usually gets a smaller witness.
  • The CI runner that found the failure is gone, and its regression file with it. How do you keep the case?
    proptest prints the `cc` seed line with a note to add it to your copy of the file when running on CI. Paste that line into the matching file under `proptest-regressions/`, commit it, and every run replays it before generating new cases. Then pin the shrunk values in an ordinary unit test, since the seed only reproduces while the strategy stays the same.
  • Where does the regression file go for a property test in tests/limits.rs?
    The default `SourceParallel` persistence looks for a directory containing `lib.rs` or `main.rs` above the source file. From `tests/` it finds none, prints a warning, and falls back to writing beside the source: `tests/limits.proptest-regressions`. That file should be committed just like `proptest-regressions/lib.txt`.

Trimming a crash report to its shortest recipe: delete one step at a time, keep each deletion only while the crash still happens, and stop when every further cut makes it disappear.

  • Shrinking re-runs the generator with smaller random numbers until one fails
  • The regression file stores the failing value, so it survives strategy changes
  • proptest-regressions is a build artefact to list in .gitignore
  • Shrinking may step outside a strategy's range to find a smaller value
  • quickcheck shrinks a hand-written Arbitrary newtype without extra code