octobot/skills/engineering/diagnose/feedback-loops-py.md
Lukasz Boryn 277ec801de refactor(skills): convert cross-skill links to /skill-name references
- implements blueprint 028
- rewrite 290 cross-skill markdown links across 59 files to unchecked
  backtick-wrapped /skill-name prose (R-023), so skill content resolves
  under any install depth — symlinked, flat-copied, or whole-tree
- add skills/productivity/conjure-a-skill/scripts/skill-ref-check —
  validates every /skill-name mention resolves to a real skill folder,
  the replacement coverage link-check lost on these references
- add scripts/link-skills.sh, mirroring mattpocock/skills, symlinking
  every skill into ~/.claude/skills and ~/.agents/skills
- docs(readme): point Install at link-skills.sh and npx skills@latest
  add for non-Claude-Code harnesses
2026-09-23 11:03:25 +02:00

2.9 KiB
Raw Blame History

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.


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 seam (return a fresh frame, don't mutate the argument — see /bdd).

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 candidate.