octobot/skills/engineering/diagnose/feedback-loops-r.md
Lukasz Boryn 22951e5b79 feat(diagnose): adopt loop-recipes taxonomy, completion gate, and minimisation
- 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.
2026-06-18 23:29:55 +02:00

3.5 KiB
Raw Blame History

Feedback loops in R — worked examples

Two worked instantiations of loop-recipes.md. R idioms: testthat::test_file(..., reporter="minimal") for a single test; targets::tar_make(names = ...) for one pipeline target; bookdown::preview_chapter() for one Rmd; shinytest2::test_app() for Shiny. Determinism: withr::local_seed/local_envvar/local_options, box::reload(R/<module>) to drop cached box::use state, renv::snapshot() to pin versions before hunting.


Worked example 1 — in-place mutation, intermittently wrong output

Symptom (user): build_plot() sometimes returns a plot with the wrong y-axis; running the same chapter twice in a row gives different results.

Phase 1 — build the loop. The bookdown render is too slow and the bug is intermittent. Compress to a testthat test that runs the broken target twice and compares:

test_that("assemble_expr is idempotent", {
  withr::local_seed(1)
  raw  <- make_modb_mock()
  a <- assemble_expr(raw)
  b <- assemble_expr(raw)            # same input, expect same output
  expect_equal(a, b)
})

Run: testthat::test_file("tests/testthat/test_assemble.R", reporter="minimal"). It fails on second invocation only. Loop is now deterministic and 0.4 s.

Phase 3 — hypotheses. H1: assemble_expr mutates raw in place (data.table reference semantics). Prediction: wrapping the input with data.table::copy(raw) makes the second call match the first.

Phase 4 — instrument. One line:

assemble_expr <- function(raw) {
  raw <- data.table::copy(raw)   ### DIAGNOSE-2026-05-28
  ...
}

Test passes. H1 confirmed.

Phase 5 — regression test = the idempotence test above (already written). Now move the copy() from the diagnostic patch into the real fix at the boundary the contract requires (likely the constructor of the modb_result object — see ../bdd/interface-design-r.md).

Phase 6 — cleanup. grep -r "### DIAGNOSE-" . shows zero hits. Commit message: "fix(assemble_expr): defensive copy of modb_result — H1 confirmed: data.table ref-semantics caused intermittent y-axis swap".


Worked example 2 — flaky test that depends on RNG

Symptom (user): test_textmining.R::"top terms are stable" passes locally, fails 1/10 times on CI.

Phase 1 — build the loop. Run the single test 50 times in a row in a one-liner:

for i in $(seq 50); do
  Rscript -e 'testthat::test_file("tests/testthat/test_textmining.R", reporter="minimal")' \
    || { echo "FAIL on iter $i"; break; }
done

Fails on iteration 7. Loop reproduces in < 30 s.

Phase 3 — hypotheses. H1: the mock data uses runif() without a seed; ties in the z-score ranking sort differently across runs. H2: a locale-dependent sort on term names.

H1 is more likely (untied scores are improbable when sampling 100 uniforms; ties happen when the test mock samples a tiny matrix).

Phase 4 — instrument. Add withr::local_seed(42) at the top of the test. Run the 50× loop again. All green. H1 confirmed; H2 not pursued (one variable at a time).

Phase 5 — regression test: the seeded test is the regression test. Add a comment: # seed pins the tie-breaking order; see DIAGNOSE post-mortem.

Phase 6 — cleanup. Nothing to revert. Commit: "test(textmining): seed RNG to stabilise top-term tie-break — H1 confirmed". File an architectural follow-up: "production score_terms() should break ties on a stable key (term_id) rather than relying on input order" → goes to ../bdd/refactoring-r.md candidate list.