Files
leleka/scripts/limits/baseline.py
T
Pavlo Ratushnyi 334af80d3d STRA-367: limits gate for lawbook 301 (function 30 lines, complexity 10, nesting 4, width 120)
New and changed code is checked against the base (merge-base with the integration branch):
every added or touched function and every added or changed line must be within the limits,
unless limits-allowlist.json lists the function with a reason and an owner; an allowlisted
function may only shrink. The legacy that exists today is a per-crate census in
limits-baseline.json whose totals may only fall (same --update-baseline semantics as the
architecture gate). Rust is measured from the rustsplit lexer, the gate's own Python from ast.
56 self-tests, including a temporary git repository.
2026-11-06 11:26:53 +01:00

48 lines
2.0 KiB
Python

"""limits-baseline.json: the census that exists today. A total may only fall; a rewrite never raises one."""
import json
from . import census
COMMENT = ("The legacy census of scripts/check-limits.py: functions over each limit and lines over the width, per "
"scope (prod, test) and crate. A total over all crates may only fall (a function moved between crates "
"keeps it). "
"Do not edit by hand; regenerate with scripts/check-limits.py --update-baseline. Policy: limits.json.")
def empty():
return {"limits": {}, "census": {scope: {} for scope in census.SCOPES}}
def load(path):
return json.loads(path.read_text(encoding="utf-8")) if path.exists() else empty()
def save(path, data):
with open(path, "w", encoding="utf-8", newline="\n") as handle:
handle.write(json.dumps(data, indent=1) + "\n")
def build(counts, limits):
return {"_comment": COMMENT, "limits": dict(limits),
"census": {scope: dict(sorted(counts[scope].items())) for scope in census.SCOPES}}
def raised(old, new):
"""Problems for a total that would rise (the first baseline may be written freely)."""
before, after = census.totals(old["census"]), census.totals(new["census"])
return [f"{scope} {kind}: total {before[scope][kind]} -> {n}"
for scope in census.SCOPES for kind, n in after[scope].items() if n > before[scope][kind]]
def findings(counts, base):
"""(errors, stale): a total above the baseline is an error, one below it asks for a tighter baseline."""
now, before = census.totals(counts), census.totals(base["census"])
errors, stale = [], []
for scope in census.SCOPES:
for kind, n in now[scope].items():
old = before[scope][kind]
if n > old:
errors.append(f"census {scope} {kind} rose to {n} (baseline {old}); the count may only fall")
elif n < old:
stale.append(f"census {scope} {kind} fell from {old} to {n}: tighten the baseline")
return errors, stale