STRA-338 S13: the source ratchets count production code only, not modules compiled for tests

The ratchet counted `#[path]` proof harnesses (gpu_proofs/, backend_original_serve_replay.rs) and
`#[ignore]` test files that sit outside a column-0 `#[cfg(test)] mod`, so the env-read count of
leleka-runtime (261) was mostly test-only reads that Settings cannot and should not remove.

measure.py now also skips a file declared as `mod x;` under a test cfg attribute (with or without
`#[path]`, multi-line `cfg(all(...))` included) and every file below such a module. A cfg(not(test))
module still counts. Unit test added. This is an accounting correction, not a code change: every
ratchet of the baseline can only fall (env-read leleka-runtime 261 to 101, leleka-cuda-driver 130
to 77, instant-now 79 to 45 and 45 to 31, dyn-trait, path-include, process-global), nothing rose.
This commit is contained in:
Pavlo Ratushnyi
2026-11-05 21:43:43 +01:00
parent 2be88ede85
commit a389ebb0eb
3 changed files with 91 additions and 10 deletions
+8 -8
View File
@@ -135,7 +135,7 @@
"source": {
"dyn-trait:leleka-backend-api": 1,
"dyn-trait:leleka-cpu-worker": 4,
"dyn-trait:leleka-cuda-driver": 51,
"dyn-trait:leleka-cuda-driver": 48,
"dyn-trait:leleka-deploy": 17,
"dyn-trait:leleka-experimental-ref": 3,
"dyn-trait:leleka-gptoss": 10,
@@ -144,13 +144,13 @@
"dyn-trait:leleka-gpu-npu-worker": 6,
"dyn-trait:leleka-memory": 17,
"dyn-trait:leleka-model-format": 8,
"dyn-trait:leleka-runtime": 70,
"dyn-trait:leleka-runtime": 68,
"dyn-trait:leleka-tokenizer": 2,
"dyn-trait:strata-control": 35,
"dyn-trait:strata-desktop-core": 2,
"env-read:leleka-cpu-kernels": 1,
"env-read:leleka-cpu-worker": 2,
"env-read:leleka-cuda-driver": 130,
"env-read:leleka-cuda-driver": 77,
"env-read:leleka-deploy": 41,
"env-read:leleka-gateway-text": 2,
"env-read:leleka-gptoss": 6,
@@ -165,13 +165,13 @@
"env-read:leleka-npu-service": 1,
"env-read:leleka-npu-worker": 1,
"env-read:leleka-obs": 2,
"env-read:leleka-runtime": 261,
"env-read:leleka-runtime": 101,
"env-read:leleka-tokenizer": 2,
"env-read:strata-control": 19,
"env-read:strata-desktop-core": 15,
"instant-now:leleka-backend-api": 2,
"instant-now:leleka-cpu-worker": 9,
"instant-now:leleka-cuda-driver": 45,
"instant-now:leleka-cuda-driver": 31,
"instant-now:leleka-deploy": 28,
"instant-now:leleka-gptoss": 4,
"instant-now:leleka-gptoss-igpu": 5,
@@ -182,13 +182,13 @@
"instant-now:leleka-model-format": 10,
"instant-now:leleka-native-cpu-expert": 30,
"instant-now:leleka-npu-service": 5,
"instant-now:leleka-runtime": 79,
"instant-now:leleka-runtime": 45,
"instant-now:strata-control": 37,
"instant-now:strata-desktop-core": 15,
"path-include:leleka-cuda-driver": 115,
"path-include:leleka-model-format": 12,
"path-include:leleka-native-cpu-expert": 2,
"path-include:leleka-runtime": 107,
"path-include:leleka-runtime": 102,
"path-include:strata-desktop-core": 2,
"process-global:leleka-cuda-driver": 110,
"process-global:leleka-gateway-text": 5,
@@ -198,7 +198,7 @@
"process-global:leleka-model-format": 8,
"process-global:leleka-native-cpu-expert": 32,
"process-global:leleka-obs": 7,
"process-global:leleka-runtime": 114,
"process-global:leleka-runtime": 113,
"process-global:leleka-tokenizer": 4,
"process-global:strata-control": 14
}
+66 -2
View File
@@ -61,7 +61,7 @@ def production_lines(text):
def count_patterns(packages, patterns):
"""{"<pattern id>:<crate>": n} for the non-zero counts, over production code in src/ only."""
"""{"<pattern id>:<crate>": n} for the non-zero counts, over production code in src/ only (test files and modules declared under a test cfg are skipped)."""
compiled = [(p["id"], re.compile(p["regex"])) for p in patterns]
counts = {}
for pkg in packages:
@@ -70,12 +70,76 @@ def count_patterns(packages, patterns):
return counts
_MOD_LINE = re.compile(r"(?:pub(?:\([^)]*\))?\s+)?mod\s+(\w+)\s*;")
_PATH_ATTR = re.compile(r'#\[path\s*=\s*"([^"]+)"\]')
def test_only_modules(src):
"""Files compiled only for tests: a `mod x;` declared under a `#[cfg(...test...)]` attribute (with or without a
`#[path]`). `_under_test_module` extends this to the files below such a module."""
found = set()
for path in rust_files(src):
for name, explicit in _test_declarations(_text(path)):
target = _module_file(path, name, explicit)
if target is not None:
found.add(target)
return found
def _test_declarations(text):
"""[(module name, #[path] value or None)] of the `mod x;` lines whose attribute block is a test cfg."""
out = []
attrs = []
for line in text.splitlines():
stripped = line.strip()
if stripped.startswith("#[") or (attrs and _unbalanced(" ".join(attrs))):
attrs.append(stripped)
continue
decl = _MOD_LINE.match(stripped)
if decl and _is_test_cfg(" ".join(attrs)):
explicit = _PATH_ATTR.search(" ".join(attrs))
out.append((decl.group(1), explicit.group(1) if explicit else None))
attrs = []
return out
def _unbalanced(text):
return text.count("[") > text.count("]") or text.count("(") > text.count(")")
def _is_test_cfg(block):
return "cfg(" in block and re.search(r"\btest\b", block) is not None and "not(test" not in block
def _module_file(declaring, name, explicit):
here = declaring.parent
if explicit:
target = (here / explicit).resolve()
return target if target.is_file() else None
base = here if declaring.name in ("lib.rs", "main.rs", "mod.rs") else here / declaring.stem
for candidate in (base / f"{name}.rs", base / name / "mod.rs"):
if candidate.is_file():
return candidate.resolve()
return None
def _under_test_module(path, test_files):
"""The file itself or a file in the directory a test-only module owns (`x.rs` owns `x/`, `mod.rs` its folder)."""
resolved = path.resolve()
for owner in test_files:
folder = owner.parent if owner.name == "mod.rs" else owner.with_suffix("")
if resolved == owner or resolved.is_relative_to(folder):
return True
return False
def _package_counts(src, compiled):
totals = {}
if not src.is_dir():
return totals
test_files = test_only_modules(src)
for path in rust_files(src):
if is_test_file(path, src):
if is_test_file(path, src) or _under_test_module(path, test_files):
continue
rows = production_lines(_text(path))
for pid, rx in compiled:
+17
View File
@@ -148,6 +148,23 @@ class Measurement(unittest.TestCase):
counts = measure.count_patterns([package], [{"id": "e", "regex": r"env::var\("}])
self.assertEqual(counts, {"e:p": 1})
def test_modules_declared_under_a_test_cfg_are_not_production(self):
with tempfile.TemporaryDirectory() as tmp:
src = pathlib.Path(tmp) / "src"
(src / "a" / "proofs").mkdir(parents=True)
(src / "lib.rs").write_text('mod a;\nmod b;\nlet a = env::var("A");\n')
(src / "b.rs").write_text('let b = env::var("B");\n')
(src / "a.rs").write_text(
'let c = env::var("C");\n#[cfg(not(test))]\nmod live;\n'
'#[cfg(all(\n test,\n target_os = "linux"\n))]\n#[path = "a/proofs/x.rs"]\nmod proof;\n'
'#[cfg(test)]\nmod helper;\n')
(src / "a" / "proofs" / "x.rs").write_text('let d = env::var("D");\n')
(src / "a" / "helper.rs").write_text('let e = env::var("E");\n')
(src / "a" / "live.rs").write_text('let f = env::var("F");\n')
package = dict(pkg("p"), dir=pathlib.Path(tmp))
counts = measure.count_patterns([package], [{"id": "e", "regex": r"env::var\("}])
self.assertEqual(counts, {"e:p": 4})
if __name__ == "__main__":
unittest.main()