Skip to main content

babregen step by step: exercises

The shell commands of the tutorial, to type yourself

Author

Thomas Bartz-Beielstein

Published

November 28, 2026

This sheet belongs to the tutorial babregen step by step. The tutorial shows the output of every command; here you type the commands yourself, in a terminal, on a copy of the package mathregen. Each exercise names the section of the tutorial that explains it. The script of the page Step by step in a terminal runs the commands of the tutorial for you, if you want to watch before you type.

The results in the solutions are checked. Hidden cells of this page run the commands of the exercises in a clone of the example, and they stop the render if an output differs from the text of a solution. The last run used babregen 0.1.0.

You need uv, Python 3.13 or later and git. The last exercise is optional and needs a local model server. The pages Commands and Schema and check IDs are the reference for every command and every check ID that the exercises meet.

Setup

The package mathregen is an example of the repository babregen-examples. Clone the repository at the tag babregen-v0.1.0, the state that was checked against the babregen release of that number, and work on a branch of your own:

mkdir ~/regen-exercise
cd ~/regen-exercise
git clone --branch babregen-v0.1.0 https://gitlab.git.nrw/thomas.bartz-beielstein/babregen-examples.git
cd babregen-examples/mathregen
git switch -c exercise
git rm -r -q regen/runs
git commit -m "exercise: start without the archived model runs"
uv sync

uv sync installs babregen as a development dependency of the example. The second commit removes the archive of the model runs. With the archive in place, the check warns after every change to a record that the newest run judged another state (Exercise 13 comes back to this).

babregen score and babregen local judge against the last commit, so the project must lie in a git repository. A subfolder of a repository, as here, is fine. Without git you can download the ZIP of the tag from the GitLab page of the repository (Code, Download source code). Then run git init, git add -A and git commit in the unpacked folder before the first exercise, and delete mathregen/regen/runs first.

All later commands are run in ~/regen-exercise/babregen-examples/mathregen. Two commands bring the folder back to the last commit at any time:

git restore .
git clean -fd

The starting point

Exercise 1 (Tests, evaluations and the static check) Tutorial: the sections from Two functions and their unit tests to Decisions.

uv run pytest -q
uv run pytest -q -m regen
uv run babregen check
echo $?
  1. How many tests are there, and how many of them are durable evaluations?
  2. Open tests/test_primes.py and tests/test_regen_eval_primes.py. Which of the two files imports a name that is not public, and why does that matter?
  3. Open regen/units/primes.toml. Which evaluations check intent item PRI-3, and which decision record justifies it?

Solution 1 (Tests, evaluations and the static check).

  1. 26 tests, 20 of them durable evaluations (10 per unit). The check prints regen check: 2 units, 0 errors, 0 warnings and exits with 0.
  2. tests/test_primes.py imports the private helper _is_prime. It is a unit test: it is coupled to one implementation and stops working when a new module has no such helper. The evaluation file imports nth_prime only.
  3. Read them from the fields evaluations and decisions of the item.

What the check catches

Exercise 2 (An undeclared consumer) Tutorial: What the check catches.

A colleague adds a script that uses nth_prime.

cat > scripts/first_primes.py <<'EOF'
from mathregen.primes import nth_prime

print([nth_prime(n) for n in range(1, 21)])
EOF
uv run babregen check
  1. Which check ID is reported, and to which primitive does it belong? Let the tool explain it: uv run babregen explain with the ID.
  2. Repair the record, not the script: declare the script as a consumer of the unit primes with uv run babregen add-consumer. Use --dry-run first, then run the check again.
  3. Return to the last commit.

Solution 2 (An undeclared consumer).

  1. DELETE01, deletion:
regen/units/primes.toml:1: error DELETE01 scripts/first_primes.py depends on the unit but is not a declared consumer
regen check: 2 units, 1 errors, 0 warnings

uv run babregen explain DELETE01 prints the condition, a wrong and a corrected fragment, and the fix.

  1. The dry run prints the block, the second call appends it to regen/units/primes.toml:
uv run babregen add-consumer primes scripts/first_primes.py --note "prints the first twenty primes" --dry-run
uv run babregen add-consumer primes scripts/first_primes.py --note "prints the first twenty primes"
[[consumers]]
ref = "scripts/first_primes.py"
kind = "code"
note = "prints the first twenty primes"

The kind is inferred from the path. The check is clean again. (c) git restore . && git clean -fd

Exercise 3 (A forbidden import) Tutorial: What the check catches.

printf '\nfrom mathregen.fibonacci import fibonacci\n' >> src/mathregen/primes.py
uv run babregen check
git restore .

One line gives three findings. Explain each of them. Which file would you have to change to make the import legal, and should you?

Solution 3 (A forbidden import).

src/mathregen/primes.py:52: error COMPILE02 'primes' imports 'mathregen.fibonacci', but architecture.toml allows only []
regen/units/fibonacci.toml:1: error DELETE01 src/mathregen/primes.py depends on the unit but is not a declared consumer
src/mathregen/primes.py:52: error COMPILE07 mathregen.primes: imports 'mathregen.fibonacci' which is not in depends_on

COMPILE02: the architecture allows primes no import inside the package. COMPILE07: the unit record of primes declares no dependency. DELETE01: primes.py has become a consumer of the unit fibonacci and is not declared.

To make the import legal, three records would change: [allowed] in regen/architecture.toml, depends_on in regen/units/primes.toml and the consumers in regen/units/fibonacci.toml. The import is not needed, so the right fix is to remove it.

Exercise 4 (A misspelled name) Tutorial: What the check catches.

In regen/units/fibonacci.toml, change the evaluation "test_a_bool_index_raises_type_error" of intent item FIB-3 to "test_a_bool_index_raises_typeerror".

uv run babregen check
uv run babregen explain EVAL03
uv run babregen check --format json
git restore .
  1. One typo gives two findings. How are they connected?
  2. What does the tool suggest, and where in the line does the suggestion stand?
  3. In the JSON document, which field holds the suggestion? Why is that form better for a script than the text line?

Solution 4 (A misspelled name).

regen/units/fibonacci.toml:52: error EVAL03 intent FIB-3: evaluation 'test_a_bool_index_raises_typeerror' not found; a similar name exists: 'test_a_bool_index_raises_type_error'
tests/test_regen_eval_fibonacci.py:43: warning COMPACT05 mathregen.fibonacci: evaluation 'test_a_bool_index_raises_type_error' is not linked to any intent item
  1. The intent item names an evaluation that does not exist (EVAL03, an error, at the line of the wrong name). The evaluation that was meant still exists, and no intent item names it any more (COMPACT05, a warning).
  2. The message ends with ; a similar name exists: '...' and the name that was probably meant.
  3. The field suggestion of the finding, next to path, line, level, id and message. The message of the JSON form does not repeat the suggestion, so a script reads fields and takes no text apart.

Deletion test and bundle

Exercise 5 (The preconditions of a deletion) Tutorial: The deletion test.

uv run babregen deletion-test primes
uv run babregen context primes > ../../bundle-primes.md
grep '^#' ../../bundle-primes.md
grep -c 'candidate += 1' src/mathregen/primes.py ../../bundle-primes.md
  1. How many dependent files does the report list, and which of them are not declared under [[consumers]] in the record?
  2. The verdict is “regenerable”. Which two of the four components of the deletion test has the command checked, and which two are still open?
  3. What does the last command show? Which code does the bundle copy in full?

Solution 5 (The preconditions of a deletion).

  1. Five: the three declared consumers (README.md, scripts/table.py, src/mathregen/__init__.py), the unit test and the evaluation file. The last two come from the fields unit_tests and evaluations.
  2. Checked: the blast radius and the knowledge outside the implementation. Open: the rebuild and its judgement. The verdict is a precondition for an attempt, not its result.
  3. The line of the loop occurs once in the module and not at all in the bundle. The bundle holds signatures and docstrings of the public names and no implementation. The only code it copies in full is the evaluation file.

Measures and mutation analysis

Exercise 6 (Intent coverage and specification size) Tutorial: How good are the evaluations?

uv run babregen measures
  1. What is the intent coverage of each unit? Why is it not code coverage?
  2. How many lines of specification stand against how many lines of code in the unit fibonacci?

Solution 6 (Intent coverage and specification size).

  1. 3/3 for both units. Intent coverage counts the recorded promises that have a judge, not the lines that ran.
  2. 158 lines of specification (69 record, 43 decisions, 46 evaluations) against 36 lines of code, a ratio of 4.4.

Exercise 7 (A forgotten evaluation) Tutorial: How good are the evaluations?

uv run babregen mutate fibonacci --timeout 10

Now delete the function test_a_bool_index_raises_type_error from tests/test_regen_eval_fibonacci.py in an editor and run the command again.

  1. What is the mutation score before and after?
  2. One survivor replaces raise TypeError(...) by pass. Five evaluations with values that are not integers still pass on it. Why?
  3. Give each survivor one of the verdicts gap, output or equivalent.
  4. Return to the last commit. Then run uv run babregen mutate primes --timeout 5 and give its three survivors a verdict.

Solution 7 (A forgotten evaluation).

  1. 11/11 before, 9/11 after. With a survivor the command exits with 1.
  2. Python raises a TypeError by itself a few lines later, when it compares a string with 0 or calls range with a float. Only a bool gets through, so only the bool evaluation proves that the explicit type check is there.
  3. Both are gaps: the unit promises a TypeError for a bool (intent item FIB-3), and no evaluation is left that checks it.
  4. 28/31. All three survivors sit in the private helper _is_prime and change nothing that nth_prime promises: equivalent. Equivalent mutants stay in the denominator, so the score is a lower bound.

Judging a candidate

Exercise 8 (Three candidates) Tutorial: Regeneration.

uv run babregen score fibonacci candidates/fibonacci_doubling.py --out ../../runs/doubling
echo $?
uv run babregen score fibonacci candidates/fibonacci_binet.py --out ../../runs/binet
echo $?
grep FAILED ../../runs/binet/evaluations-1.txt
uv run babregen score primes candidates/primes_sieve.py --out ../../runs/sieve
echo $?
cat ../../runs/sieve/unit_tests.txt
  1. Which candidates are accepted? Read accepted, the counts of [[attempt]] and the counts of [final] in each output.
  2. The closed formula passes the unit tests and the dependents. What rejects it?
  3. The sieve fails the unit tests. Why is it accepted all the same?
  4. Run git status. What did score change in the working tree?

Solution 8 (Three candidates).

  1. Fast doubling: accepted, 10 evaluations passed, exit code 0. Closed formula: rejected, 5 passed and 5 failed, exit code 1. Sieve: accepted, exit code 0.
  2. The durable evaluations: the recurrence over a long range, the exact large value, and the three evaluations of the argument type (two floats and the bool). The unit tests and the docstring example look at small values only.
  3. The unit tests import _is_prime, which a sieve does not have, so they end in an error. By the acceptance rule accept/1, unit tests are recorded and never decide.
  4. Nothing. score works in a scratch worktree of the last commit and writes only the run directory given with --out, here ~/regen-exercise/runs/. Without --out it writes to a temporary directory and prints its path.

Exercise 9 (A candidate of your own) Tutorial: Regeneration.

Write a module ../../fibonacci_mine.py without looking at src/mathregen/fibonacci.py. Use the bundle as your only source:

uv run babregen context fibonacci > ../../bundle-fibonacci.md

Score your module as in Exercise 8. If it is rejected, read evaluations-1.txt in the run directory, repair the module and score it again with a new --out directory.

Which intent item did you miss at first, if any? Was the bundle silent or ambiguous at any place? Such a place is a gap of the record, and its repair belongs in the record.

Compaction

Exercise 10 (An allowed import that nobody uses) Tutorial: Compaction and the guard.

In regen/architecture.toml, change fibonacci = [] to fibonacci = ["primes"].

uv run babregen compact
uv run babregen check; echo $?
uv run babregen check --strict; echo $?
git restore .

Why does the first check pass and the second fail? Which of the two belongs in make guard?

Solution 9 (An allowed import that nobody uses).

regen/architecture.toml:37: warning COMPACT02 allowed edge fibonacci -> primes is unused
regen compact: 1 candidates

The finding is a warning. Warnings do not fail the check (exit code 0); with --strict they do (exit code 1). make guard uses --strict, so stale knowledge cannot accumulate unnoticed.

Help in the editor

Exercise 11 (Schemas for the editor) Tutorial: Help in the editor.

uv run babregen schema unit | diff - regen/schemas/unit.schema.json
echo $?
uv run babregen example unit
  1. What does the exit code of diff say about the file in regen/schemas/?
  2. Which file tells the editor which schema belongs to which record?
  3. Optional, with VS Code and the extension Even Better TOML: open regen/units/fibonacci.toml, set pace = "quick" and add a key owner = "me". What does the editor mark? Run uv run babregen check as well and compare.

Solution 10 (Schemas for the editor).

  1. 0: the file is the output of babregen schema unit. The same holds for architecture and decision.
  2. .taplo.toml, with one rule for each kind of record.
  3. The editor marks both lines while you type: quick is not one of fast, medium and slow, and the schema has no key owner. The check refuses the record as well. What the schema cannot know is a name in the source tree, for example the misspelled evaluation of Exercise 4. That stays the work of the check.

A third function

Exercise 12 (The unit factorial) Tutorial: A third function.

Add a third unit to the package, a function factorial(n) in a module src/mathregen/factorial.py. Work in this order and run uv run babregen check after every step. The commands of steps 5 and 6 are the subject of the tutorial Write the records.

  1. Write the module with __all__ = ["factorial"], a docstring and the function.

  2. Give the module a place in [allowed] of regen/architecture.toml.

  3. Draft the record: uv run babregen new-unit factorial --pace fast.

  4. Write tests/test_regen_eval_factorial.py with pytestmark = pytest.mark.regen. Decide first what a competent stranger would get wrong: what is factorial(0), and what happens for a negative number, a bool and a float? Name the file under evaluations in regen/units/factorial.toml.

  5. Is a choice made that no decision record covers yet? If so, create the record with uv run babregen new-decision, with --dry-run first.

  6. Append the intent items with uv run babregen add-intent, again with --dry-run first. Every item names its evaluations and a decision record.

  7. Run the evaluations, the deletion test and the mutation analysis:

    uv run pytest -q -m regen
    uv run babregen deletion-test factorial
    uv run babregen mutate factorial --timeout 10
  8. Give every surviving mutant a verdict. Close the gaps with an evaluation.

  9. Commit.

Which findings did you meet on the way, and in which order?

Solution 11 (The unit factorial). The findings come in this order. After step 1:

src/mathregen/factorial.py: error COMPILE03 subpackage 'factorial' is not listed in [allowed] of architecture.toml

After step 2 (the line factorial = [] under [allowed]) the check is clean. In step 3 the draft fails it on purpose, and new-unit prints the finding itself:

regen/units/factorial.toml:4: error INTENT01 declares no intent item (a unit without intent cannot be regenerated)

After step 4, the check adds one warning COMPACT05 for every evaluation that no intent item names yet. One possible record has two intent items. The first rests on a new choice, the value of the empty product, so it gets a decision record. The second is covered by the existing record integers-only.

uv run babregen new-decision empty-product --title "factorial(0) is 1" \
    --driver "The product of no factors needs a value, and a loop that starts at 0 returns 0 for every n." \
    --decision "factorial(0) == 1, the empty product, so that factorial(n) == n * factorial(n - 1) holds from n = 1 on." \
    --approved-by maintainer
uv run babregen add-intent factorial --scope "factorial(n) for n >= 0" \
    --invariant "factorial(0) == 1 and factorial(n) == n * factorial(n - 1) for n >= 1, as an exact int" \
    --reason "The empty product is 1. A loop that starts at the wrong value returns 0 for every n." \
    --evaluation test_the_empty_product_is_one \
    --evaluation test_every_value_is_n_times_the_value_before_it \
    --decision empty-product
uv run babregen add-intent factorial --scope "arguments outside the domain" \
    --invariant "A negative int raises ValueError. A bool and every value that is not an int raise TypeError." \
    --reason "factorial(True) and factorial(-1) would return 1 and hide the mistake of the caller." \
    --evaluation test_a_negative_argument_raises_value_error \
    --evaluation test_an_argument_that_is_not_an_int_raises_type_error \
    --evaluation test_a_bool_argument_raises_type_error \
    --decision integers-only

The commands give the items the ids FAC-1 and FAC-2. A name that does not exist is refused before anything is written, with a similar name when there is one. With the five evaluations that the items name, the check reports 3 units, 0 errors, 0 warnings, the deletion test ends with “regenerable”, and a module that checks the type, then the sign, and multiplies in a loop has a mutation score of 13/13. Your module and your numbers may differ.

No record of the other two units changed. The only shared file that changed is the architecture file, by one line.

Optional: regeneration with a local model

Exercise 13 (A model as the author) Tutorial: A candidate written by a model.

This exercise needs a model server on your machine, for example LM Studio with a coding model, and a clean working tree. Read the decision record regen/decisions/regen-protocol.toml first: it fixes the procedure, and its id is a required option. The page Scoring and regeneration describes the driver and its options.

Replace the three values in angle brackets. The hash is the SHA-256 that identifies the files of your model; the command records it and cannot verify it. Leave out --template and --reasoning unless your model needs them (uv run babregen local --help).

uv run babregen local fibonacci --model <model> --model-sha256 <sha256> \
    --context-length <length> --protocol regen-protocol --canary

uv run babregen local fibonacci --model <model> --model-sha256 <sha256> \
    --context-length <length> --protocol regen-protocol \
    --attempts 3 --seed 1
  1. Did the canary pass? What did it test?
  2. Was the candidate accepted, and on which attempt? Read run.toml in the run directory under regen/runs/.
  3. If an attempt failed: which evaluation, and which intent item stands behind it?
  4. Read report.md, the gap report of the model. Does any item point at a real ambiguity of the record?
  5. Change the wording of one intent item and run uv run babregen check. What does the check say about the archived run, and why?

Solution 12 (A model as the author). The answers depend on your model. For comparison, the archived runs of the example, made with babregen 0.0.1 and qwen/qwen3-coder-30b on 2026-11-28, were accepted on the second attempt for both units. In both, the first attempt failed test_a_bool_index_raises_type_error, the evaluation behind the intent item about bool. You removed that archive in the setup. It is in regen/runs/ of the tag.

  1. The check reports the warning PROV08: the bundle that the newest run judged is no longer the bundle that a generator would receive. The verdict of the run is evidence of its day only. uv run babregen explain PROV08 says what to do.