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 doneCompaction
Definition 1 (Compaction) Knowledge accumulates sediment. babregen compact is a filter of babregen check: it prints exactly the findings whose ID starts with COMPACT, and nothing is lost by it. They are a listed subpackage that does not exist (COMPACT01), an allowed edge that no import uses (COMPACT02), a known cycle that is gone (COMPACT03), an unused depends_on entry (COMPACT04), an evaluation that no intent item uses (COMPACT05), an active decision that nothing cites (COMPACT06) and a consumer that no longer references its unit (COMPACT07). Each is a candidate to remove, merge or re-examine. The command always exits 0.
Example 1 (Nothing to compact)
done = babregen("compact")
assert done.returncode == 0 and "0 candidates" in done.stdout$ babregen compact
regen compact: 0 candidates
[exit 0]
Example 2 (Two kinds of sediment)
# a consumer that no longer mentions the unit
(root / "docs" / "guide.qmd").write_text("No mention any more.\n")
# a decision nobody cites
decision = root / "regen" / "decisions" / "why-add.toml"
orphan = root / "regen" / "decisions" / "orphan.toml"
orphan.write_text(decision.read_text().replace("Addition is needed", "An orphan"))
done = babregen("compact")
assert done.returncode == 0
assert "COMPACT06" in done.stdout and "COMPACT07" in done.stdout$ babregen compact
regen/units/core.calc.toml:21: warning COMPACT07 consumer docs/guide.qmd no longer references the unit
regen/decisions/orphan.toml:1: warning COMPACT06 no intent item references this decision
regen compact: 2 candidates
[exit 0]
The same lines appear as warnings in babregen check; compact shows only them. A decision past its review date (PROV06) and an archived run that judged another bundle or module (PROV08, PROV09) are warnings of check, but they are provenance and not sediment, so compact does not list them. Run compact before a release, and turn each line into an edit of the spec or a deliberate decision to keep it.
Next: measures.