Files
leleka/scripts/archcheck/baseline.py
T
Pavlo Ratushnyi f78b89757c STRA-338 S12a: inline unit-test modules move to sibling tests.rs files in 118 oversize files
scripts/dev/extract_tests.py moves a trailing cfg(test) mod tests block into foo/tests.rs for 118 files of leleka-runtime, cuda-driver, control-plane, gateway-text, deploy and nine smaller crates (31,000 lines), leaving production code and test ids untouched; 22 files that are themselves loaded through a path attribute carry an explicit path to their tests file. Not touched: the files the kar-off-ab branch edits (backend.rs, backend_original_serve.rs, backend_original_serve_replay.rs, qwen4exp_resident_expert_source.rs and seven deploy files).

Verified: cargo check --all-targets for the whole workspace on x86_64 Linux and on the Windows host; the 2429 library unit-test ids of the workspace are identical before and after (Linux listing diff empty); the loadable sections of leleka-inference, leleka-daemon, leleka-cli and strata-next are byte-identical between a release build before and after (scripts/dev/elf-sections-equal.sh), so the shipped code is the same. The architecture ratchets exclude tests.rs from the path-include count; the baseline drops from 329 to 311 oversize files and from 302,000 to 273,600 lines.
2026-11-05 15:00:09 +01:00

96 lines
3.7 KiB
Python

"""The baseline file: what may be written to it, and what a rewrite may never raise."""
import json
EMPTY = {"violations": [], "oversize": {}, "source": {}}
COMMENT = ("Debt that exists today. Every number may only fall (scripts/check-architecture.py --update-baseline "
"refuses to raise one). Do not edit by hand; regenerate. Policy: architecture.json.")
def load(path):
"""The baseline at `path`, or an empty one when the file does not exist."""
return json.loads(path.read_text(encoding="utf-8")) if path.exists() else json.loads(json.dumps(EMPTY))
def save(path, baseline):
with open(path, "w", encoding="utf-8", newline="\n") as handle:
handle.write(json.dumps(baseline, indent=1) + "\n")
def build(limit, violations, sizes, counts):
"""The baseline that describes the current state."""
return {
"_comment": COMMENT,
"violations": sorted(violations),
"oversize": {p: n for p, n in sorted(sizes.items()) if n > limit},
"source": dict(sorted(counts.items())),
}
def _stem_root(path, oversize):
"""The oversize file `path` was split from: a same-stem directory (responses.rs -> responses/*.rs)."""
parts = path.split("/")
for depth in range(len(parts) - 1, 0, -1):
root = "/".join(parts[:depth]) + ".rs"
if root in oversize:
return root
return None
SPLIT_OVERHEAD = 3 # lines a split adds per new file: the `mod` declaration and its attributes
def _family_lines(old, new):
"""Lines of each original oversize file plus its same-stem split files; every new file may add its declaration."""
totals = {}
for path, n in new.items():
root = path if path in old else _stem_root(path, old)
if root:
overhead = 0 if path in old else SPLIT_OVERHEAD
totals[root] = totals.get(root, 0) + n - overhead
return totals
def _moved(path, lines, old, new):
"""Same base name and no more lines than an oversize entry that disappeared: the file moved."""
name = path.rsplit("/", 1)[-1]
gone = [n for p, n in old.items() if p not in new and p.rsplit("/", 1)[-1] == name]
return any(lines <= before for before in gone)
def _oversize_problem(path, lines, old, new, family):
if path in old:
grew = lines > old[path] and family[path] > old[path]
return f"oversize {path}: {old[path]} -> {lines}" if grew else None
root = _stem_root(path, old)
if root:
too_big = family[root] > old[root]
return f"oversize {path}: split of {root} holds {family[root]} lines, was {old[root]}" if too_big else None
return None if _moved(path, lines, old, new) else f"oversize {path}: new file with {lines} lines"
def raised_oversize(old, new):
"""Problems for an oversize section that gained lines. Moves and same-stem splits are not gains."""
family = _family_lines(old, new)
found = [_oversize_problem(p, n, old, new, family) for p, n in new.items()]
return [problem for problem in found if problem]
def _totals(section):
totals = {}
for key, n in section.items():
pid = key.split(":", 1)[0]
totals[pid] = totals.get(pid, 0) + n
return totals
def raised_source(old, new):
"""Problems for a source ratchet whose total over all crates rose (code that moved crates keeps its total)."""
before, after = _totals(old), _totals(new)
return [f"source {pid}: total {before.get(pid, 0)} -> {n}" for pid, n in after.items() if n > before.get(pid, 0)]
def raised(old, new):
"""Everything a baseline rewrite would raise."""
problems = [f"new violation {v}" for v in new["violations"] if v not in old["violations"]]
return problems + raised_oversize(old["oversize"], new["oversize"]) + raised_source(old["source"], new["source"])