| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -euo pipefail |
| 4 |
|
| 5 |
target="${1:-.}" |
| 6 |
bin="${THOTH_BIN:-thoth}" |
| 7 |
|
| 8 |
if [[ ! -e "$target" ]]; then |
| 9 |
echo "error: target not found: $target" >&2 |
| 10 |
exit 1 |
| 11 |
fi |
| 12 |
if [[ ! -d "$target" ]]; then |
| 13 |
echo "error: target must be a directory: $target" >&2 |
| 14 |
exit 1 |
| 15 |
fi |
| 16 |
if ! command -v "$bin" >/dev/null 2>&1; then |
| 17 |
echo "error: thoth binary not found: $bin; set THOTH_BIN or install thoth" >&2 |
| 18 |
exit 1 |
| 19 |
fi |
| 20 |
|
| 21 |
report="$(mktemp)" |
| 22 |
prod_tree="$(mktemp -d)" |
| 23 |
trap 'rm -f "$report"; rm -rf "$prod_tree"' EXIT |
| 24 |
|
| 25 |
analysis_target="$(python3 - "$target" "$prod_tree" <<'PY' |
| 26 |
import shutil |
| 27 |
import sys |
| 28 |
from pathlib import Path |
| 29 |
|
| 30 |
source = Path(sys.argv[1]) |
| 31 |
dest = Path(sys.argv[2]) / "clnkr-production" |
| 32 |
|
| 33 |
def ignored(path, names): |
| 34 |
current = Path(path) |
| 35 |
rejected = set() |
| 36 |
for name in names: |
| 37 |
child = current / name |
| 38 |
rel_text = child.relative_to(source).as_posix() if source in child.parents or child == source else child.as_posix() |
| 39 |
if name in {".git", ".worktrees", "build"} or rel_text == "site/public": |
| 40 |
rejected.add(name) |
| 41 |
elif name.endswith("_test.go"): |
| 42 |
rejected.add(name) |
| 43 |
return rejected |
| 44 |
|
| 45 |
shutil.copytree(source, dest, ignore=ignored) |
| 46 |
print(dest) |
| 47 |
PY |
| 48 |
)" |
| 49 |
|
| 50 |
if [[ "${THOTH_DEBUG_INPUT:-}" == "1" ]]; then |
| 51 |
echo "debug: semantic cohesion input copied to $analysis_target" >&2 |
| 52 |
fi |
| 53 |
|
| 54 |
"$bin" "$analysis_target" --report > "$report" |
| 55 |
|
| 56 |
python3 - "$report" <<'PY' |
| 57 |
import json |
| 58 |
import re |
| 59 |
import sys |
| 60 |
from collections import Counter, defaultdict |
| 61 |
from json import JSONDecodeError |
| 62 |
from pathlib import Path |
| 63 |
|
| 64 |
try: |
| 65 |
data = json.loads(Path(sys.argv[1]).read_text()) |
| 66 |
except JSONDecodeError as exc: |
| 67 |
print(f"error: thoth --report did not return valid JSON: {exc}", file=sys.stderr) |
| 68 |
sys.exit(1) |
| 69 |
|
| 70 |
missing = [key for key in ("module_health", "functions") if key not in data] |
| 71 |
if missing: |
| 72 |
print(f"error: thoth --report missing expected keys: {', '.join(missing)}", file=sys.stderr) |
| 73 |
sys.exit(1) |
| 74 |
|
| 75 |
modules = data["module_health"] |
| 76 |
functions = data["functions"] |
| 77 |
|
| 78 |
stop = { |
| 79 |
"init", "test", "main", "run", "write", "read", "build", "get", "set", |
| 80 |
"put", "new", "old", "json", "text", "file", "files", "path", "node", |
| 81 |
"nodes", "list", "map", "array", "entry", "items", "item", "ctx", "self", |
| 82 |
"data", "root", "from", "into", "with", "count", "name", "type", |
| 83 |
"function", "string", "error", "context", "options", "config", "request", |
| 84 |
"response", "result", "results", "message", "messages", "command", |
| 85 |
"commands", "event", "events", |
| 86 |
} |
| 87 |
|
| 88 |
def identifier_terms(text): |
| 89 |
normalized = re.sub(r"[^0-9A-Za-z]+", " ", text) |
| 90 |
pieces = [] |
| 91 |
for raw in normalized.split(): |
| 92 |
pieces.extend(re.findall( |
| 93 |
r"[A-Z]+(?=[A-Z][a-z]|[0-9]|$)|[A-Z]?[a-z]+|[0-9]+", |
| 94 |
raw, |
| 95 |
)) |
| 96 |
out = [] |
| 97 |
for piece in pieces: |
| 98 |
lowered = piece.lower() |
| 99 |
if len(lowered) >= 4 and lowered not in stop: |
| 100 |
out.append(lowered) |
| 101 |
return out |
| 102 |
|
| 103 |
by_file = defaultdict(Counter) |
| 104 |
term_files = defaultdict(set) |
| 105 |
file_sloc = {} |
| 106 |
file_funcs = {} |
| 107 |
|
| 108 |
for module in modules: |
| 109 |
path = module.get("path", "") |
| 110 |
file_sloc[path] = int(module.get("sloc", 0)) |
| 111 |
file_funcs[path] = int(module.get("funcs", 0)) |
| 112 |
|
| 113 |
for fn in functions: |
| 114 |
path = fn.get("file", "") |
| 115 |
for term in identifier_terms(fn.get("name", "")): |
| 116 |
by_file[path][term] += 2 |
| 117 |
term_files[term].add(path) |
| 118 |
|
| 119 |
print("semantic cohesion report") |
| 120 |
print("mode: manual-report-only") |
| 121 |
|
| 122 |
print("file_terms:") |
| 123 |
for path in sorted(by_file): |
| 124 |
common = ", ".join(f"{term}:{count}" for term, count in by_file[path].most_common(8)) |
| 125 |
print(f"- {path}: {common if common else 'none'}") |
| 126 |
|
| 127 |
scattered = [ |
| 128 |
(term, sorted(paths)) |
| 129 |
for term, paths in term_files.items() |
| 130 |
if len(paths) >= 3 |
| 131 |
] |
| 132 |
scattered.sort(key=lambda item: (-len(item[1]), item[0])) |
| 133 |
|
| 134 |
print("scattered_terms:") |
| 135 |
if scattered: |
| 136 |
for term, paths in scattered[:20]: |
| 137 |
print(f"- {term}: files={len(paths)} paths={', '.join(paths[:8])}") |
| 138 |
else: |
| 139 |
print("- none") |
| 140 |
|
| 141 |
mixed = [] |
| 142 |
for path, counts in by_file.items(): |
| 143 |
if not counts: |
| 144 |
continue |
| 145 |
if sum(counts.values()) < 20 and file_sloc.get(path, 0) < 100 and file_funcs.get(path, 0) < 5: |
| 146 |
continue |
| 147 |
top = counts.most_common(5) |
| 148 |
total = sum(counts.values()) |
| 149 |
dominant = top[0][1] / total if total else 0 |
| 150 |
if len(top) >= 5 and dominant < 0.35: |
| 151 |
mixed.append((path, dominant, top)) |
| 152 |
|
| 153 |
print("mixed_concept_files:") |
| 154 |
if mixed: |
| 155 |
for path, dominant, top in sorted(mixed, key=lambda item: (item[1], item[0]))[:20]: |
| 156 |
rendered = ", ".join(f"{term}:{count}" for term, count in top) |
| 157 |
print(f"- {path}: dominant_share={dominant:.2f} terms={rendered}") |
| 158 |
else: |
| 159 |
print("- none") |
| 160 |
PY |