Skip to main content

Draft a unit

import subprocess
import sys
import tempfile
from pathlib import Path

from babregen import make_sample_repo

tmp = tempfile.TemporaryDirectory()
root = make_sample_repo(Path(tmp.name) / "demo")


def babregen(*args, drop=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).splitlines()
    print("\n".join(x for x in lines if not (drop and drop in x)))
    print(f"[exit {done.returncode}]")
    return done

Definition 1 (The draft) babregen new-unit MODULE --pace P drafts regen/units/<stem>.toml for a module that has no record yet; the file name is the unit name without the package. Everything the tree can tell is filled in: the first docstring paragraph as summary, the public names (__all__, else public definitions and constants), the package imports as depends_on, and every file that references the module as a consumer. What only a person can supply is left open: the intent items, the decisions and the external consumers. A file tests/test_regen_eval_<module>.py is listed under evaluations, not under consumers; a unit without such a file keeps evaluations = [] and a comment that says where to write one.

First remove the record of the sample unit, so that the module is unrecorded:

(root / "regen" / "units" / "core.calc.toml").unlink()
done = babregen("check")
assert done.returncode == 0 and "0 units" in done.stdout
$ babregen check
regen/decisions/why-add.toml:1: warning COMPACT06 no intent item references this decision
regen check: 0 units, 0 errors, 1 warnings
[exit 0]

Example 1 (Look at the draft) --dry-run prints the draft instead of writing it. The header line carries today’s date, so it is left out here.

babregen("new-unit", "core.calc", "--pace", "slow", "--dry-run", drop="Drafted by")
$ babregen new-unit core.calc --pace slow --dry-run
# `babregen check` fails with INTENT01 until the first [[intent]] item is written.

summary = "Calculator."
pace = "slow"
public = ["add"]
depends_on = []
evaluations = ["tests/test_regen_eval_calc.py"]
unit_tests = []

# Intent: what must stay true for any implementation, and why. Ask "what would a
# competent stranger get wrong?" and turn every incident that touched the module into
# an item with an evaluation and a decision record. `babregen add-intent` appends one.
#
# [[intent]]
# id = "CAL-1"
# scope = ""  # where the requirement applies
# invariant = ""  # what must remain true
# reason = ""  # why the invariant matters
# evaluations = []  # test functions in the evaluation files above
# decisions = []  # ids of regen/decisions/<id>.toml

[[consumers]]
ref = "src/samplepkg/app/main.py"
kind = "code"
note = ""

[[consumers]]
ref = "docs/guide.qmd"
kind = "doc"
note = ""

# Add what the tree cannot show as kind = "external": downstream files
# (ref = "<repo>:<path>"), scheduled runs and people (a free-text ref).
# `babregen add-consumer` appends a consumer; say in its note what it relies on.
[exit 0]
CompletedProcess(args=['/Users/bartz/workspace/babregen-0.1.0/.venv/bin/python3', '-m', 'babregen', 'new-unit', 'core.calc', '--pace', 'slow', '--dry-run'], returncode=0, stdout='# Drafted by `babregen new-unit` on 2026-11-29. Review every field;\n# `babregen check` fails with INTENT01 until the first [[intent]] item is written.\n\nsummary = "Calculator."\npace = "slow"\npublic = ["add"]\ndepends_on = []\nevaluations = ["tests/test_regen_eval_calc.py"]\nunit_tests = []\n\n# Intent: what must stay true for any implementation, and why. Ask "what would a\n# competent stranger get wrong?" and turn every incident that touched the module into\n# an item with an evaluation and a decision record. `babregen add-intent` appends one.\n#\n# [[intent]]\n# id = "CAL-1"\n# scope = ""  # where the requirement applies\n# invariant = ""  # what must remain true\n# reason = ""  # why the invariant matters\n# evaluations = []  # test functions in the evaluation files above\n# decisions = []  # ids of regen/decisions/<id>.toml\n\n[[consumers]]\nref = "src/samplepkg/app/main.py"\nkind = "code"\nnote = ""\n\n[[consumers]]\nref = "docs/guide.qmd"\nkind = "doc"\nnote = ""\n\n# Add what the tree cannot show as kind = "external": downstream files\n# (ref = "<repo>:<path>"), scheduled runs and people (a free-text ref).\n# `babregen add-consumer` appends a consumer; say in its note what it relies on.\n', stderr='')

Example 2 (Write the draft and see the check fail)  

done = babregen("new-unit", "core.calc", "--pace", "slow")
assert done.returncode == 0
done = babregen("check")
assert done.returncode == 1 and "INTENT01" in done.stdout
$ babregen new-unit core.calc --pace slow
wrote regen/units/core.calc.toml
next: write the intent items and evaluations, then run babregen check
still open (babregen check will report):
regen/units/core.calc.toml:4: error INTENT01 declares no intent item (a unit without intent cannot be regenerated)
tests/test_regen_eval_calc.py:8: warning COMPACT05 samplepkg.core.calc: evaluation 'test_add' is not linked to any intent item
[exit 0]
$ babregen check
regen/units/core.calc.toml:4: error INTENT01 declares no intent item (a unit without intent cannot be regenerated)
tests/test_regen_eval_calc.py:8: warning COMPACT05 samplepkg.core.calc: evaluation 'test_add' is not linked to any intent item
regen/decisions/why-add.toml:1: warning COMPACT06 no intent item references this decision
regen check: 1 units, 1 errors, 2 warnings
[exit 1]

The draft deliberately fails the check (INTENT01) until the first intent item is written, so a draft never passes unreviewed.

Example 3 (Write the first intent item) Ask what a competent stranger would get wrong, state it with its reason, and name the evaluation that checks it and the decision record behind it. Here the sample already has an evaluation, test_add, in tests/test_regen_eval_calc.py, which the draft already lists, and a decision, why-add. add-intent appends the item. It takes the prose as options, checks that the evaluation and the decision exist before it writes, and prints what the check says about the record afterwards. A misspelled name is refused with the name it probably meant.

unit = root / "regen" / "units" / "core.calc.toml"
assert 'evaluations = ["tests/test_regen_eval_calc.py"]' in unit.read_text()
item = [
    "--scope", "add", "--invariant", "add(a, b) == a + b",
    "--reason", "Callers rely on plain arithmetic.",
    "--decision", "why-add",
]
done = babregen("add-intent", "core.calc", *item, "--evaluation", "test_ad")
assert done.returncode == 2 and "a similar name exists: 'test_add'" in done.stderr
done = babregen("add-intent", "core.calc", *item, "--evaluation", "test_add")
assert done.returncode == 0
done = babregen("check")
assert done.returncode == 0 and "0 errors" in done.stdout
$ babregen add-intent core.calc --scope add --invariant add(a, b) == a + b --reason Callers rely on plain arithmetic. --decision why-add --evaluation test_ad
babregen: evaluation 'test_ad' is not defined in the evaluation files of samplepkg.core.calc; a similar name exists: 'test_add'
[exit 2]
$ babregen add-intent core.calc --scope add --invariant add(a, b) == a + b --reason Callers rely on plain arithmetic. --decision why-add --evaluation test_add
wrote regen/units/core.calc.toml
next: run babregen check
[exit 0]
$ babregen check
regen check: 1 units, 0 errors, 0 warnings
[exit 0]
TipWhat the tool cannot see

Downstream repositories, scheduled runs and people depend on a unit too. Declare them as [[consumers]] of kind external (ref = "<repo>:<path>" for a file, free text for a scheduled run or a person).

The unit is now regenerable; see the bundle and scoring and regeneration.