Skip to main content

Coming from spotregen

babregen 0.0.1 is the successor of spotregen 0.15.0, which gets no further release. This page is for a repository whose records were written for spotregen 0.15. It lists what changes and what to do about it. It is not a compatibility promise: babregen has no loader for old records, no legacy switch and no deprecated alias. The reasons are in ADR 0001.

Table 1: What changes.
what spotregen 0.15 babregen 0.0.1
package, command spotregen babregen
prose in a record line breaks kept; a backslash at every line end folded; no backslash
EVAL03, PROV01 the line of the intent item the line of the wrong name
message of a wrong name the message only the message and ; a similar name exists: 'X'
run record spotregen_version babregen_version
module of the writers spotregen.scaffold babregen.write
old run records (0.12) read with fallbacks not read
commands nine fifteen

The check IDs and their levels are the same: all 39 keep their ID and level, and the finding line {path}:{line}: {level} {id} {message} is unchanged. A script that calls the command needs its name changed.

Convert the records once

The conversion script is in the repository of babregen, scripts/convert_spotregen_0_15.py. It is not part of the wheel. It removes the line-end backslashes of the multi-line strings as a text edit, keeps comments and layout, and then proves by loading that every value is the one that spotregen 0.15 read:

python scripts/convert_spotregen_0_15.py regen --dry-run
python scripts/convert_spotregen_0_15.py regen

It writes a record only when no value of that record changes. It refuses, with the file, the line and the reason, a value that would change:

  • an inner line break without a backslash, which babregen folds into a space;
  • a blank line in a prose field, which is an error in babregen;
  • a line break in a name (an id, a reference, a path), which is an error in babregen;
  • whitespace at the edge of a prose value, which babregen strips;
  • a file that is not UTF-8 or not valid TOML.

A backslash that cannot go without changing the value stays. Fix a refused value by hand, then run the script again. The next cell converts a small set of records written in the 0.15 way. The first record holds an inner line break:

import subprocess
import sys
import tempfile
from pathlib import Path

from babregen import make_sample_repo

here = Path.cwd().resolve()
script = next(
    p / "scripts" / "convert_spotregen_0_15.py"
    for p in [here, *here.parents]
    if (p / "scripts" / "convert_spotregen_0_15.py").is_file()
)
tmp = tempfile.TemporaryDirectory()
root = make_sample_repo(Path(tmp.name) / "demo")
unit = root / "regen" / "units" / "core.calc.toml"
old = unit.read_text().replace(
    'summary = "Adds numbers."',
    'summary = """Adds numbers, \\\nand nothing else."""',
).replace(
    'invariant = "add(a, b) == a + b"',
    'invariant = """add(a, b) == a + b\nfor every pair of integers"""',
)
unit.write_text(old)


def convert(*args):
    """Run the conversion script on the regen/ directory of the demo repository."""
    done = subprocess.run(
        [sys.executable, str(script), "regen", *args],
        cwd=root,
        capture_output=True,
        text=True,
        check=False,
    )
    print("$ python scripts/convert_spotregen_0_15.py regen", *args)
    print((done.stdout + done.stderr).replace(str(root) + "/", "").rstrip())
    print(f"[exit {done.returncode}]")
    return done


refused = convert("--dry-run")
assert refused.returncode == 1
assert "the loaded value would change: intent.0.invariant" in refused.stderr
assert unit.read_text() == old
$ python scripts/convert_spotregen_0_15.py regen --dry-run
regen/units/core.calc.toml: 1 backslashes removed, 0 kept, not written
regen/units/core.calc.toml:11: the loaded value would change: intent.0.invariant (0.15 value 'add(a, b) == a + b\nfor every pair of integers')
[exit 1]

The dry run changes nothing. Join the two lines of the invariant, and convert:

unit.write_text(old.replace("a + b\nfor every", "a + b for every"))
done = convert()
assert done.returncode == 0
assert "\\" not in unit.read_text()
print("\n".join(unit.read_text().splitlines()[:2]))
$ python scripts/convert_spotregen_0_15.py regen
regen/units/core.calc.toml: 1 backslashes removed, 0 kept, written
[exit 0]
summary = """Adds numbers,
and nothing else."""

The script is meant to run once. Records that are folded already hold wrapped values without a backslash, which spotregen 0.15 read with line breaks, so a second run lists them and writes nothing. If some records are written for babregen already, name them with --skip.

Folded prose

In a prose field a line break and the blanks around it load as one space, so you write no backslash. Wrap a paragraph over several lines in a multi-line string. The rule and the list of the prose fields are on the schema page. A blank line inside a prose field is an error, and so is a line break in a name. A required text that is blank after stripping is an error as well, and so is an empty entry of a list of prose.

from babregen.spec import fold

assert fold("Adds numbers,\n    and sums them.\n") == "Adds numbers, and sums them."
print(fold("Adds numbers,\n    and sums them.\n"))
Adds numbers, and sums them.

A bundle changes when the value of a record changes, and folding does that only for a value with an inner line break. The values that the script accepts load as before, so their bundles stay the same.

Line numbers and suggestions

EVAL03, PROV01, PROV02, COMPILE06 and COMPILE08 report the line of the wrong name inside its array. In spotregen 0.15, EVAL03 and PROV01 reported the line of the intent item. A tool that matches the line of these two IDs must be adjusted. Line numbers are not part of the stability contract.

When a similar valid name exists (difflib, cutoff 0.75), the message of these five IDs and of an unknown key ends with ; a similar name exists: 'X'. A script that compares whole messages must read the suggestion instead from check --format json, where it is the field suggestion and the message does not repeat it:

import json

unit = root / "regen" / "units" / "core.calc.toml"
unit.write_text(unit.read_text().replace('"test_add"', '"test_ad"', 1))
done = subprocess.run(
    [sys.executable, "-m", "babregen", "--root", str(root), "check", "--format", "json"],
    capture_output=True,
    text=True,
    check=False,
)
finding = json.loads(done.stdout)["findings"][0]
assert (done.returncode, finding["id"], finding["suggestion"]) == (1, "EVAL03", "test_add")
print(finding["path"], finding["line"], finding["id"], finding["suggestion"])
regen/units/core.calc.toml 13 EVAL03 test_add

Run records

babregen score and babregen local write the key babregen_version where spotregen wrote spotregen_version. A run record is read as babregen writes it and in no other form: the tolerance for the old shapes of 0.12 is gone (a date instead of started, a dotted unit name). The archived runs of spotregen are not carried over; they stay evidence in the spotregen repository. The keys are listed on the schema page.

Module and function names

The module babregen.scaffold is babregen.write, which also holds the three new writers. scaffold_unit and write_unit keep their names and are importable from babregen; write_unit returns a Written, as the three new writers do, not a tuple. The drafts of new-unit carry two more comment lines. Finding has a sixth field, suggestion, and SpecError has the attributes detail and suggestion.

New commands

explain ID and example KIND teach the format and need no regen/ tree. new-decision, add-intent and add-consumer write a record without an editor: they create a file or append one complete block, check the names first, never rewrite existing text and have --dry-run. schema KIND prints the JSON Schema for the editor. check --format json is for CI. All of them are on the commands page.