import subprocess
import sys
import tempfile
from pathlib import Path
from babregen import load_spec, make_sample_repo
tmp = tempfile.TemporaryDirectory()
root = make_sample_repo(Path(tmp.name) / "demo")
def babregen(*args, head=None):
"""Run `babregen` in the sample repository; show the call and its output."""
done = subprocess.run(
[sys.executable, "-m", "babregen", *args],
cwd=root,
capture_output=True,
text=True,
check=False,
)
print("$ babregen", *args)
lines = (done.stdout + done.stderr).replace(str(root) + "/", "").splitlines()
print("\n".join(lines[:head] + (["..."] if head and len(lines) > head else [])))
print(f"[exit {done.returncode}]")
return doneWrite the records
This tutorial adds a function to the sample repository and records what is now true about it, using the commands that write records. The commands create a file or append one complete block, check every name before they write, and have --dry-run. For a larger project that you can clone and run, see the repository babregen-examples. Every cell here builds its own repository in a temporary directory.
See a record before you write one
babregen example KIND prints one complete, valid, commented record of a kind (architecture, unit or decision). It needs no regen/ tree, so it works in an empty directory.
done = babregen("example", "unit", head=16)
assert done.returncode == 0$ babregen example unit
# regen/units/core.calc.toml: the record of the unit samplepkg.core.calc. The file name is
# the unit name without the package; the module path follows from it
# (src/samplepkg/core/calc.py). The names are those of the sample repository.
# One paragraph: what the unit is for. Prose fields are folded, so a paragraph may be
# wrapped over several lines without a backslash.
summary = """
Adds numbers. The one function of the package that callers use to sum two integers;
everything else is a private helper."""
pace = "fast" # fast, medium or slow; the policy is in architecture.toml
public = ["add"] # the contract: names the module must define
depends_on = [] # modules of the package the unit may import
evaluations = ["tests/test_regen_eval_calc.py"] # the durable evaluation files
unit_tests = [] # tests that are coupled to the implementation
# Intent: what must stay true for any implementation, and why.
...
[exit 0]
A check ID that you do not know is explained by babregen explain ID: its level, the condition, a wrong and a corrected fragment, and the usual fix.
done = babregen("explain", "INTENT02", head=12)
assert done.returncode == 0$ babregen explain INTENT02
INTENT02 (error): an intent item names no evaluation
Wrong, in regen/units/core.calc.toml:
evaluations = []
Right:
evaluations = ["test_add"]
Fix: Name the test functions that check the invariant, or write the evaluation first and then name it. An invariant that no evaluation checks is a wish.
[exit 0]
Prose is wrapped, never continued
The text fields of a record are prose: a summary, an invariant, a reason. Wrap a paragraph over several lines in a multi-line string and write no backslash. The loader strips the value and turns every line break, with the blanks around it, into one space.
unit = root / "regen" / "units" / "core.calc.toml"
unit.write_text(
unit.read_text().replace(
'summary = "Adds numbers."',
'summary = """\nAdds and subtracts numbers.\nIt is the only arithmetic\nthe application needs.\n"""',
)
)
summary = load_spec(root).units["samplepkg.core.calc"].summary
assert summary == "Adds and subtracts numbers. It is the only arithmetic the application needs."
print(summary)Adds and subtracts numbers. It is the only arithmetic the application needs.
A blank line inside a prose field is an error, because a prose field is one paragraph. A prose field is for sentences, not for code or a table. The fields and the rule are on the schema page.
A new function and its evaluation
The unit gets a second promise, sub. The function, its durable evaluation and the name in the public list of the record are the part that only you can write. The check reports the evaluation, because no intent item names it yet.
calc = root / "src" / "samplepkg" / "core" / "calc.py"
calc.write_text(
calc.read_text().replace(
"\n\ndef _helper",
'\n\ndef sub(a: int, b: int) -> int:\n """Subtract b from a."""\n return a - b\n\n\ndef _helper',
)
)
evaluation = root / "tests" / "test_regen_eval_calc.py"
evaluation.write_text(
evaluation.read_text().replace("import add", "import add, sub")
+ "\n\ndef test_sub():\n assert sub(5, 3) == 2\n"
)
unit.write_text(unit.read_text().replace('public = ["add"]', 'public = ["add", "sub"]'))
done = babregen("check")
assert "COMPACT05" in done.stdout and done.returncode == 0$ babregen check
tests/test_regen_eval_calc.py:12: warning COMPACT05 samplepkg.core.calc: evaluation 'test_sub' is not linked to any intent item
regen check: 1 units, 0 errors, 1 warnings
[exit 0]
Record the decision
babregen new-decision ID creates regen/decisions/ID.toml. The id is the file name without .toml: lower-case letters and digits, with ., - or _ between them. The date is today unless --date is given. --dry-run prints the record and writes nothing.
options = [
"--title", "Subtraction is provided",
"--driver", "A user asked for it.",
"--decision", "Provide sub next to add.",
"--approved-by", "maintainer",
"--evidence", "file:src/samplepkg/core/calc.py",
"--date", "2026-11-28",
]
done = babregen("new-decision", "why-sub", *options, "--dry-run")
assert not (root / "regen/decisions/why-sub.toml").exists()
done = babregen("new-decision", "why-sub", *options)
assert (root / "regen/decisions/why-sub.toml").is_file()$ babregen new-decision why-sub --title Subtraction is provided --driver A user asked for it. --decision Provide sub next to add. --approved-by maintainer --evidence file:src/samplepkg/core/calc.py --date 2026-11-28 --dry-run
title = "Subtraction is provided"
date = 2026-11-28
status = "active"
driver = "A user asked for it."
decision = "Provide sub next to add."
approved_by = "maintainer"
evidence = ["file:src/samplepkg/core/calc.py"]
[exit 0]
$ babregen new-decision why-sub --title Subtraction is provided --driver A user asked for it. --decision Provide sub next to add. --approved-by maintainer --evidence file:src/samplepkg/core/calc.py --date 2026-11-28
wrote regen/decisions/why-sub.toml
next: cite it from an intent item (babregen add-intent UNIT --decision why-sub) or from `decisions` of architecture.toml, then run babregen check
still open (babregen check will report):
regen/decisions/why-sub.toml:1: warning COMPACT06 no intent item references this decision
[exit 0]
The command prints wrote and the next: line, then what is still open: what the check says about the new file. The decision is not cited yet, and the next step cites it.
Write the intent item and read a suggestion
babregen add-intent UNIT appends one [[intent]] block. You give the prose as options; the tool never drafts it. It checks that every evaluation is defined in an evaluation file of the unit and that every decision exists. A name that matches nothing is refused with the name it probably meant, and nothing is written:
item = [
"--scope", "sub",
"--invariant", "sub(a, b) == a - b",
"--reason", "Callers rely on plain arithmetic.",
"--decision", "why-sub",
]
before = unit.read_text()
done = babregen("add-intent", "core.calc", *item, "--evaluation", "test_sbu")
assert done.returncode == 2 and "a similar name exists: 'test_sub'" in done.stderr
assert unit.read_text() == before$ babregen add-intent core.calc --scope sub --invariant sub(a, b) == a - b --reason Callers rely on plain arithmetic. --decision why-sub --evaluation test_sbu
babregen: evaluation 'test_sbu' is not defined in the evaluation files of samplepkg.core.calc; a similar name exists: 'test_sub'
[exit 2]
The same suffix, ; a similar name exists: 'X', ends a finding of babregen check for a name that you wrote by hand. With the corrected name the item is appended:
done = babregen("add-intent", "core.calc", *item, "--evaluation", "test_sub")
assert done.returncode == 0 and unit.read_text().startswith(before)
done = babregen("check")
assert done.returncode == 0 and "0 errors, 0 warnings" in done.stdout$ babregen add-intent core.calc --scope sub --invariant sub(a, b) == a - b --reason Callers rely on plain arithmetic. --decision why-sub --evaluation test_sub
wrote regen/units/core.calc.toml
next: run babregen check
[exit 0]
$ babregen check
regen check: 1 units, 0 errors, 0 warnings
[exit 0]
Declare a consumer
A script that uses the unit is a consumer. babregen add-consumer UNIT REF appends one [[consumers]] block and infers the kind from the path (--kind overrides it). REF must exist, unless the kind is external.
(root / "scripts").mkdir()
(root / "scripts" / "use_it.py").write_text("from samplepkg.core import calc\n")
done = babregen("check")
assert "DELETE01" in done.stdout and done.returncode == 1
done = babregen("add-consumer", "core.calc", "scripts/use_it.py", "--note", "runs sub")
done = babregen("check", "--strict")
assert done.returncode == 0$ babregen check
regen/units/core.calc.toml:1: error DELETE01 scripts/use_it.py depends on the unit but is not a declared consumer
regen check: 1 units, 1 errors, 0 warnings
[exit 1]
$ babregen add-consumer core.calc scripts/use_it.py --note runs sub
wrote regen/units/core.calc.toml
next: run babregen check
[exit 0]
$ babregen check --strict
regen check: 1 units, 0 errors, 0 warnings
[exit 0]
Where to go next
Set up the editor, so that keys are completed and a wrong one is marked while you type (Editor support). Use babregen check --format json in CI (Check a repository). Draft the record of a module that has none with babregen new-unit (Draft a unit).