pytest-probability 0.2.0__tar.gz → 0.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/.gitignore +1 -0
  2. pytest_probability-0.3.1/AGENTS.md +47 -0
  3. pytest_probability-0.3.1/CLAUDE.md +1 -0
  4. pytest_probability-0.3.1/PKG-INFO +464 -0
  5. pytest_probability-0.3.1/README.md +436 -0
  6. pytest_probability-0.3.1/agcoord.toml +16 -0
  7. pytest_probability-0.3.1/docs/bdd.md +181 -0
  8. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/docs/cases.md +2 -2
  9. pytest_probability-0.3.1/docs/changelog.md +366 -0
  10. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/docs/conf.py +2 -2
  11. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/docs/contributing.md +9 -2
  12. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/docs/cost.md +2 -2
  13. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/docs/index.md +78 -7
  14. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/docs/installation.md +2 -2
  15. pytest_probability-0.3.1/docs/internals.md +985 -0
  16. pytest_probability-0.3.1/docs/json-report.md +573 -0
  17. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/docs/parametrization.md +20 -4
  18. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/docs/quickstart.md +6 -2
  19. pytest_probability-0.3.1/docs/reference.md +1610 -0
  20. pytest_probability-0.3.1/docs/running.md +382 -0
  21. pytest_probability-0.3.1/docs/statistics.md +446 -0
  22. pytest_probability-0.3.1/gardenia.toml +8 -0
  23. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/pyproject.toml +15 -1
  24. pytest_probability-0.3.1/src/pytest_probability/__init__.py +18 -0
  25. pytest_probability-0.3.1/src/pytest_probability/explain.py +2056 -0
  26. pytest_probability-0.3.1/src/pytest_probability/plugin.py +5973 -0
  27. pytest_probability-0.3.1/src/pytest_probability/stats.py +1256 -0
  28. pytest_probability-0.3.1/tests/test_explain.py +2079 -0
  29. pytest_probability-0.3.1/tests/test_gate_items.py +744 -0
  30. pytest_probability-0.3.1/tests/test_latency.py +550 -0
  31. pytest_probability-0.3.1/tests/test_plan.py +640 -0
  32. pytest_probability-0.3.1/tests/test_plugin.py +3755 -0
  33. pytest_probability-0.3.1/tests/test_sequential.py +1112 -0
  34. pytest_probability-0.3.1/tests/test_stats.py +1153 -0
  35. pytest_probability-0.3.1/tests/test_stop.py +816 -0
  36. pytest_probability-0.2.0/PKG-INFO +0 -225
  37. pytest_probability-0.2.0/README.md +0 -201
  38. pytest_probability-0.2.0/docs/changelog.md +0 -65
  39. pytest_probability-0.2.0/docs/internals.md +0 -214
  40. pytest_probability-0.2.0/docs/json-report.md +0 -155
  41. pytest_probability-0.2.0/docs/reference.md +0 -173
  42. pytest_probability-0.2.0/docs/running.md +0 -132
  43. pytest_probability-0.2.0/src/pytest_probability/__init__.py +0 -6
  44. pytest_probability-0.2.0/src/pytest_probability/plugin.py +0 -754
  45. pytest_probability-0.2.0/tests/test_plugin.py +0 -607
  46. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/.readthedocs.yaml +0 -0
  47. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/LICENSE +0 -0
  48. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/docs/requirements.txt +0 -0
  49. {pytest_probability-0.2.0 → pytest_probability-0.3.1}/tests/conftest.py +0 -0
@@ -11,3 +11,4 @@ docs/_build/
11
11
  coverage.xml
12
12
  .idea/
13
13
  .vscode/
14
+ .claude/
@@ -0,0 +1,47 @@
1
+ # pytest-probability contributor instructions
2
+
3
+ See [README.md](README.md) and [docs/index.md](docs/index.md) (including [docs/contributing.md](docs/contributing.md) and the rest of [docs/](docs/)) for project overview, usage, and documentation.
4
+
5
+ - Keep statistical reference and test-only dependencies (`scipy`, `statsmodels`, `pytest-xdist`) strictly in `[project.optional-dependencies].test` in `pyproject.toml`, never in runtime `dependencies`.
6
+
7
+ <!-- gardenia:begin -->
8
+ - File an issue (`#N — one sentence describing the work`) for each code, test, docs, or packaging
9
+ change; work in an issue-named `/var/tmp` worktree from `origin/main`, keep the primary checkout
10
+ read-only, and keep check logs complete (never truncated).
11
+ - Verify behavior at public boundaries with focused tests (bug fixes start with a failing one) and
12
+ add canonical docs plus a `CHANGELOG.md` entry for user-facing changes. Tests stop and remove all
13
+ they create and never touch `~/.codex`, `~/.claude`, `~/.gemini`, `~/.config`, or `~/.local`.
14
+ - Docs-only changes (every path `README.md`, `CHANGELOG.md`, `AGENTS.md`, Markdown in `docs/`, or
15
+ a `docs/assets/` `.png`/`.svg` with provenance in `docs/assets/README.md`) skip tests and the
16
+ gate; verify the PR file list and diff, then merge via the forge.
17
+ - Gate and land only through `agc run` / `agc land <pr>`.
18
+ - Never put private project names, machine-local paths, or other repositories' issue links in
19
+ tracked files, commits, or pull requests.
20
+ - PyPI uploads and live host or production changes need explicit owner approval per operation.
21
+ <!-- gardenia:end -->
22
+
23
+ <!-- agrein:begin -->
24
+ Use the installed agrein skills: ticket-delivery before changing code, docs,
25
+ config, infrastructure, or live state; epic-queue when decomposing multi-ticket
26
+ work or coordinating subagent workers; bug-investigation before fixing defects or
27
+ failing checks; and owner-escalation for unsettled product/policy rulings, live
28
+ operation approvals, and decision handoffs. An assigned leaf worker implements
29
+ its ticket directly without recursively delegating it.
30
+ <!-- agrein:end -->
31
+
32
+ <!-- agroute:begin -->
33
+ Before delegating an authorized task to a worker, invoke the installed `agroute` and `howhard` skills to inspect live
34
+ quota, select an eligible route, and honor `needs_review` and the native or cross-agent execution plan; report
35
+ unavailable routing or exhausted quota instead of bypassing them. Afterward, record the truthful outcome and measured
36
+ usage in `howhard` memory. Assigned leaf workers execute their task directly, without recursive delegation.
37
+ <!-- agroute:end -->
38
+
39
+ <!-- agcoord:begin -->
40
+ When running checks or landing pull requests, follow the installed `agcoord` skill: pass a
41
+ short plain-English `--label` and integer byte resource claims with `agc run`, and keep
42
+ heavy checks portable for overflow unless they are host-bound. Where `agc repo-config` reports
43
+ repository defaults (`agcoord.toml` or `[tool.agcoord]`), let them supply claims and the
44
+ `full` or `land` gate command, and pass only what you need to change. Use one `agc land` request
45
+ for gate and publication. Never run `agc` recursively inside an admitted job or merge a
46
+ pull request directly.
47
+ <!-- agcoord:end -->
@@ -0,0 +1 @@
1
+ AGENTS.md
@@ -0,0 +1,464 @@
1
+ Metadata-Version: 2.5
2
+ Name: pytest-probability
3
+ Version: 0.3.1
4
+ Summary: pytest plugin for nondeterministic tests: run cases N times, report empirical pass fractions, flaky detection, and cost.
5
+ Project-URL: Homepage, https://github.com/xu-hao/pytest-probability
6
+ Project-URL: Repository, https://github.com/xu-hao/pytest-probability
7
+ Project-URL: Documentation, https://pytest-probability.readthedocs.io
8
+ Project-URL: Changelog, https://github.com/xu-hao/pytest-probability/blob/main/docs/changelog.md
9
+ Author: Hao Xu
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: benchmark,eval,evaluation,flaky,llm,probability,pytest
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Framework :: Pytest
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Topic :: Software Development :: Testing
21
+ Requires-Python: >=3.11
22
+ Requires-Dist: pytest>=7.4
23
+ Provides-Extra: test
24
+ Requires-Dist: pytest-xdist; extra == 'test'
25
+ Requires-Dist: scipy; extra == 'test'
26
+ Requires-Dist: statsmodels; extra == 'test'
27
+ Description-Content-Type: text/markdown
28
+
29
+ # pytest-probability
30
+
31
+ [![Documentation](https://readthedocs.org/projects/pytest-probability/badge/?version=latest)](https://pytest-probability.readthedocs.io/en/latest/)
32
+ [![PyPI](https://img.shields.io/pypi/v/pytest-probability)](https://pypi.org/project/pytest-probability/)
33
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
34
+
35
+ **A semantic bug won't reproduce in one run — and can't hide from ten.**
36
+
37
+ A pytest plugin for nondeterministic code — LLM-driven functions, model
38
+ inference, integration points, anything flaky-prone. A single-shot test lies
39
+ to you: it samples a distribution once and calls the result truth.
40
+ pytest-probability runs every case N times and reports the empirical pass
41
+ fraction per case, so `7/10 FLAKY` stops hiding inside a green checkmark.
42
+
43
+ This enables **benchmark-driven development** for semantic functions —
44
+ TDD with the instrument swapped: red is a low fraction, green is a
45
+ fraction meeting your bar, refactor means beating the incumbent prompt
46
+ on a comparison axis. When a prompt fails in production, turn the
47
+ verbatim input into a case and sample it: the `6/10 FLAKY` row *is* the
48
+ reproduction that a single playground retry would have closed as
49
+ "cannot reproduce" — and the case stays in CI forever as a regression
50
+ sentinel. The worked loop:
51
+ [Benchmark-driven development](https://pytest-probability.readthedocs.io/en/latest/bdd.html).
52
+
53
+ ```
54
+ $ pytest benchmarks/ --prob-runs=10
55
+ ...
56
+ ================================= probability ==================================
57
+ classify::is_question 10/10 [69%, 100%] $0.0020
58
+ classify::identify_pii 7/10 [35%, 93%] $0.0020 FLAKY
59
+ classify::extract_amount 0/10 [ 0%, 31%] $0.0020 FAIL
60
+ triage::refund-terse 8/10 [44%, 97%] $0.0010 FLAKY
61
+ triage::refund-chain_of_thought 10/10 [69%, 100%] $0.0040
62
+
63
+ Overall: 35/50 passed (70%)
64
+ Cost: $0.0110
65
+ Tokens: m-small 1,200 in / 80 out / 640 cached $0.0010
66
+ m-large 4,800 in / 900 out $0.0040
67
+ ```
68
+
69
+ ## Documentation
70
+
71
+ Full documentation at
72
+ **[pytest-probability.readthedocs.io](https://pytest-probability.readthedocs.io)** —
73
+ installation, a quickstart, guides for benchmark files, parametrization,
74
+ running, a statistics guide (what the intervals, gates and verdicts
75
+ mean and assume), cost accounting and the JSON report, a complete options
76
+ reference, and a developer guide covering the plugin's internals.
77
+
78
+ To build locally: `pip install -r docs/requirements.txt && sphinx-build -W -b html docs docs/_build/html`.
79
+
80
+ ## Install
81
+
82
+ ```bash
83
+ pip install pytest-probability
84
+ ```
85
+
86
+ No configuration needed — the plugin registers itself via the `pytest11`
87
+ entry point. The only dependency is pytest itself. Requires Python 3.11+
88
+ and pytest 7.4+.
89
+
90
+ ## Usage
91
+
92
+ Benchmark files are named `bench_*.py`, and every `bench_*` function in
93
+ them is a benchmark — the same convention pytest applies to `test_*`.
94
+ **A bench function is a pytest test body**: cases are stock
95
+ `@pytest.mark.parametrize`, values arrive as function arguments, and
96
+ the body asserts:
97
+
98
+ ```python
99
+ import pytest
100
+ from pytest_probability import record_cost
101
+
102
+ @pytest.mark.parametrize("text,expected", [
103
+ pytest.param("is this a question?", "question", id="is_question"),
104
+ pytest.param("my ssn is 078-05-1120", "pii", id="identify_pii",
105
+ marks=pytest.mark.fast),
106
+ ])
107
+ def bench_classify(text, expected):
108
+ answer = my_classifier(text)
109
+ record_cost(0.0002) # optional — summed into the report
110
+ assert answer == expected
111
+ ```
112
+
113
+ A run's class falls straight out of Python: a clean return **passes**,
114
+ an `AssertionError` **fails** (wrong answer — with pytest's full
115
+ assertion introspection, `assert 'other' == 'pii'`), any other
116
+ exception **errors** (broken harness). The plugin times each run
117
+ itself.
118
+
119
+ Every parameter combination is a case with its own fraction row,
120
+ namespaced by function (`classify::identify_pii`); items collect as
121
+ `bench_x.py::bench_classify::identify_pii[run3]`. `pytest.param`
122
+ names and marks individual cases; a function with no parametrize runs
123
+ as a single case. Dataset-driven suites are a comprehension away:
124
+ `[pytest.param(row, id=row["id"]) for row in load_dataset()]`.
125
+
126
+ ```bash
127
+ pytest benchmarks/ --prob-runs=10 # every case ×10
128
+ pytest benchmarks/ -k identify_pii # one case, by id
129
+ pytest benchmarks/ -m "fast and not eu" # marks, boolean expressions
130
+ pytest benchmarks/ -n 4 # parallel via pytest-xdist
131
+ ```
132
+
133
+ Module-level `setup()` / `teardown()` run once per benchmark file.
134
+
135
+ ### A/B-testing prompts (comparison axes)
136
+
137
+ Stacked decorators cross-product with pytest's exact id layout and
138
+ ordering — put the prompts (or models, or thresholds) you're comparing
139
+ on their own axis and each combination gets its own fraction row, same
140
+ inputs, same sample size, cost attached:
141
+
142
+ ```python
143
+ import pytest
144
+ from pytest_probability import record_usage
145
+
146
+ @pytest.mark.parametrize("style", ["terse", "chain_of_thought"])
147
+ @pytest.mark.parametrize("text", [
148
+ pytest.param("my card was charged twice", id="refund"),
149
+ ])
150
+ def bench_triage(text, style):
151
+ response = my_classifier(text, prompt_style=style)
152
+ record_usage(model=response.model,
153
+ input_tokens=response.input_tokens,
154
+ output_tokens=response.output_tokens,
155
+ cost=response.cost) # survives a failing assert
156
+ assert response.answer == "billing"
157
+ ```
158
+
159
+ Name the axis and each arm is compared with the baseline (the first
160
+ value; `baseline=` picks another) on the inputs both ran:
161
+
162
+ ```
163
+ $ pytest benchmarks/ --prob-runs=10 --prob-compare=style
164
+ ...
165
+ triage::refund-terse 8/10 [44%, 97%] $0.0010 FLAKY
166
+ triage::refund-chain_of_thought 10/10 [69%, 100%] $0.0040
167
+ ...
168
+ =========================== probability: comparisons ===========================
169
+ triage[style] chain_of_thought − terse +20 pp [−11, +51] p=0.47 1 paired cost ×4.0
170
+ ```
171
+
172
+ 10/10 against 8/10 looks like a win, but it is one input: the
173
+ difference is +20 points with a 95% interval from −11 to +51, and
174
+ p = 0.47 — if the prompts were equally good, a gap this big would turn
175
+ up by chance about half the time. The data can't tell yet which prompt
176
+ is better; what it does show is that chain_of_thought costs four times
177
+ as much per run. Add inputs: with 10 or more pairs the interval comes
178
+ from a paired bootstrap over inputs and p from a sign-flip test on the
179
+ per-input differences. `@pytest.mark.probability(compare="style",
180
+ margin=0.02)` makes it a gate — the arm may be at most 2 points worse
181
+ (`equivalence=True`: within ±2 points) — and `--prob-adjust=holm`
182
+ corrects p for several arms.
183
+
184
+ The same comparison guards against regressions between runs:
185
+ `--prob-baseline=main.json --prob-margin=0.05` pairs every case with
186
+ its row in an earlier `--prob-json` report and fails a function whose
187
+ pass rate may have dropped by more than 5 points; new and removed
188
+ cases are listed, not compared.
189
+
190
+ ### Gates: pass on a rate, not on every run
191
+
192
+ By default any failing run fails the session. To hold a case to a pass
193
+ rate instead, gate it:
194
+
195
+ ```python
196
+ @pytest.mark.probability(min_rate=0.9) # or --prob-min-rate=0.9 for every case
197
+ @pytest.mark.parametrize("text,expected", CASES)
198
+ def bench_classify(text, expected):
199
+ assert my_classifier(text) == expected
200
+ ```
201
+
202
+ A gated case **passes** when its whole interval lies above the bar,
203
+ **fails** when it lies entirely below, and is **undecided** (fails by
204
+ default; `--prob-undecided=pass` relaxes it) in between. Its failing
205
+ runs are reported as xfailed, so only the verdicts decide the exit
206
+ status:
207
+
208
+ ```
209
+ $ pytest benchmarks/ --prob-runs=40
210
+ ...
211
+ Overall: 80/120 passed (67%)
212
+ Gates: 1 passed, 1 failed, 1 undecided
213
+ ============================== probability: gates ==============================
214
+ classify::identify_pii 37/40 [80%, 98%] ≥90% UNDECIDED
215
+ classify::never 3/40 [ 2%, 20%] ≥90% FAIL
216
+ ```
217
+
218
+ Each gated case also gets a gate item after its last run,
219
+ `bench_classify.py::bench_classify::never[gate]`, which passes or fails
220
+ with the verdict: a failed gate shows in `-v`, is a failed testcase in
221
+ JUnit XML, and `--lf` reruns it with its runs. (Under pytest-xdist it
222
+ needs `--dist loadgroup`; `--prob-no-gate-items` leaves them out.)
223
+
224
+ `min_passes=19, runs=20` gates on a count instead; `confidence=`,
225
+ `method=` and `prior=` override the session's settings for one gate;
226
+ `pytest.param(..., marks=pytest.mark.probability(...))` gates one case.
227
+ A collection-time warning flags gates that can't pass with the runs
228
+ they have (`min_rate=0.9` needs at least 36 at 95%).
229
+
230
+ ### Function-level intervals
231
+
232
+ A function with at least 10 cases also gets a line for the function as
233
+ a whole — the mean of its per-case pass fractions, with an interval
234
+ from a seeded bootstrap that re-draws whole cases, since runs of one
235
+ input are correlated — and the session gets an `Overall` line:
236
+
237
+ ```
238
+ classify N=40 inputs × k=10 81.0% [69.5%, 90.8%] ρ=0.76
239
+ runs ×2 → interval −1% · inputs ×2 → −29% · each +$0.0400
240
+ triage N=15 inputs × k=5–10 59.3% [47.3%, 70.7%] ρ=0.09
241
+ runs ×2 → interval −15% · inputs ×2 → −29% · each +$0.0125
242
+ Overall N=55 inputs × k=5–10 75.1% [66.0%, 83.8%] ρ=0.56
243
+ runs ×2 → interval −2% · inputs ×2 → −29% · each +$0.0525
244
+ ```
245
+
246
+ With more than one run per case, ρ says how alike an input's runs are,
247
+ and the second line how much doubling the runs or the inputs would
248
+ narrow the interval, at the recorded cost. Low ρ (triage): outputs vary
249
+ from run to run, so more runs help. High ρ (classify): each input is
250
+ consistently right or wrong, so add inputs instead.
251
+
252
+ ### pass^k and pass@k
253
+
254
+ How often does an input pass three runs in a row, or at least one of
255
+ five? `--prob-metric` reports either, per case (in the JSON report),
256
+ per function and overall, with the same bootstrap interval:
257
+
258
+ ```
259
+ $ pytest benchmarks/ --prob-runs=10 --prob-metric='pass^3,pass@5'
260
+ ...
261
+ ============================= probability: metrics =============================
262
+ classify pass^3 N=12 inputs 34.3% [17.2%, 53.8%]
263
+ pass@5 N=12 inputs 99.4% [98.8%, 100.0%]
264
+ triage pass^3 N=3 inputs 69.4%
265
+ pass@5 N=2 inputs 99.8% 1 left out (fewer than 5 runs)
266
+ Overall pass^3 N=15 inputs 41.3% [22.9%, 61.2%]
267
+ pass@5 N=14 inputs 99.5% [98.9%, 99.9%] 1 left out (fewer than 5 runs)
268
+ ```
269
+
270
+ pass^k is reliability — all k runs of the same input pass — and pass@k
271
+ is best-of-k — at least one does. Each case's value is the unbiased
272
+ estimate from its runs, and cases with fewer than k runs are left out.
273
+ Quote `^` in shells that treat it specially (zsh with `extendedglob`,
274
+ Windows `cmd`).
275
+
276
+ ### Latency: a quantile with an interval, and a gate on it
277
+
278
+ Every run is timed. `max_latency=` gates a case on a quantile of its
279
+ run times (the 95th percentile unless `latency_quantile=` says
280
+ otherwise), and `--prob-latency` shows each case's quantile with a
281
+ distribution-free interval:
282
+
283
+ ```python
284
+ @pytest.mark.probability(max_latency=2.0) # p95 must be under 2s
285
+ @pytest.mark.parametrize("case", ["search", "summarize", "translate"])
286
+ def bench_api(case):
287
+ call_service(case)
288
+ ```
289
+
290
+ ```
291
+ $ pytest benchmarks/ --prob-runs=100 --prob-latency
292
+ ...
293
+ ============================= probability: latency =============================
294
+ api::search 100 runs p95 769ms [600ms, 933ms] ≤2s PASS
295
+ api::summarize 100 runs p95 2.00s [1.71s, 4.84s] ≤2s UNDECIDED
296
+ api::translate 100 runs p95 3.00s [2.60s, 3.25s] ≤2s FAIL
297
+ ```
298
+
299
+ The verdict follows the same rule as a rate gate, with lower being
300
+ better: PASS when the whole interval is below the limit, FAIL when it
301
+ is entirely above. The interval needs enough runs to have an upper end
302
+ (72 for p95 at 95%; fewer leave it open, `[600ms, —]`, and the gate
303
+ can't pass yet). A latency gate judges speed only: failing asserts
304
+ still fail the session unless the case also has a rate gate.
305
+
306
+ ### Stopping early: skip runs that can't change the verdict
307
+
308
+ Once `never` has failed 9 of its 40 runs, no remaining run can lift it
309
+ over a 90% bar. `--prob-stop=curtail` skips a gated case's remaining
310
+ runs as soon as its verdict can no longer change — so the verdicts,
311
+ and the exit status, are exactly those of running every run:
312
+
313
+ ```
314
+ $ pytest benchmarks/ --prob-runs=40 --prob-stop=curtail
315
+ ...
316
+ classify::is_question 40/40 [91%, 100%]
317
+ classify::identify_pii 37/38 [86%, 99%] FLAKY decided after 38/40
318
+ classify::never 3/12 [ 5%, 57%] FLAKY decided after 12/40
319
+
320
+ Overall: 80/90 passed (89%)
321
+ Gates: 1 passed, 1 failed, 1 undecided
322
+ Stopped: 2 cases early, saving 30 of 120 runs (25%)
323
+ ```
324
+
325
+ The skipped runs are pytest skips, not samples. A stopped case's
326
+ fraction leans toward its verdict, so function-level, metric and
327
+ comparison intervals over it are hidden, with a note. Under xdist it
328
+ needs `--dist loadgroup`, so that one worker runs all of a case's runs.
329
+
330
+ `--prob-stop=sequential` goes further: it stops a pass-rate gate as
331
+ soon as the case is clearly above or below its bar, judged on an
332
+ interval that stays valid however often it is checked (marked `seq`),
333
+ and the run count becomes the most a case may use:
334
+
335
+ ```
336
+ $ pytest benchmarks/ --prob-stop=sequential # min_rate=0.9, runs=100
337
+ ...
338
+ classify::solid 53/53 [90%, 100%] seq decided after 53/100
339
+ classify::weak 45/60 [55%, 90%] seq FLAKY decided after 60/100
340
+ ```
341
+
342
+ That interval is wider than a fixed-run one, so clear cases stop much
343
+ sooner (53 runs here, 96 under curtailment) while borderline ones take
344
+ longer; `--prob-plan` shows the trade-off before you spend anything.
345
+
346
+ ## Options
347
+
348
+ | Option | Where | Default | Meaning |
349
+ |---|---|---|---|
350
+ | `--prob-runs=N` | CLI | ini or 1 | run every case N times |
351
+ | `--prob-json=PATH` | CLI | — | write a JSON report to PATH |
352
+ | `--prob-delay=SECONDS` | CLI | ini or 0 | sleep between case executions |
353
+ | `--prob-transpose` | CLI | ini or off | run-major order: run 1 of everything, then run 2, … |
354
+ | `--prob-method=M` | CLI | ini or `exact` | row interval method: `exact` (Clopper-Pearson), `wilson`, or `bayes` |
355
+ | `--prob-confidence=LEVEL` | CLI | ini or 0.95 | two-sided level for intervals |
356
+ | `--prob-no-intervals` | CLI | ini or shown | hide the interval column |
357
+ | `--prob-min-rate=RATE` | CLI | ini or none | gate every case on its pass rate |
358
+ | `--prob-undecided={fail,pass}` | CLI | ini or `fail` | whether an UNDECIDED gate fails the session |
359
+ | `--prob-explain` | CLI | ini or off | add a plain-language reading of the results (terminal and JSON) |
360
+ | `--prob-bootstrap=N` | CLI | ini or 5000 | bootstrap resamples for function-level and overall intervals |
361
+ | `--prob-seed=SEED` | CLI | ini or 0 | seed for every resampling procedure |
362
+ | `--prob-compare=AXIS` | CLI | ini or none | compare the values of parametrize argument AXIS, each against the first |
363
+ | `--prob-adjust=M` | CLI | ini or `none` | adjust comparison p-values: `none` (exploratory), `holm`, `bonferroni`, `bh` |
364
+ | `--prob-metric=M` | CLI | ini or none | also report `pass^K` (all K runs of an input pass: reliability) and/or `pass@K` (at least one of K passes: best-of-K); comma-separated, repeatable |
365
+ | `--prob-plan` | CLI | off | print a run budget per case (runs to pass a gate, to catch a flake, projected cost) and exit without running |
366
+ | `--prob-plan-assume=RATE` | CLI | 0.97 | true pass rate `--prob-plan` plans for |
367
+ | `--prob-plan-flake=RATES` | CLI | `0.1,0.01` | failure rates `--prob-plan` plans to catch |
368
+ | `--prob-plan-report=PATH` | CLI | — | previous JSON report to read cost per run from, for `--prob-plan` |
369
+ | `--prob-baseline=PATH` | CLI | none | compare every function with an earlier `--prob-json` report, pairing cases by id |
370
+ | `--prob-margin=MARGIN` | CLI | none (report only) | with `--prob-baseline`: fail a function whose pass rate may have dropped by more than MARGIN |
371
+ | `--prob-latency` | CLI | ini or off | show each case's latency quantile and its interval |
372
+ | `--prob-stop={off,curtail,sequential}` | CLI | ini or `off` | `curtail`: skip a gated case's remaining runs once its verdict can no longer change (same verdicts); `sequential`: judge rate gates on an anytime-valid interval and stop as soon as it clears or falls below the bar (`runs` becomes a maximum). Under xdist both need `--dist loadgroup` |
373
+ | `--prob-no-gate-items` | CLI | ini or added | don't add a `[gate]` item per gated case (and `[compare:ARM]`/`[baseline]` per margin) carrying its verdict into `-v`, JUnit XML and `--lf` |
374
+ | `prob_delay` | ini | 0 | default for `--prob-delay` |
375
+ | `prob_transpose` | ini | false | default for `--prob-transpose` |
376
+ | `prob_runs` | ini | 1 | default for `--prob-runs` |
377
+ | `prob_pattern` | ini | `bench_*.py` | glob for benchmark files |
378
+ | `prob_method` | ini | `exact` | default for `--prob-method` |
379
+ | `prob_confidence` | ini | 0.95 | default for `--prob-confidence` |
380
+ | `prob_prior` | ini | `1,1` | Beta prior `a,b` for `bayes` |
381
+ | `prob_intervals` | ini | true | show the interval column |
382
+ | `prob_min_rate` | ini | — | default for `--prob-min-rate` |
383
+ | `prob_undecided` | ini | `fail` | default for `--prob-undecided` |
384
+ | `prob_errors` | ini | `count` | errored runs in gated cases: `count` as non-passes, or `exclude` |
385
+ | `prob_explain` | ini | false | default for `--prob-explain` |
386
+ | `prob_bootstrap` | ini | 5000 | default for `--prob-bootstrap` |
387
+ | `prob_seed` | ini | 0 | default for `--prob-seed` |
388
+ | `prob_min_inputs` | ini | 10 | fewest cases a function needs for its function-level interval (and fewest paired inputs for a comparison's) |
389
+ | `prob_compare` | ini | — | default for `--prob-compare` |
390
+ | `prob_adjust` | ini | `none` | default for `--prob-adjust` |
391
+ | `prob_metric` | ini | — | default for `--prob-metric` (comma- or space-separated) |
392
+ | `prob_latency` | ini | false | default for `--prob-latency` |
393
+ | `prob_latency_quantile` | ini | 0.95 | latency quantile for cases whose mark sets none |
394
+ | `prob_stop` | ini | `off` | default for `--prob-stop` |
395
+ | `prob_gate_items` | ini | true | add gate items; `false` is `--prob-no-gate-items` |
396
+ | `@pytest.mark.probability(...)` | marker | — | per-function/case gate (`min_rate` or `min_passes`), `runs`, `confidence`, `method`, `prior`; per-function comparison (`compare`, `baseline`, `margin`, `equivalence`); latency (`latency_quantile`, `max_latency`) |
397
+
398
+ ## JSON report
399
+
400
+ `--prob-json=report.json` writes a single machine-readable file (think
401
+ `--junitxml`, but for fractions) — aggregate rows plus the raw per-run
402
+ step records for downstream analysis:
403
+
404
+ ```json
405
+ {
406
+ "created": "2026-07-07T21:43:18",
407
+ "runs": 10,
408
+ "exit_status": 1,
409
+ "totals": {"passes": 35, "fails": 15, "errors": 0, "count": 50,
410
+ "pass_rate": 70.0, "cost": 0.011,
411
+ "usage": {"m-small": {"input_tokens": 1200, "output_tokens": 80,
412
+ "cached_input_tokens": 640, "cost": 0.001}}},
413
+ "rows": [
414
+ {"case": "classify::identify_pii", "label": "classify", "passes": 7,
415
+ "fails": 3, "errors": 0, "total": 10, "pass_rate": 70.0,
416
+ "status": "flaky", "cost": 0.002, "usage": {}}
417
+ ],
418
+ "records": [
419
+ {"case": "classify::identify_pii", "run": 3, "steps": [
420
+ {"label": "classify", "passed": false, "elapsed": 0.01, "error": null,
421
+ "message": "got 'other'", "cost": 0.0002, "usage": []}]}
422
+ ]
423
+ }
424
+ ```
425
+
426
+ Under pytest-xdist the controller writes the file with the full result
427
+ set; workers never write partial reports.
428
+
429
+ ## Semantics
430
+
431
+ - One pytest item per **(function, case, run)** —
432
+ `bench_x.py::bench_classify::identify_pii[run3]`. `-k`, `-x`, `--lf`,
433
+ JUnit XML, and xdist all operate per run. A gated case adds one
434
+ `…::identify_pii[gate]` item carrying its verdict.
435
+ - A failing step fails that run's item with a compact message (no traceback
436
+ noise); an exception in a bench function is a normal pytest failure *and*
437
+ counts as an `error` step in the aggregate.
438
+ - Runs are a three-class outcome — pass, fail, error — and the row status
439
+ names the combination: `FLAKY` is reserved for genuine pass/fail
440
+ nondeterminism, while passes mixed with errors show `7/10 3 ERRORED`
441
+ (the model never answered wrong; the harness dropped runs). Fractions
442
+ are always over all runs.
443
+ - Selection is pure pytest: `-k` over composed case ids, `-m` over marks
444
+ (with and/or/not), node ids for single runs. Note pytest's `-k` grammar
445
+ rejects `=` — select on id substrings and mark names.
446
+ - Steps are consumed by duck-typing: anything with `label`, `passed`, and
447
+ optionally `elapsed` / `error` / `message` / `cost` / `usage` works,
448
+ including types from other frameworks. `usage` entries (objects or plain
449
+ dicts with `model` and token fields) aggregate per model into the
450
+ `Tokens:` block and the JSON report.
451
+ - The fraction summary is computed from `report.user_properties`, so it
452
+ stays correct under pytest-xdist (workers serialize results back to the
453
+ controller, which renders the table).
454
+ - Exit code follows pytest: any failed or errored run fails the session,
455
+ so a flaky case exits nonzero. For softer CI gates (e.g. alert only
456
+ below 80%), gate the case: `@pytest.mark.probability(min_rate=0.8)` or
457
+ `--prob-min-rate=0.8`. Then the verdict, not each run, decides.
458
+ - `--prob-delay` throttles between executions and never sleeps before the
459
+ first; under pytest-xdist each worker throttles its own stream.
460
+ - Default order is case-major (`alpha[run1]`, `alpha[run2]`, `beta[run1]`,
461
+ …); `--prob-transpose` makes it run-major (`alpha[run1]`, `beta[run1]`,
462
+ `alpha[run2]`, …), which spreads each case's runs over time — useful
463
+ when back-to-back runs would hit the same transient state. Ordering is
464
+ advisory under xdist, which schedules items across workers itself.