- Extract 10-recipe loop taxonomy + tighten/non-determinism principles
into new sibling loop-recipes.md (language-agnostic)
- Shrink feedback-loops-{py,r}.md to worked-examples-only; both now
under the 100-line cap (R: 146 -> 95, py: 111 -> 79)
- Phase 1: add four-bullet completion gate (red / tight / deterministic
/ agent-runnable) + "no red-capable command, no Phase 2" guardrail
- Phase 2: rename to "Reproduce + minimise"; add shrink-to-smallest
sub-step
- Phase 5: add "no correct seam is itself the finding" escape valve
pointing to refactoring-r.md instead of writing a lying test
- Phase 6: lead with "what would have prevented this bug?" framing
Adapted from mattpocock/skills diagnosing-bugs.
3.0 KiB
Feedback loops in Python — worked examples
Two worked instantiations of loop-recipes.md. Python
idioms: pytest path::test -x -q for a single test; -k for substring,
--lf for last-failed, -p no:cov to drop coverage overhead.
Determinism: np.random.default_rng(42) (never global np.random),
monkeypatch.setenv("PYTHONHASHSEED", "0"), tmp_path for filesystem
isolation; autouse network-kill fixture in
../bdd/mocking-py.md.
Worked example 1 — in-place mutation, intermittently wrong output
Symptom (user): assemble_expr() sometimes returns a frame with the
wrong totals; running the same step twice gives different results.
Phase 1 — build the loop. Compress to an idempotence test:
def test_assemble_expr_is_idempotent():
"""Given one raw frame, when assembled twice, then both results match."""
raw = make_raw_fixture()
pd.testing.assert_frame_equal(assemble_expr(raw), assemble_expr(raw))
pytest tests/test_assemble.py -x -q fails on the second call. Loop is
deterministic and 0.3 s.
Phase 3 — hypotheses. H1: assemble_expr mutates raw in place
(pandas view aliasing). Prediction: a defensive raw = raw.copy() makes
call two match call one.
Phase 4 — instrument. One tagged line:
def assemble_expr(raw: pd.DataFrame) -> pd.DataFrame:
raw = raw.copy() ### DIAGNOSE-2026-06-11
...
Test passes. H1 confirmed.
Phase 5 — regression test = the idempotence test. Move the copy from the diagnostic patch into the real fix at the contract boundary (return a fresh frame, don't mutate the argument — see ../bdd/interface-design-py.md).
Phase 6 — cleanup. grep -rn "### DIAGNOSE-" . → zero hits. Commit:
"fix(assemble_expr): return a fresh frame — H1: pandas view aliasing
caused non-idempotent totals".
Worked example 2 — flaky test that depends on RNG
Symptom (user): test_top_terms_stable passes locally, fails ~1/10 on CI.
Phase 1 — build the loop. Run it many times via the plugin
pytest-repeat, or a shell loop:
for i in $(seq 50); do
pytest tests/test_textmining.py::test_top_terms_stable -q \
|| { echo "FAIL on iter $i"; break; }
done
Fails on iteration 7. Reproduces in < 30 s.
Phase 3 — hypotheses. H1: the fixture samples with an unseeded
generator; ties in the z-score ranking sort differently per run. H2:
dict/set iteration order via unset PYTHONHASHSEED. H1 more likely (the
mock matrix is tiny → ties common).
Phase 4 — instrument. Pass a seeded generator into the fixture; rerun the 50× loop. All green. H1 confirmed; H2 not pursued (one variable at a time).
Phase 5 — regression test: the seeded test is the regression test.
Phase 6 — cleanup. Nothing to revert. Commit: "test(textmining): seed
RNG to stabilise tie-break — H1 confirmed". File a follow-up: production
score_terms() should break ties on a stable key (term_id), not input
order → ../bdd/refactoring-r.md candidate.