flaky-anything 0.1.0__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 (102) hide show
  1. flaky_anything-0.1.0/.github/workflows/ci.yml +25 -0
  2. flaky_anything-0.1.0/.gitignore +21 -0
  3. flaky_anything-0.1.0/LICENSE +21 -0
  4. flaky_anything-0.1.0/PKG-INFO +177 -0
  5. flaky_anything-0.1.0/README.md +150 -0
  6. flaky_anything-0.1.0/assets/demo.gif +0 -0
  7. flaky_anything-0.1.0/pyproject.toml +79 -0
  8. flaky_anything-0.1.0/src/flaky_anything/__init__.py +5 -0
  9. flaky_anything-0.1.0/src/flaky_anything/cli.py +405 -0
  10. flaky_anything-0.1.0/src/flaky_anything/config.py +80 -0
  11. flaky_anything-0.1.0/src/flaky_anything/diagnose.py +514 -0
  12. flaky_anything-0.1.0/src/flaky_anything/errors.py +8 -0
  13. flaky_anything-0.1.0/src/flaky_anything/fix/__init__.py +0 -0
  14. flaky_anything-0.1.0/src/flaky_anything/fix/apply.py +96 -0
  15. flaky_anything-0.1.0/src/flaky_anything/fix/backends.py +193 -0
  16. flaky_anything-0.1.0/src/flaky_anything/fix/context.py +150 -0
  17. flaky_anything-0.1.0/src/flaky_anything/fix/guards.py +345 -0
  18. flaky_anything-0.1.0/src/flaky_anything/fix/loop.py +168 -0
  19. flaky_anything-0.1.0/src/flaky_anything/fix/mutate.py +227 -0
  20. flaky_anything-0.1.0/src/flaky_anything/fix/playbooks.md +61 -0
  21. flaky_anything-0.1.0/src/flaky_anything/fix/prompt.py +77 -0
  22. flaky_anything-0.1.0/src/flaky_anything/hints.py +200 -0
  23. flaky_anything-0.1.0/src/flaky_anything/hunt.py +352 -0
  24. flaky_anything-0.1.0/src/flaky_anything/knobs/__init__.py +1 -0
  25. flaky_anything-0.1.0/src/flaky_anything/knobs/base.py +41 -0
  26. flaky_anything-0.1.0/src/flaky_anything/knobs/clock.py +127 -0
  27. flaky_anything-0.1.0/src/flaky_anything/knobs/order.py +37 -0
  28. flaky_anything-0.1.0/src/flaky_anything/knobs/registry.py +45 -0
  29. flaky_anything-0.1.0/src/flaky_anything/knobs/seeds.py +47 -0
  30. flaky_anything-0.1.0/src/flaky_anything/knobs/tz.py +41 -0
  31. flaky_anything-0.1.0/src/flaky_anything/report.py +164 -0
  32. flaky_anything-0.1.0/src/flaky_anything/repro.py +130 -0
  33. flaky_anything-0.1.0/src/flaky_anything/runner.py +158 -0
  34. flaky_anything-0.1.0/src/flaky_anything/selection.py +137 -0
  35. flaky_anything-0.1.0/src/flaky_anything/signatures.py +89 -0
  36. flaky_anything-0.1.0/src/flaky_anything/stats.py +46 -0
  37. flaky_anything-0.1.0/src/flaky_anything/store.py +230 -0
  38. flaky_anything-0.1.0/src/flaky_anything/trial_plugin.py +196 -0
  39. flaky_anything-0.1.0/src/flaky_anything/verify.py +124 -0
  40. flaky_anything-0.1.0/tests/corpus/ground_truth.toml +93 -0
  41. flaky_anything-0.1.0/tests/corpus/project/corpuslib/__init__.py +1 -0
  42. flaky_anything-0.1.0/tests/corpus/project/corpuslib/billing.py +13 -0
  43. flaky_anything-0.1.0/tests/corpus/project/corpuslib/cache.py +24 -0
  44. flaky_anything-0.1.0/tests/corpus/project/corpuslib/ids.py +18 -0
  45. flaky_anything-0.1.0/tests/corpus/project/corpuslib/scheduling.py +32 -0
  46. flaky_anything-0.1.0/tests/corpus/project/corpuslib/tags.py +13 -0
  47. flaky_anything-0.1.0/tests/corpus/project/pyproject.toml +4 -0
  48. flaky_anything-0.1.0/tests/corpus/project/tests/test_order.py +36 -0
  49. flaky_anything-0.1.0/tests/corpus/project/tests/test_seeds.py +17 -0
  50. flaky_anything-0.1.0/tests/corpus/project/tests/test_stable.py +38 -0
  51. flaky_anything-0.1.0/tests/corpus/project/tests/test_time.py +31 -0
  52. flaky_anything-0.1.0/tests/e2e/test_cli_e2e.py +76 -0
  53. flaky_anything-0.1.0/tests/integration/sanity_plugin.py +30 -0
  54. flaky_anything-0.1.0/tests/integration/test_corpus_sanity.py +130 -0
  55. flaky_anything-0.1.0/tests/integration/test_diagnose.py +232 -0
  56. flaky_anything-0.1.0/tests/integration/test_fix_apply.py +103 -0
  57. flaky_anything-0.1.0/tests/integration/test_fix_loop.py +176 -0
  58. flaky_anything-0.1.0/tests/integration/test_fix_mutate_run.py +63 -0
  59. flaky_anything-0.1.0/tests/integration/test_hunt_corpus.py +28 -0
  60. flaky_anything-0.1.0/tests/integration/test_hunt_determinism.py +38 -0
  61. flaky_anything-0.1.0/tests/integration/test_hunt_phase_a.py +41 -0
  62. flaky_anything-0.1.0/tests/integration/test_m2_acceptance.py +43 -0
  63. flaky_anything-0.1.0/tests/integration/test_m3_acceptance.py +106 -0
  64. flaky_anything-0.1.0/tests/integration/test_repro_check.py +88 -0
  65. flaky_anything-0.1.0/tests/integration/test_runner.py +173 -0
  66. flaky_anything-0.1.0/tests/integration/test_selection_collect.py +82 -0
  67. flaky_anything-0.1.0/tests/integration/test_trial_plugin.py +175 -0
  68. flaky_anything-0.1.0/tests/integration/test_verify.py +91 -0
  69. flaky_anything-0.1.0/tests/unit/snapshots/pr.md +31 -0
  70. flaky_anything-0.1.0/tests/unit/snapshots/report.md +55 -0
  71. flaky_anything-0.1.0/tests/unit/test_cli_diagnose.py +136 -0
  72. flaky_anything-0.1.0/tests/unit/test_cli_hunt.py +115 -0
  73. flaky_anything-0.1.0/tests/unit/test_config.py +82 -0
  74. flaky_anything-0.1.0/tests/unit/test_diagnose_classify.py +55 -0
  75. flaky_anything-0.1.0/tests/unit/test_diagnose_steps.py +345 -0
  76. flaky_anything-0.1.0/tests/unit/test_errors.py +33 -0
  77. flaky_anything-0.1.0/tests/unit/test_find_root.py +48 -0
  78. flaky_anything-0.1.0/tests/unit/test_fix_backends.py +230 -0
  79. flaky_anything-0.1.0/tests/unit/test_fix_context.py +138 -0
  80. flaky_anything-0.1.0/tests/unit/test_fix_guards.py +256 -0
  81. flaky_anything-0.1.0/tests/unit/test_fix_mutate.py +90 -0
  82. flaky_anything-0.1.0/tests/unit/test_fix_prompt.py +94 -0
  83. flaky_anything-0.1.0/tests/unit/test_ground_truth.py +52 -0
  84. flaky_anything-0.1.0/tests/unit/test_hints.py +128 -0
  85. flaky_anything-0.1.0/tests/unit/test_hunt.py +91 -0
  86. flaky_anything-0.1.0/tests/unit/test_hunt_budget.py +125 -0
  87. flaky_anything-0.1.0/tests/unit/test_hunt_report.py +111 -0
  88. flaky_anything-0.1.0/tests/unit/test_knob_base.py +51 -0
  89. flaky_anything-0.1.0/tests/unit/test_knob_clock.py +111 -0
  90. flaky_anything-0.1.0/tests/unit/test_knob_order.py +47 -0
  91. flaky_anything-0.1.0/tests/unit/test_knob_registry.py +41 -0
  92. flaky_anything-0.1.0/tests/unit/test_knob_seeds.py +33 -0
  93. flaky_anything-0.1.0/tests/unit/test_knob_tz.py +21 -0
  94. flaky_anything-0.1.0/tests/unit/test_report.py +177 -0
  95. flaky_anything-0.1.0/tests/unit/test_repro.py +48 -0
  96. flaky_anything-0.1.0/tests/unit/test_runner_args.py +6 -0
  97. flaky_anything-0.1.0/tests/unit/test_selection.py +64 -0
  98. flaky_anything-0.1.0/tests/unit/test_signatures.py +128 -0
  99. flaky_anything-0.1.0/tests/unit/test_smoke.py +16 -0
  100. flaky_anything-0.1.0/tests/unit/test_stats.py +76 -0
  101. flaky_anything-0.1.0/tests/unit/test_store.py +104 -0
  102. flaky_anything-0.1.0/uv.lock +1708 -0
@@ -0,0 +1,25 @@
1
+ name: ci
2
+ on:
3
+ push:
4
+ branches: [main]
5
+ pull_request:
6
+
7
+ jobs:
8
+ test:
9
+ runs-on: ubuntu-latest
10
+ strategy:
11
+ fail-fast: false
12
+ matrix:
13
+ python: ["3.10", "3.13"]
14
+ steps:
15
+ - uses: actions/checkout@v7
16
+ - uses: astral-sh/setup-uv@v10.4.0 # upstream has no moving v10 tag
17
+ with:
18
+ python-version: ${{ matrix.python }}
19
+ - run: uv sync --all-extras
20
+ - run: uv run ruff check .
21
+ - run: uv run ruff format --check .
22
+ - run: uv run mypy src
23
+ - run: uv run pytest --cov=flaky_anything
24
+ - name: e2e against the flaky corpus
25
+ run: uv run pytest tests/e2e -m e2e
@@ -0,0 +1,21 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ .venv/
4
+ dist/
5
+ build/
6
+ *.egg-info/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
10
+ .coverage
11
+ htmlcov/
12
+ .flaky-anything/
13
+ .env
14
+ .env.*
15
+
16
+ # local planning kit (not published)
17
+ CLAUDE.md
18
+ PLAN.md
19
+ START_HERE.md
20
+ docs/
21
+ .claude/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yasas Sri
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,177 @@
1
+ Metadata-Version: 2.5
2
+ Name: flaky-anything
3
+ Version: 0.1.0
4
+ Summary: Your flaky test isn't random. Find why it fails, get a deterministic repro, and a verified fix.
5
+ Project-URL: Homepage, https://github.com/Yasas-Sri/flaky-anything
6
+ Project-URL: Issues, https://github.com/Yasas-Sri/flaky-anything/issues
7
+ Author: Yasas
8
+ License: MIT
9
+ License-File: LICENSE
10
+ Keywords: agents,ci,debugging,flaky tests,pytest,testing
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Framework :: Pytest
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Topic :: Software Development :: Testing
17
+ Requires-Python: >=3.10
18
+ Requires-Dist: pydantic>=2.6
19
+ Requires-Dist: pytest>=7.4
20
+ Requires-Dist: rich>=13
21
+ Requires-Dist: time-machine>=2.14
22
+ Requires-Dist: typer>=0.12
23
+ Requires-Dist: tzdata; sys_platform == 'win32'
24
+ Provides-Extra: agent
25
+ Requires-Dist: anthropic>=0.40; extra == 'agent'
26
+ Description-Content-Type: text/markdown
27
+
28
+ # flaky-anything
29
+
30
+ **Your flaky test isn't random. Here's why it fails.**
31
+
32
+ flaky-anything turns a test that fails "sometimes" into a command that fails **every time**,
33
+ tells you the hidden input that causes it, and hands you a fix that's verified not to weaken the test.
34
+
35
+ ![hunt → diagnose → repro (fails 20/20) → fix → verify (0/500, rate < 0.60% at 95%): a real session on the bundled corpus, waits shortened to 1s](https://raw.githubusercontent.com/Yasas-Sri/flaky-anything/main/assets/demo.gif)
36
+
37
+ ```console
38
+ $ flaky-anything diagnose tests/test_order.py::test_lookup_empty_cache
39
+ tests/test_order.py::test_lookup_empty_cache → order:victim
40
+ Replayed config 5f9f450e38e620b8 (a subset of e25589614a1db3d9: the victim alone or with its file):
41
+ failed 10/10.
42
+ needs order: as listed
43
+ polluter: tests/test_order.py::test_warm_cache (runs first, breaks it)
44
+
45
+ $ flaky-anything repro tests/test_order.py::test_lookup_empty_cache
46
+ PYTHONHASHSEED=0 TZ=UTC pytest -p no:randomly tests/test_order.py::test_warm_cache tests/test_order.py::test_lookup_empty_cache
47
+ fails 20/20 (run verbatim just now)
48
+ ```
49
+
50
+ ## Install
51
+
52
+ Python 3.10+, any pytest project.
53
+
54
+ ```bash
55
+ uv tool install flaky-anything # or: pipx install flaky-anything
56
+ uv tool install 'flaky-anything[agent]' # + the Anthropic SDK for `fix` with Claude
57
+ ```
58
+
59
+ It runs your tests with your project's interpreter (`.venv` by default, or `--python`).
60
+
61
+ ## Quick start
62
+
63
+ ```bash
64
+ cd your-project
65
+ flaky-anything hunt --budget 10m # find flaky tests
66
+ flaky-anything diagnose <nodeid> # why it fails: category, minimal cause, polluter
67
+ flaky-anything repro <nodeid> # a plain pytest command that fails, re-run 20× to prove it
68
+ flaky-anything fix <nodeid> # model patch → guards → verify, in a git worktree
69
+ flaky-anything verify <nodeid> # 500 perturbed runs + a confidence bound
70
+ flaky-anything report # every diagnosis and fix, as markdown
71
+ ```
72
+
73
+ Every command takes `--json`, for scripts and agents. `--jobs N` runs trials in parallel.
74
+
75
+ `hunt` prints each flaky test with its failure rate over the configurations it tried, a
76
+ failure signature, and the configuration that failed:
77
+
78
+ ```text
79
+ test verdict failed fail rate (95% CI)
80
+ tests/test_order.py::test_lookup_empty_cache flaky 4/32 12% (5.0%-28%)
81
+ tests/test_time.py::test_next_month_invoice flaky 4/35 11% (4.5%-26%)
82
+ ...
83
+ 87 trials, 20 tests, seed 0, knobs order,hash_seed,random_seed,tz,clock.
84
+ ```
85
+
86
+ Feed it your CI history to start with the tests that already flake: `hunt --from-junit report.xml`
87
+ (repeat the option for more reports).
88
+
89
+ ### Fixing
90
+
91
+ `fix` asks a model for a patch, then accepts it only if it passes, in order:
92
+
93
+ 1. a **static guard**: no deleted tests or assertions, no new skips, retries, sleeps or
94
+ swallowed assertion errors, no changes to test configuration;
95
+ 2. a **mutation guard**: the fixed test must still catch every injected bug the original caught;
96
+ 3. the **reproducer**, which must now pass;
97
+ 4. **200 perturbed runs** with no failure.
98
+
99
+ A rejection goes back to the model with the reason; at most 3 attempts. Everything happens in a
100
+ git worktree under `.flaky-anything/worktrees/`. Your working tree is only touched by `--apply`,
101
+ and only after verification passed.
102
+
103
+ Pick any model with `FLAKY_ANYTHING_MODEL=<provider>/<model>`:
104
+
105
+ ```bash
106
+ export FLAKY_ANYTHING_MODEL=anthropic/claude-opus-5-5 # ANTHROPIC_API_KEY
107
+ export FLAKY_ANYTHING_MODEL=openai/gpt-5 # OPENAI_API_KEY
108
+ export FLAKY_ANYTHING_MODEL=gemini/gemini-3-flash-preview # GEMINI_API_KEY
109
+ export FLAKY_ANYTHING_MODEL=ollama/llama3.1 # local, no key
110
+ ```
111
+
112
+ `openrouter` and `groq` work the same way; `custom` plus `FLAKY_ANYTHING_BASE_URL` takes any
113
+ OpenAI-compatible server. No key at all? `fix --emit-prompt` prints the full prompt (diagnosis,
114
+ reproducer, the rules a fix must follow) for whatever agent you already use.
115
+
116
+ `flaky-anything report --pr <nodeid>` prints a PR body (root cause, reproducer, verification) and,
117
+ on stderr, the matching `gh pr create --title … --body-file -` command to pipe it into.
118
+
119
+ ## How it works
120
+
121
+ A flaky test depends on a **hidden input** it doesn't control. Each one becomes a **knob**:
122
+
123
+ | Knob | Varied over | Baseline |
124
+ |---|---|---|
125
+ | `order` | shuffled test order | collection order |
126
+ | `hash_seed` | `PYTHONHASHSEED` (set iteration order) | `0` |
127
+ | `random_seed` | `random` (and `numpy.random`) seeded per test | `0` |
128
+ | `clock` | month ends, midnight, DST days, leap days (frozen per test) | 2027-06-16 10:00 UTC |
129
+ | `tz` | `TZ`: UTC, New York, London, Kolkata, Lord Howe, Kiritimati, … | `UTC` |
130
+
131
+ Every trial runs in a **fresh pytest subprocess**, and its full knob configuration is stored.
132
+ When a trial fails:
133
+
134
+ 1. **Replay** that exact configuration 10 times. If it fails every time, it's deterministic.
135
+ 2. **Ablate**: put the knobs back to baseline one at a time. The ones it can't pass without are
136
+ the cause. If it even fails at baseline (a test that only breaks in UTC), take the knobs
137
+ from a configuration it passed under, one at a time.
138
+ 3. **Minimise** the test order: bisect to the single polluter, the state-setter it relies on, or
139
+ the smallest combination of earlier tests.
140
+ 4. **Reproduce**: print the smallest command, plain `pytest` whenever the knobs allow it, and
141
+ re-run it to say how often it fails. It only says "fails 20/20" if it just did.
142
+
143
+ Diagnosis tries the victim alone, then its own file, before replaying the whole suite.
144
+
145
+ **Verification is a bound, not a promise.** 0 failures in 500 runs means the failure rate is
146
+ below 0.60% at 95% confidence (0 in 200: below 1.49%). Not zero. The report says exactly that.
147
+
148
+ ## Results
149
+
150
+ Run on popular projects with 15-minute hunts:
151
+
152
+ | Project | Flaky tests found | Root cause |
153
+ |---|---|---|
154
+ | [arrow](https://github.com/arrow-py/arrow) | 2 | `order:victim`: a custom-locale test leaves its locales registered globally |
155
+ | [dateutil](https://github.com/dateutil/dateutil) | 1 | `tz`: fails when the machine's zone is UTC (CI pins New York) |
156
+ | [click](https://github.com/pallets/click) | 59, one cause | `order:brittle`: needs a submodule another test file imports; `pytest` on that file alone fails |
157
+ | [humanize](https://github.com/python-humanize/humanize) | 0 | |
158
+ | [marshmallow](https://github.com/marshmallow-code/marshmallow) | 0 | |
159
+
160
+ Each found test has a reproducer that failed 20/20. Running the tool on these projects also
161
+ found 6 bugs in flaky-anything itself, all fixed with regression tests.
162
+
163
+ ## What it can't do (yet)
164
+
165
+ - **pytest only.**
166
+ - **Races, network, `os.urandom`, real wall time** are outside the knobs. Those tests are reported
167
+ as `uncontrolled`, with no reproducer and no fix, rather than given a made-up cause.
168
+ - **The clock is frozen while each test runs**, not during collection or import. Time read at
169
+ import time isn't controlled.
170
+ - **A test whose node id changes on every run** (e.g. a parameter built from `datetime.now()`)
171
+ can't be targeted. `hunt` lists it under "never ran" instead of calling the project clean.
172
+ - **Order causes are single tests** or a small set (the set search is capped, and it says so).
173
+ - **Ablation flips one knob at a time** when a test fails at the baseline value.
174
+
175
+ ## License
176
+
177
+ MIT
@@ -0,0 +1,150 @@
1
+ # flaky-anything
2
+
3
+ **Your flaky test isn't random. Here's why it fails.**
4
+
5
+ flaky-anything turns a test that fails "sometimes" into a command that fails **every time**,
6
+ tells you the hidden input that causes it, and hands you a fix that's verified not to weaken the test.
7
+
8
+ ![hunt → diagnose → repro (fails 20/20) → fix → verify (0/500, rate < 0.60% at 95%): a real session on the bundled corpus, waits shortened to 1s](https://raw.githubusercontent.com/Yasas-Sri/flaky-anything/main/assets/demo.gif)
9
+
10
+ ```console
11
+ $ flaky-anything diagnose tests/test_order.py::test_lookup_empty_cache
12
+ tests/test_order.py::test_lookup_empty_cache → order:victim
13
+ Replayed config 5f9f450e38e620b8 (a subset of e25589614a1db3d9: the victim alone or with its file):
14
+ failed 10/10.
15
+ needs order: as listed
16
+ polluter: tests/test_order.py::test_warm_cache (runs first, breaks it)
17
+
18
+ $ flaky-anything repro tests/test_order.py::test_lookup_empty_cache
19
+ PYTHONHASHSEED=0 TZ=UTC pytest -p no:randomly tests/test_order.py::test_warm_cache tests/test_order.py::test_lookup_empty_cache
20
+ fails 20/20 (run verbatim just now)
21
+ ```
22
+
23
+ ## Install
24
+
25
+ Python 3.10+, any pytest project.
26
+
27
+ ```bash
28
+ uv tool install flaky-anything # or: pipx install flaky-anything
29
+ uv tool install 'flaky-anything[agent]' # + the Anthropic SDK for `fix` with Claude
30
+ ```
31
+
32
+ It runs your tests with your project's interpreter (`.venv` by default, or `--python`).
33
+
34
+ ## Quick start
35
+
36
+ ```bash
37
+ cd your-project
38
+ flaky-anything hunt --budget 10m # find flaky tests
39
+ flaky-anything diagnose <nodeid> # why it fails: category, minimal cause, polluter
40
+ flaky-anything repro <nodeid> # a plain pytest command that fails, re-run 20× to prove it
41
+ flaky-anything fix <nodeid> # model patch → guards → verify, in a git worktree
42
+ flaky-anything verify <nodeid> # 500 perturbed runs + a confidence bound
43
+ flaky-anything report # every diagnosis and fix, as markdown
44
+ ```
45
+
46
+ Every command takes `--json`, for scripts and agents. `--jobs N` runs trials in parallel.
47
+
48
+ `hunt` prints each flaky test with its failure rate over the configurations it tried, a
49
+ failure signature, and the configuration that failed:
50
+
51
+ ```text
52
+ test verdict failed fail rate (95% CI)
53
+ tests/test_order.py::test_lookup_empty_cache flaky 4/32 12% (5.0%-28%)
54
+ tests/test_time.py::test_next_month_invoice flaky 4/35 11% (4.5%-26%)
55
+ ...
56
+ 87 trials, 20 tests, seed 0, knobs order,hash_seed,random_seed,tz,clock.
57
+ ```
58
+
59
+ Feed it your CI history to start with the tests that already flake: `hunt --from-junit report.xml`
60
+ (repeat the option for more reports).
61
+
62
+ ### Fixing
63
+
64
+ `fix` asks a model for a patch, then accepts it only if it passes, in order:
65
+
66
+ 1. a **static guard**: no deleted tests or assertions, no new skips, retries, sleeps or
67
+ swallowed assertion errors, no changes to test configuration;
68
+ 2. a **mutation guard**: the fixed test must still catch every injected bug the original caught;
69
+ 3. the **reproducer**, which must now pass;
70
+ 4. **200 perturbed runs** with no failure.
71
+
72
+ A rejection goes back to the model with the reason; at most 3 attempts. Everything happens in a
73
+ git worktree under `.flaky-anything/worktrees/`. Your working tree is only touched by `--apply`,
74
+ and only after verification passed.
75
+
76
+ Pick any model with `FLAKY_ANYTHING_MODEL=<provider>/<model>`:
77
+
78
+ ```bash
79
+ export FLAKY_ANYTHING_MODEL=anthropic/claude-opus-5-5 # ANTHROPIC_API_KEY
80
+ export FLAKY_ANYTHING_MODEL=openai/gpt-5 # OPENAI_API_KEY
81
+ export FLAKY_ANYTHING_MODEL=gemini/gemini-3-flash-preview # GEMINI_API_KEY
82
+ export FLAKY_ANYTHING_MODEL=ollama/llama3.1 # local, no key
83
+ ```
84
+
85
+ `openrouter` and `groq` work the same way; `custom` plus `FLAKY_ANYTHING_BASE_URL` takes any
86
+ OpenAI-compatible server. No key at all? `fix --emit-prompt` prints the full prompt (diagnosis,
87
+ reproducer, the rules a fix must follow) for whatever agent you already use.
88
+
89
+ `flaky-anything report --pr <nodeid>` prints a PR body (root cause, reproducer, verification) and,
90
+ on stderr, the matching `gh pr create --title … --body-file -` command to pipe it into.
91
+
92
+ ## How it works
93
+
94
+ A flaky test depends on a **hidden input** it doesn't control. Each one becomes a **knob**:
95
+
96
+ | Knob | Varied over | Baseline |
97
+ |---|---|---|
98
+ | `order` | shuffled test order | collection order |
99
+ | `hash_seed` | `PYTHONHASHSEED` (set iteration order) | `0` |
100
+ | `random_seed` | `random` (and `numpy.random`) seeded per test | `0` |
101
+ | `clock` | month ends, midnight, DST days, leap days (frozen per test) | 2027-06-16 10:00 UTC |
102
+ | `tz` | `TZ`: UTC, New York, London, Kolkata, Lord Howe, Kiritimati, … | `UTC` |
103
+
104
+ Every trial runs in a **fresh pytest subprocess**, and its full knob configuration is stored.
105
+ When a trial fails:
106
+
107
+ 1. **Replay** that exact configuration 10 times. If it fails every time, it's deterministic.
108
+ 2. **Ablate**: put the knobs back to baseline one at a time. The ones it can't pass without are
109
+ the cause. If it even fails at baseline (a test that only breaks in UTC), take the knobs
110
+ from a configuration it passed under, one at a time.
111
+ 3. **Minimise** the test order: bisect to the single polluter, the state-setter it relies on, or
112
+ the smallest combination of earlier tests.
113
+ 4. **Reproduce**: print the smallest command, plain `pytest` whenever the knobs allow it, and
114
+ re-run it to say how often it fails. It only says "fails 20/20" if it just did.
115
+
116
+ Diagnosis tries the victim alone, then its own file, before replaying the whole suite.
117
+
118
+ **Verification is a bound, not a promise.** 0 failures in 500 runs means the failure rate is
119
+ below 0.60% at 95% confidence (0 in 200: below 1.49%). Not zero. The report says exactly that.
120
+
121
+ ## Results
122
+
123
+ Run on popular projects with 15-minute hunts:
124
+
125
+ | Project | Flaky tests found | Root cause |
126
+ |---|---|---|
127
+ | [arrow](https://github.com/arrow-py/arrow) | 2 | `order:victim`: a custom-locale test leaves its locales registered globally |
128
+ | [dateutil](https://github.com/dateutil/dateutil) | 1 | `tz`: fails when the machine's zone is UTC (CI pins New York) |
129
+ | [click](https://github.com/pallets/click) | 59, one cause | `order:brittle`: needs a submodule another test file imports; `pytest` on that file alone fails |
130
+ | [humanize](https://github.com/python-humanize/humanize) | 0 | |
131
+ | [marshmallow](https://github.com/marshmallow-code/marshmallow) | 0 | |
132
+
133
+ Each found test has a reproducer that failed 20/20. Running the tool on these projects also
134
+ found 6 bugs in flaky-anything itself, all fixed with regression tests.
135
+
136
+ ## What it can't do (yet)
137
+
138
+ - **pytest only.**
139
+ - **Races, network, `os.urandom`, real wall time** are outside the knobs. Those tests are reported
140
+ as `uncontrolled`, with no reproducer and no fix, rather than given a made-up cause.
141
+ - **The clock is frozen while each test runs**, not during collection or import. Time read at
142
+ import time isn't controlled.
143
+ - **A test whose node id changes on every run** (e.g. a parameter built from `datetime.now()`)
144
+ can't be targeted. `hunt` lists it under "never ran" instead of calling the project clean.
145
+ - **Order causes are single tests** or a small set (the set search is capped, and it says so).
146
+ - **Ablation flips one knob at a time** when a test fails at the baseline value.
147
+
148
+ ## License
149
+
150
+ MIT
Binary file
@@ -0,0 +1,79 @@
1
+ [project]
2
+ name = "flaky-anything"
3
+ version = "0.1.0"
4
+ description = "Your flaky test isn't random. Find why it fails, get a deterministic repro, and a verified fix."
5
+ readme = "README.md"
6
+ license = { text = "MIT" }
7
+ requires-python = ">=3.10"
8
+ authors = [{ name = "Yasas" }]
9
+ keywords = ["pytest", "flaky tests", "testing", "ci", "agents", "debugging"]
10
+ classifiers = [
11
+ "Development Status :: 3 - Alpha",
12
+ "Framework :: Pytest",
13
+ "Intended Audience :: Developers",
14
+ "License :: OSI Approved :: MIT License",
15
+ "Programming Language :: Python :: 3",
16
+ "Topic :: Software Development :: Testing",
17
+ ]
18
+ dependencies = [
19
+ "pytest>=7.4",
20
+ "typer>=0.12",
21
+ "rich>=13",
22
+ "pydantic>=2.6",
23
+ "time-machine>=2.14",
24
+ "tzdata; sys_platform == 'win32'",
25
+ ]
26
+
27
+ [project.optional-dependencies]
28
+ agent = ["anthropic>=0.40"]
29
+
30
+ [project.scripts]
31
+ flaky-anything = "flaky_anything.cli:run"
32
+
33
+ # Not auto-registered as pytest11 on purpose: the trial plugin must only be active inside
34
+ # our trial subprocesses (loaded explicitly with `-p flaky_anything.trial_plugin`).
35
+
36
+ [project.urls]
37
+ Homepage = "https://github.com/Yasas-Sri/flaky-anything"
38
+ Issues = "https://github.com/Yasas-Sri/flaky-anything/issues"
39
+
40
+ [build-system]
41
+ requires = ["hatchling"]
42
+ build-backend = "hatchling.build"
43
+
44
+ [tool.hatch.build.targets.wheel]
45
+ packages = ["src/flaky_anything"]
46
+
47
+ [dependency-groups]
48
+ dev = [
49
+ "pytest>=8",
50
+ "pytest-cov>=5",
51
+ "ruff>=0.6",
52
+ "mypy>=1.10",
53
+ "syrupy>=4", # snapshot tests for report output
54
+ "tomli>=2; python_version < '3.11'", # read ground_truth.toml on 3.10 (D10)
55
+ ]
56
+
57
+ [tool.pytest.ini_options]
58
+ testpaths = ["tests"]
59
+ norecursedirs = ["tests/corpus", ".flaky-anything", ".git", ".venv"]
60
+ markers = ["e2e: end-to-end tests against the flaky corpus (slower)"]
61
+ addopts = "-m 'not e2e' -p no:randomly"
62
+
63
+ [tool.ruff]
64
+ line-length = 100
65
+ target-version = "py310"
66
+ extend-exclude = ["tests/corpus/project"]
67
+ force-exclude = true # also when the format-on-save hook passes a corpus file path explicitly
68
+
69
+ [tool.ruff.format]
70
+ exclude = ["*.md"] # ponytail: docs code blocks are illustrative, keep their alignment
71
+
72
+ [tool.ruff.lint]
73
+ select = ["E", "F", "I", "B", "UP", "SIM", "RUF", "PT"]
74
+
75
+ [tool.mypy]
76
+ strict = true
77
+ python_version = "3.10"
78
+ packages = ["flaky_anything"]
79
+ mypy_path = "src"
@@ -0,0 +1,5 @@
1
+ """flaky-anything: your flaky test isn't random. Here's why it fails."""
2
+
3
+ from importlib.metadata import version
4
+
5
+ __version__ = version("flaky-anything")