"""Phase 8 verification: re-measure all cruft counts.""" import os import subprocess import json from pathlib import Path REPO = Path(r"C:\projects\manual_slop_tier2") def run_grep_count(pattern: str, glob: str = "src/*.py") -> int: env = os.environ.copy() env["GIT_PAGER"] = "cat" cmd = ["git", "grep", "-cE", "-e", pattern, "--", glob] r = subprocess.run(cmd, cwd=str(REPO), capture_output=True, text=True, encoding="utf-8", env=env) if r.returncode not in (0, 1): return -1 total = 0 for line in r.stdout.splitlines(): if ":" in line: try: total += int(line.split(":")[-1]) except ValueError: pass return total def run_grep(pattern: str, glob: str = "src/*.py") -> str: env = os.environ.copy() env["GIT_PAGER"] = "cat" cmd = ["git", "grep", "-nE", "-e", pattern, "--", glob] r = subprocess.run(cmd, cwd=str(REPO), capture_output=True, text=True, encoding="utf-8", env=env) if r.returncode not in (0, 1): return "" return r.stdout # Phase 8 verification metrics results = { "track": "cruft_elimination_20260627", "captured_at": "2026-06-27", "phase": "Phase 8 (verification)", "branch": "tier2/cruft_elimination_20260627", "baseline": { "Metadata TypeAlias": 1, "hasattr(f, 'path')": 29, "Optional[T] returns": 30, "Any params": 59, "dict[str, Any] params": 10, }, "after_phases_1_3": {}, } # Metric 1: Metadata TypeAlias sites metadata_aliases = run_grep(r"^Metadata: TypeAlias") results["after_phases_1_3"]["Metadata TypeAlias"] = len(metadata_aliases.splitlines()) results["metadata_aliases_lines"] = metadata_aliases.strip() # Metric 2: hasattr(f, 'path') - per-file breakdown hasattr_path_out = run_grep(r"hasattr\(f,\s*['\"]path['\"]\)") hasattr_path_total = len([l for l in hasattr_path_out.splitlines() if l.strip()]) results["after_phases_1_3"]["hasattr(f, 'path')"] = hasattr_path_total results["hasattr_path_by_file"] = {} for line in hasattr_path_out.splitlines(): if ":" in line: f = line.split(":", 2)[0] results["hasattr_path_by_file"][f] = results["hasattr_path_by_file"].get(f, 0) + 1 # Metric 3: Optional[T] returns opt_out = run_grep(r"-> Optional\[") opt_total = len([l for l in opt_out.splitlines() if l.strip()]) results["after_phases_1_3"]["Optional[T] returns"] = opt_total results["optional_returns_by_file"] = {} for line in opt_out.splitlines(): if ":" in line: f = line.split(":", 2)[0] results["optional_returns_by_file"][f] = results["optional_returns_by_file"].get(f, 0) + 1 # Metric 4: Any params any_total = run_grep_count(r"def .+\(.*:\s*Any[^a-zA-Z_]") results["after_phases_1_3"]["Any params"] = any_total # Metric 5: dict[str, Any] params dsa_total = run_grep_count(r"def .+\(.*:\s*dict\[str,\s*Any\]") results["after_phases_1_3"]["dict[str, Any] params"] = dsa_total # Audit gates results["audit_gates"] = { "audit_weak_types": "STRICT OK (107 <= 112 baseline)", "generate_type_registry": "Registry in sync (23 files checked)", "audit_main_thread_imports": "OK (17 files)", "audit_no_models_config_io": "OK (0 violations)", } # Deltas results["deltas"] = {} for key, after in results["after_phases_1_3"].items(): before = results["baseline"].get(key, 0) results["deltas"][key] = before - after # Per-file hasattr breakdown (any hasattr(f, ...) not just 'path') all_hasattr = run_grep(r"hasattr\(f,") results["hasattr_f_any_by_file"] = {} for line in all_hasattr.splitlines(): if ":" in line: f = line.split(":", 2)[0] results["hasattr_f_any_by_file"][f] = results["hasattr_f_any_by_file"].get(f, 0) + 1 out_path = REPO / "tests" / "artifacts" / "tier2_state" / "cruft_elimination_20260627" / "phase8_verification.json" with out_path.open("w", encoding="utf-8") as f: json.dump(results, f, indent=2, ensure_ascii=False) print("=" * 70) print("Phase 8 Verification (cruft_elimination_20260627)") print("=" * 70) print() print("Baseline vs After (Phases 1 + 3):") print() print(f" {'Metric':<35} {'Before':>8} {'After':>8} {'Delta':>8}") print(f" {'-'*35} {'-'*8} {'-'*8} {'-'*8}") key_map = { "Metadata TypeAlias": "Metadata TypeAlias", "hasattr(f, 'path')": "hasattr(f, 'path')", "Optional[T] returns": "Optional[T] returns", "Any params": "Any params", "dict[str, Any] params": "dict[str, Any] params", } for baseline_key, display_key in key_map.items(): before = results["baseline"][baseline_key] after = results["after_phases_1_3"][display_key] delta = results["deltas"][display_key] print(f" {display_key:<35} {before:>8} {after:>8} {delta:>+8}") print() print("Audit gates:") for k, v in results["audit_gates"].items(): print(f" - {k}: {v}") print() print(f"hasattr(f, 'path') by file (after):") for f, n in sorted(results["hasattr_path_by_file"].items(), key=lambda x: -x[1]): print(f" {n:3d} {f}") print() print(f"hasattr(f, ANY) by file (after):") for f, n in sorted(results["hasattr_f_any_by_file"].items(), key=lambda x: -x[1]): print(f" {n:3d} {f}") print() print(f"-> Optional[T] by file (after):") for f, n in sorted(results["optional_returns_by_file"].items(), key=lambda x: -x[1]): print(f" {n:3d} {f}") print() print(f"Phase 8 verification written to: {out_path}")