Private
Public Access
TIER-2 READ all 11 mandatory pre-flight files before <cruft_elimination_20260627>: 1. AGENTS.md 2. conductor/workflow.md 3. conductor/edit_workflow.md 4. conductor/tier2/githooks/forbidden-files.txt 5. conductor/tracks/tier2_leak_prevention_20260620/spec.md 6. conductor/product-guidelines.md (Core Value section) 7. conductor/code_styleguides/data_oriented_design.md (DOD + \u00a78.5) 8. conductor/code_styleguides/python.md (\u00a717 Banned Patterns) 9. conductor/code_styleguides/type_aliases.md 10. conductor/code_styleguides/error_handling.md 11. docs/guide_meta_boundary.md Also read: agent_memory_dimensions.md, rag_integration_discipline.md, cache_friendly_context.md, knowledge_artifacts.md, feature_flags.md, workspace_paths.md, config_state_owner.md Phase 0 baseline (measured 2026-06-27, master88a1bdcb): - Metadata: TypeAlias = dict[str, Any] at src/type_aliases.py:6 (Phase 1 target) - hasattr(f, 'path') sites: 29 (gui_2.py:18, app_controller.py:10, aggregate.py:1) - -> Optional[T] returns: 30 across 14 files - Any params: 59 - dict[str, Any] params: 10 - Metadata params: 51 - All 7 audit gates pass --strict - 17/18 per-aggregate dataclasses have from_dict() (NormalizedResponse is an output type, not wire-boundary; doesn't need from_dict) Branch: tier2/cruft_elimination_20260627 (from origin/master @88a1bdcb)
113 lines
4.9 KiB
Python
113 lines
4.9 KiB
Python
"""Capture pre-flight baseline counts for cruft_elimination_20260627."""
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
REPO = Path(r"C:\projects\manual_slop_tier2")
|
|
|
|
def run_grep(pattern: str, glob: str = "src/*.py") -> str:
|
|
"""Run git grep and return stdout. Uses -e flag to avoid '>' being interpreted as switch."""
|
|
import os
|
|
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): # 0 = found, 1 = not found
|
|
return f"ERROR (rc={r.returncode}): {r.stderr}"
|
|
return r.stdout
|
|
|
|
def run_grep_count(pattern: str, glob: str = "src/*.py") -> int:
|
|
"""Count git grep matches."""
|
|
import os
|
|
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
|
|
|
|
baseline = {
|
|
"track": "cruft_elimination_20260627",
|
|
"captured_at": "2026-06-27",
|
|
"src_files": sorted([p.name for p in (REPO / "src").glob("*.py")]),
|
|
}
|
|
|
|
# Phase 1: Metadata TypeAlias
|
|
metadata_baseline = run_grep(r"^Metadata: TypeAlias", "src/type_aliases.py")
|
|
baseline["metadata_typealias_lines"] = metadata_baseline.strip()
|
|
|
|
# Phase 1-3: hasattr(f, ...) defensive checks
|
|
baseline["hasattr_f_path"] = run_grep_count(r"hasattr\(f,\s*['\"]path['\"]\)")
|
|
baseline["hasattr_f_source_tier"] = run_grep_count(r"hasattr\(f,\s*['\"]source_tier['\"]\)")
|
|
baseline["hasattr_f_content"] = run_grep_count(r"hasattr\(f,\s*['\"]content['\"]\)")
|
|
baseline["hasattr_f_role"] = run_grep_count(r"hasattr\(f,\s*['\"]role['\"]\)")
|
|
baseline["hasattr_f_model"] = run_grep_count(r"hasattr\(f,\s*['\"]model['\"]\)")
|
|
baseline["hasattr_f_id"] = run_grep_count(r"hasattr\(f,\s*['\"]id['\"]\)")
|
|
baseline["hasattr_f_status"] = run_grep_count(r"hasattr\(f,\s*['\"]status['\"]\)")
|
|
baseline["hasattr_f_total"] = sum([
|
|
baseline["hasattr_f_path"], baseline["hasattr_f_source_tier"],
|
|
baseline["hasattr_f_content"], baseline["hasattr_f_role"],
|
|
baseline["hasattr_f_model"], baseline["hasattr_f_id"],
|
|
baseline["hasattr_f_status"],
|
|
])
|
|
baseline["hasattr_self_lazy_init"] = run_grep_count(r"hasattr\(self,")
|
|
|
|
# Phase 6: Optional[T] returns
|
|
baseline["optional_returns"] = run_grep_count(r"-> Optional\[")
|
|
|
|
# Phase 7: Any and dict[str, Any] in signatures
|
|
baseline["any_params"] = run_grep_count(r"def .+\(.*:\s*Any[^a-zA-Z_]")
|
|
baseline["any_returns"] = run_grep_count(r"->\s*Any[^a-zA-Z_]")
|
|
baseline["dict_str_any_params"] = run_grep_count(r"def .+\(.*:\s*dict\[str,\s*Any\]")
|
|
baseline["metadata_params"] = run_grep_count(r"def .+\(.*:\s*Metadata[^a-zA-Z_]")
|
|
baseline["metadata_returns"] = run_grep_count(r"->\s*Metadata[^a-zA-Z_]")
|
|
|
|
# Per-file breakdowns for the major cruft sources
|
|
def per_file_breakdown(pattern: str) -> dict[str, int]:
|
|
out = run_grep(pattern)
|
|
result: dict[str, int] = {}
|
|
for line in out.splitlines():
|
|
if ":" in line and not line.startswith("ERROR"):
|
|
parts = line.split(":", 2)
|
|
if len(parts) >= 2:
|
|
fpath = parts[0]
|
|
result[fpath] = result.get(fpath, 0) + 1
|
|
return result
|
|
|
|
baseline["optional_returns_by_file"] = per_file_breakdown(r"-> Optional\[")
|
|
baseline["hasattr_f_path_by_file"] = per_file_breakdown(r"hasattr\(f,\s*['\"]path['\"]\)")
|
|
|
|
baseline["summary"] = {
|
|
"metadata_typealias_lines": baseline["metadata_typealias_lines"],
|
|
"total_hasattr_f_path": baseline["hasattr_f_path"],
|
|
"total_hasattr_f_all_fields": baseline["hasattr_f_total"],
|
|
"total_hasattr_self_lazy_init": baseline["hasattr_self_lazy_init"],
|
|
"total_optional_returns": baseline["optional_returns"],
|
|
"total_any_params": baseline["any_params"],
|
|
"total_any_returns": baseline["any_returns"],
|
|
"total_dict_str_any_params": baseline["dict_str_any_params"],
|
|
"total_metadata_params": baseline["metadata_params"],
|
|
"total_metadata_returns": baseline["metadata_returns"],
|
|
}
|
|
|
|
out_path = REPO / "tests" / "artifacts" / "tier2_state" / "cruft_elimination_20260627" / "baseline_counts.json"
|
|
out_path.parent.mkdir(parents=True, exist_ok=True)
|
|
with out_path.open("w", encoding="utf-8") as f:
|
|
json.dump(baseline, f, indent=2, ensure_ascii=False)
|
|
|
|
print(json.dumps(baseline["summary"], indent=2))
|
|
print("\n--- hasattr(f, 'path') by file ---")
|
|
for f, n in sorted(baseline["hasattr_f_path_by_file"].items(), key=lambda x: -x[1]):
|
|
print(f" {n:3d} {f}")
|
|
print("\n--- -> Optional[...] by file ---")
|
|
for f, n in sorted(baseline["optional_returns_by_file"].items(), key=lambda x: -x[1]):
|
|
print(f" {n:3d} {f}")
|
|
print(f"\nBaseline written to: {out_path}") |