Skip to main content

spotregen step by step: exercises

The shell commands of the tutorial, to type yourself

Author

Thomas Bartz-Beielstein

Published

November 27, 2026

This sheet belongs to the tutorial spotregen 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.

No cell of this page is executed. The results in the solutions were taken from a run of these commands with spotregen 0.15.0 on 2026-11-27.

You need uv, Python 3.13 or later and git. The last exercise is optional and needs a local model server.

Setup

spotregen score and spotregen local judge against the last commit, so regen/ must lie at the top of a git repository. Inside the repository of the tutorial it does not. The exercises therefore work on a copy with a git repository of its own. The package and the candidates lie in the directory examples/ of the spotregen repository: clone it and copy both from there.

git clone https://git-ce.rwth-aachen.de/thomas.bartz-beielstein/spotregen.git
mkdir ~/regen-exercise
cp -R spotregen/examples/mathregen spotregen/examples/candidates ~/regen-exercise/
cd ~/regen-exercise/mathregen
rm -rf .venv regen/runs
git init
uv sync
git add -A
git commit -m "mathregen 0.1.0 with its regen records"

The copy leaves out the archived model runs (regen/runs). All later commands are run in ~/regen-exercise/mathregen. Two commands bring the copy 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 spotregen 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 spotregen check
  1. Which check ID is reported, and to which primitive does it belong?
  2. Repair the record, not the script: declare the script as a consumer of the unit primes and 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
  1. Append to regen/units/primes.toml:
[[consumers]]
ref = "scripts/first_primes.py"
kind = "code"
note = "prints the first twenty primes"

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

Deletion test and bundle

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

uv run spotregen deletion-test primes
uv run spotregen 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 4 (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 5 (Intent coverage and specification size) Tutorial: How good are the evaluations?

uv run spotregen 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 5 (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 6 (A forgotten evaluation) Tutorial: How good are the evaluations?

uv run spotregen 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 spotregen mutate primes --timeout 5 and give its three survivors a verdict.

Solution 6 (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 7 (Three candidates) Tutorial: Regeneration.

uv run spotregen score fibonacci ../candidates/fibonacci_doubling.py --out ../runs/doubling
echo $?
uv run spotregen score fibonacci ../candidates/fibonacci_binet.py --out ../runs/binet
echo $?
grep FAILED ../runs/binet/evaluations-1.txt
uv run spotregen 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 7 (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.

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

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

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

Score your module as in Exercise 7. 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 9 (An allowed import that nobody uses) Tutorial: Compaction and the guard.

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

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

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

Solution 8 (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.

A third function

Exercise 10 (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 spotregen check after every step.

  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 spotregen 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?

  5. Write the intent items in regen/units/factorial.toml, name the evaluation file under evaluations, and let every item name its evaluations and a decision record.

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

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

  8. Commit.

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

Solution 9 (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, and after step 3 the draft fails it on purpose:

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

One possible record has two intent items, both justified by the existing decision record integers-only, so no new decision record is needed:

evaluations = ["tests/test_regen_eval_factorial.py"]

[[intent]]
id = "FAC-1"
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"
evaluations = ["test_the_empty_product_is_one", "test_every_value_is_n_times_the_value_before_it"]
decisions = ["integers-only"]

[[intent]]
id = "FAC-2"
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"
evaluations = ["test_a_negative_argument_raises_value_error", "test_an_argument_that_is_not_an_int_raises_type_error", "test_a_bool_argument_raises_type_error"]
decisions = ["integers-only"]

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 11 (A model as the author) Tutorial: Regeneration.

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.

Replace the four 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 spotregen local --help).

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

uv run spotregen 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 spotregen check. What does the check say about the archived run, and why?

Solution 10 (A model as the author). The answers depend on your model. For comparison, the runs of the tutorial with qwen/qwen3-coder-30b 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.

  1. The check reports a warning of the family PROV: 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.