Files
leleka/scripts/archcheck/measure.py
T
Pavlo Ratushnyi 52d554e72c STRA-338 S6 part 2: SSE streaming leaves responses.rs; architecture gate rewritten to the 301 limits
The text, buffered and tool-call streaming code of the Responses API (750 lines) moves from control-plane/src/responses.rs to responses/stream.rs without a logic change, and the architecture gate becomes a small package (scripts/archcheck) whose functions stay within 30 lines, complexity 10, nesting 4 and 5 parameters.

The source counters now stop only at a column-0 cfg(test) module, so a single cfg(test) function no longer hides the code after it; that raises some measured baselines (cuda-driver, runtime, model-format, strata-control) without any code change, and the baseline was rewritten using --allow-increase for that reason alone.

Verified: cargo test -p strata-control 277 lib tests, 0 failed; 18 gate self-tests pass; fmt clean.
2026-11-05 13:47:14 +01:00

86 lines
3.1 KiB
Python

"""Measurements taken from the workspace: package graph, file sizes, source pattern counts."""
import json
import pathlib
import re
import subprocess
TEST_DIRS = ("tests",)
def load_metadata(root):
"""Workspace packages with their normal internal and external dependencies."""
cmd = ["cargo", "metadata", "--locked", "--no-deps", "--format-version", "1"]
meta = json.loads(subprocess.run(cmd, cwd=root, check=True, capture_output=True, text=True).stdout)
internal = {p["name"] for p in meta["packages"]}
return sorted((_package(p, internal) for p in meta["packages"]), key=lambda p: p["name"])
def _package(raw, internal):
normal = [d["name"] for d in raw["dependencies"] if d["kind"] is None]
return {
"name": raw["name"],
"dir": pathlib.Path(raw["manifest_path"]).parent,
"normal": sorted({n for n in normal if n in internal}),
"external": sorted({n for n in normal if n not in internal}),
}
def rust_files(directory):
"""Every .rs file under a directory except build output."""
for path in sorted(directory.rglob("*.rs")):
if "target" not in path.relative_to(directory).parts:
yield path
def _text(path):
return path.read_text(encoding="utf-8", errors="replace")
def file_lines(root, packages):
"""{relative posix path: line count} for every .rs file in a workspace package."""
sizes = {}
for pkg in packages:
for path in rust_files(pkg["dir"]):
sizes[path.relative_to(root).as_posix()] = len(_text(path).splitlines())
return sizes
def is_test_file(path, src):
"""A test module in its own file counts like the inline `#[cfg(test)]` module it replaces."""
parts = path.relative_to(src).parts
return path.name == "tests.rs" or path.name.endswith("_tests.rs") or any(d in parts[:-1] for d in TEST_DIRS)
def production_lines(text):
"""Lines before the first `#[cfg(test)]` module (column 0, followed by `mod`); comment lines are dropped."""
lines = text.splitlines()
for index, line in enumerate(lines):
if line.startswith("#[cfg(test)]") and index + 1 < len(lines) and lines[index + 1].startswith("mod "):
return [row for row in lines[:index] if not row.lstrip().startswith("//")]
return [row for row in lines if not row.lstrip().startswith("//")]
def count_patterns(packages, patterns):
"""{"<pattern id>:<crate>": n} for the non-zero counts, over production code in src/ only."""
compiled = [(p["id"], re.compile(p["regex"])) for p in patterns]
counts = {}
for pkg in packages:
for pid, n in _package_counts(pkg["dir"] / "src", compiled).items():
counts[f"{pid}:{pkg['name']}"] = n
return counts
def _package_counts(src, compiled):
totals = {}
if not src.is_dir():
return totals
for path in rust_files(src):
if is_test_file(path, src):
continue
rows = production_lines(_text(path))
for pid, rx in compiled:
n = sum(len(rx.findall(row)) for row in rows)
if n:
totals[pid] = totals.get(pid, 0) + n
return totals