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 doneThe regeneration bundle
Definition 1 (The regeneration bundle) babregen context UNIT writes everything a generator needs and nothing it must not copy: the architecture (allowed imports, conventions, open choices, pace policy), the unit’s purpose and intent, the decisions behind it, the public contract (signatures and docstrings, no bodies), the contracts of its dependencies, the source of its durable evaluations and its consumers. The sections come in this order; a unit without depends_on entries has no section for dependencies.
Example 1 (The outline of a bundle)
done = subprocess.run(
[sys.executable, "-m", "babregen", "context", "core.calc"],
cwd=root,
capture_output=True,
text=True,
check=True,
)
bundle = done.stdout
print("\n".join(line for line in bundle.splitlines() if line.startswith("#")))# Regeneration bundle: samplepkg.core.calc
## Purpose
## Architecture (compilation target)
## Intent
### C-1
## Provenance
### why-add: Addition is needed
## Public contract (signatures and docstrings, no bodies)
## Durable evaluations
## Consumers (must keep working)
Example 2 (The contract without the implementation)
start = bundle.index("## Public contract")
print(bundle[start : bundle.index("## Durable evaluations")])
print("implementation leaked:", "return a + b" in bundle)## Public contract (signatures and docstrings, no bodies)
```python
"""Calculator."""
def add(a: int, b: int) -> int:
"""Add two numbers."""
...
```
implementation leaked: False
The bundle shows def add(a: int, b: int) -> int with its docstring and a ... body; the private _helper is absent. A stub holds an expression only in the signature (parameters, annotations, default values, decorators, base classes) and in docstrings: the value of a class-level assignment is ..., whatever it is. A name in public that a top-level assignment binds is in the contract as NAME = <value> when the value is a literal and as NAME = ... otherwise, because the record declared the name public.
Example 3 (The contracts of the dependencies) A model that gets the bundle alone must not guess the signatures of what it may import. Every entry of depends_on therefore adds the contract of that module to the bundle: the one name of a module.name entry, the public names of a module that is a unit, else the names in __all__, else the public functions and classes of the module.
limits = root / "src" / "samplepkg" / "core" / "limits.py"
limits.write_text(
'"""Limits."""\n\nLIMIT = 100\n\n\n'
"def within(value: int, limit: int = LIMIT) -> bool:\n"
' """Whether the value is at most the limit."""\n'
" return value <= limit\n"
)
record = root / "regen" / "units" / "core.calc.toml"
record.write_text(
record.read_text().replace("depends_on = []", 'depends_on = ["samplepkg.core.limits"]')
)
done = subprocess.run(
[sys.executable, "-m", "babregen", "context", "core.calc"],
cwd=root,
capture_output=True,
text=True,
check=True,
)
print([x for x in done.stdout.splitlines() if "may import:" in x and "unit" in x])
start = done.stdout.index("## Contracts of dependencies")
print(done.stdout[start : done.stdout.index("## Durable evaluations")])
print("implementation leaked:", "return value <= limit" in done.stdout)['- this unit may import: samplepkg.core.limits']
## Contracts of dependencies
### samplepkg.core.limits
```python
LIMIT = 100
def within(value: int, limit: int=LIMIT) -> bool:
"""Whether the value is at most the limit."""
...
```
implementation leaked: False
The line - this unit may import: of the architecture section names the same entries, a module as its dotted name and the definitions of one module together, as in from samplepkg.core.limits: LIMIT, within for the entries samplepkg.core.limits.LIMIT and samplepkg.core.limits.within; the subsection of such an entry is headed ### LIMIT (from samplepkg.core.limits). A dotted name whose last part is a definition is therefore never read as a module, and a generator writes from samplepkg.core.limits import LIMIT.
The bundle size grows with the contracts. A model with a small context window needs a narrower depends_on (module.name entries) or a shorter public list: that is a change of the record, not an option of the command.
Example 4 (Write it to a file and record its hash)
import hashlib
babregen("context", "core.calc", "-o", "bundle.md")
data = (root / "bundle.md").read_bytes()
print(hashlib.sha256(data).hexdigest()[:16], len(data), "bytes")$ babregen context core.calc -o bundle.md
wrote bundle.md
[exit 0]
cbf53c0092c7bc40 1515 bytes
The bundle is deterministic Markdown. babregen score and babregen local record its SHA-256 (bundle_sha256 in run.toml), so a run can be tied to the exact input it was given. A release that changes the bytes of the bundle says so (it is part of the stability contract), and babregen check then reports PROV08 for the archived runs of the units whose bundle changed.
pytest -m regen and the unit’s own tests pass and babregen check stays clean. The pace of the unit decides what else is needed (see the [pace] table of the architecture). When the generator hits an ambiguity, fix the spec first; do not patch the generated code.
To let an LLM do the generation, see scoring and regeneration. Next: compaction.