mulle-roam-code 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mulle_roam_code-0.0.1.dist-info/METADATA +1590 -0
- mulle_roam_code-0.0.1.dist-info/RECORD +257 -0
- mulle_roam_code-0.0.1.dist-info/WHEEL +5 -0
- mulle_roam_code-0.0.1.dist-info/entry_points.txt +3 -0
- mulle_roam_code-0.0.1.dist-info/licenses/LICENSE +21 -0
- mulle_roam_code-0.0.1.dist-info/top_level.txt +1 -0
- roam/__init__.py +8 -0
- roam/__main__.py +5 -0
- roam/analysis/__init__.py +1 -0
- roam/analysis/effects.py +639 -0
- roam/analysis/taint.py +689 -0
- roam/api.py +238 -0
- roam/bridges/__init__.py +3 -0
- roam/bridges/base.py +49 -0
- roam/bridges/bridge_config.py +218 -0
- roam/bridges/bridge_protobuf.py +352 -0
- roam/bridges/bridge_rest_api.py +242 -0
- roam/bridges/bridge_salesforce.py +198 -0
- roam/bridges/bridge_template.py +247 -0
- roam/bridges/registry.py +51 -0
- roam/catalog/__init__.py +0 -0
- roam/catalog/detectors.py +2242 -0
- roam/catalog/fixes.py +86 -0
- roam/catalog/smells.py +599 -0
- roam/catalog/tasks.py +1260 -0
- roam/cli.py +548 -0
- roam/commands/__init__.py +0 -0
- roam/commands/changed_files.py +139 -0
- roam/commands/cmd_adversarial.py +699 -0
- roam/commands/cmd_affected.py +432 -0
- roam/commands/cmd_affected_tests.py +375 -0
- roam/commands/cmd_agent_context.py +168 -0
- roam/commands/cmd_agent_export.py +890 -0
- roam/commands/cmd_agent_plan.py +315 -0
- roam/commands/cmd_ai_ratio.py +765 -0
- roam/commands/cmd_ai_readiness.py +775 -0
- roam/commands/cmd_alerts.py +409 -0
- roam/commands/cmd_annotate.py +210 -0
- roam/commands/cmd_api_changes.py +796 -0
- roam/commands/cmd_api_drift.py +781 -0
- roam/commands/cmd_attest.py +913 -0
- roam/commands/cmd_auth_gaps.py +799 -0
- roam/commands/cmd_bisect.py +216 -0
- roam/commands/cmd_breaking.py +464 -0
- roam/commands/cmd_budget.py +413 -0
- roam/commands/cmd_bus_factor.py +403 -0
- roam/commands/cmd_capsule.py +275 -0
- roam/commands/cmd_check_rules.py +574 -0
- roam/commands/cmd_clean.py +147 -0
- roam/commands/cmd_closure.py +294 -0
- roam/commands/cmd_clusters.py +457 -0
- roam/commands/cmd_codeowners.py +600 -0
- roam/commands/cmd_complexity.py +364 -0
- roam/commands/cmd_config.py +310 -0
- roam/commands/cmd_context.py +1151 -0
- roam/commands/cmd_conventions.py +697 -0
- roam/commands/cmd_coupling.py +462 -0
- roam/commands/cmd_coverage_gaps.py +638 -0
- roam/commands/cmd_cut.py +235 -0
- roam/commands/cmd_dark_matter.py +127 -0
- roam/commands/cmd_dashboard.py +365 -0
- roam/commands/cmd_dead.py +1585 -0
- roam/commands/cmd_debt.py +731 -0
- roam/commands/cmd_deps.py +121 -0
- roam/commands/cmd_describe.py +959 -0
- roam/commands/cmd_dev_profile.py +580 -0
- roam/commands/cmd_diagnose.py +377 -0
- roam/commands/cmd_diff.py +633 -0
- roam/commands/cmd_digest.py +214 -0
- roam/commands/cmd_doc_staleness.py +334 -0
- roam/commands/cmd_docs_coverage.py +193 -0
- roam/commands/cmd_doctor.py +319 -0
- roam/commands/cmd_drift.py +377 -0
- roam/commands/cmd_duplicates.py +559 -0
- roam/commands/cmd_effects.py +362 -0
- roam/commands/cmd_endpoints.py +768 -0
- roam/commands/cmd_entry_points.py +358 -0
- roam/commands/cmd_fan.py +311 -0
- roam/commands/cmd_file.py +334 -0
- roam/commands/cmd_fingerprint.py +172 -0
- roam/commands/cmd_fitness.py +723 -0
- roam/commands/cmd_fn_coupling.py +295 -0
- roam/commands/cmd_forecast.py +366 -0
- roam/commands/cmd_grep.py +282 -0
- roam/commands/cmd_guard.py +434 -0
- roam/commands/cmd_health.py +922 -0
- roam/commands/cmd_hooks.py +458 -0
- roam/commands/cmd_hotspots.py +606 -0
- roam/commands/cmd_impact.py +228 -0
- roam/commands/cmd_index.py +102 -0
- roam/commands/cmd_ingest_trace.py +143 -0
- roam/commands/cmd_init.py +166 -0
- roam/commands/cmd_intent.py +459 -0
- roam/commands/cmd_invariants.py +301 -0
- roam/commands/cmd_layers.py +387 -0
- roam/commands/cmd_map.py +275 -0
- roam/commands/cmd_math.py +213 -0
- roam/commands/cmd_mcp_setup.py +135 -0
- roam/commands/cmd_metrics.py +663 -0
- roam/commands/cmd_migration_safety.py +712 -0
- roam/commands/cmd_minimap.py +519 -0
- roam/commands/cmd_missing_index.py +885 -0
- roam/commands/cmd_module.py +204 -0
- roam/commands/cmd_mutate.py +400 -0
- roam/commands/cmd_n1.py +967 -0
- roam/commands/cmd_onboard.py +740 -0
- roam/commands/cmd_orchestrate.py +180 -0
- roam/commands/cmd_orphan_routes.py +754 -0
- roam/commands/cmd_over_fetch.py +633 -0
- roam/commands/cmd_owner.py +299 -0
- roam/commands/cmd_partition.py +714 -0
- roam/commands/cmd_path_coverage.py +564 -0
- roam/commands/cmd_patterns.py +689 -0
- roam/commands/cmd_plan.py +616 -0
- roam/commands/cmd_plan_refactor.py +432 -0
- roam/commands/cmd_pr_diff.py +292 -0
- roam/commands/cmd_pr_risk.py +677 -0
- roam/commands/cmd_preflight.py +706 -0
- roam/commands/cmd_relate.py +405 -0
- roam/commands/cmd_report.py +304 -0
- roam/commands/cmd_reset.py +117 -0
- roam/commands/cmd_risk.py +533 -0
- roam/commands/cmd_rules.py +319 -0
- roam/commands/cmd_safe_delete.py +224 -0
- roam/commands/cmd_safe_zones.py +295 -0
- roam/commands/cmd_schema.py +166 -0
- roam/commands/cmd_search.py +295 -0
- roam/commands/cmd_search_semantic.py +101 -0
- roam/commands/cmd_secrets.py +636 -0
- roam/commands/cmd_semantic_diff.py +538 -0
- roam/commands/cmd_simulate.py +296 -0
- roam/commands/cmd_simulate_departure.py +632 -0
- roam/commands/cmd_sketch.py +160 -0
- roam/commands/cmd_smells.py +179 -0
- roam/commands/cmd_snapshot.py +50 -0
- roam/commands/cmd_spectral.py +183 -0
- roam/commands/cmd_split.py +432 -0
- roam/commands/cmd_suggest_refactoring.py +486 -0
- roam/commands/cmd_suggest_reviewers.py +491 -0
- roam/commands/cmd_supply_chain.py +831 -0
- roam/commands/cmd_symbol.py +135 -0
- roam/commands/cmd_syntax_check.py +301 -0
- roam/commands/cmd_test_gaps.py +574 -0
- roam/commands/cmd_testmap.py +308 -0
- roam/commands/cmd_tour.py +377 -0
- roam/commands/cmd_trace.py +304 -0
- roam/commands/cmd_trend.py +461 -0
- roam/commands/cmd_trends.py +929 -0
- roam/commands/cmd_understand.py +763 -0
- roam/commands/cmd_uses.py +170 -0
- roam/commands/cmd_verify.py +812 -0
- roam/commands/cmd_verify_imports.py +435 -0
- roam/commands/cmd_vibe_check.py +932 -0
- roam/commands/cmd_visualize.py +361 -0
- roam/commands/cmd_vuln_map.py +139 -0
- roam/commands/cmd_vuln_reach.py +289 -0
- roam/commands/cmd_vulns.py +414 -0
- roam/commands/cmd_watch.py +759 -0
- roam/commands/cmd_weather.py +118 -0
- roam/commands/cmd_why.py +325 -0
- roam/commands/cmd_ws.py +755 -0
- roam/commands/cmd_xlang.py +170 -0
- roam/commands/context_helpers.py +1137 -0
- roam/commands/gate_presets.py +222 -0
- roam/commands/graph_helpers.py +93 -0
- roam/commands/metrics_history.py +298 -0
- roam/commands/next_steps.py +107 -0
- roam/commands/resolve.py +302 -0
- roam/competitor_site_data.py +2229 -0
- roam/coverage_reports.py +462 -0
- roam/db/__init__.py +0 -0
- roam/db/connection.py +359 -0
- roam/db/queries.py +151 -0
- roam/db/schema.py +335 -0
- roam/exit_codes.py +100 -0
- roam/graph/__init__.py +43 -0
- roam/graph/anomaly.py +493 -0
- roam/graph/builder.py +61 -0
- roam/graph/clusters.py +286 -0
- roam/graph/cycles.py +210 -0
- roam/graph/dark_matter.py +207 -0
- roam/graph/diff.py +467 -0
- roam/graph/fingerprint.py +297 -0
- roam/graph/layers.py +134 -0
- roam/graph/pagerank.py +171 -0
- roam/graph/partition.py +437 -0
- roam/graph/pathfinding.py +119 -0
- roam/graph/propagation.py +208 -0
- roam/graph/simulate.py +286 -0
- roam/graph/spectral.py +260 -0
- roam/index/__init__.py +5 -0
- roam/index/complexity.py +1108 -0
- roam/index/discovery.py +338 -0
- roam/index/file_roles.py +586 -0
- roam/index/git_stats.py +577 -0
- roam/index/incremental.py +75 -0
- roam/index/indexer.py +1191 -0
- roam/index/parser.py +461 -0
- roam/index/relations.py +537 -0
- roam/index/symbols.py +71 -0
- roam/index/test_conventions.py +508 -0
- roam/languages/__init__.py +21 -0
- roam/languages/apex_lang.py +241 -0
- roam/languages/aura_lang.py +228 -0
- roam/languages/base.py +111 -0
- roam/languages/c_lang.py +458 -0
- roam/languages/csharp_lang.py +1215 -0
- roam/languages/foxpro_lang.py +1554 -0
- roam/languages/generic_lang.py +702 -0
- roam/languages/go_lang.py +392 -0
- roam/languages/hcl_lang.py +398 -0
- roam/languages/java_lang.py +418 -0
- roam/languages/javascript_lang.py +799 -0
- roam/languages/kotlin_lang.py +147 -0
- roam/languages/objc_lang.py +401 -0
- roam/languages/php_lang.py +696 -0
- roam/languages/python_lang.py +850 -0
- roam/languages/registry.py +283 -0
- roam/languages/ruby_lang.py +429 -0
- roam/languages/rust_lang.py +545 -0
- roam/languages/sfxml_lang.py +276 -0
- roam/languages/swift_lang.py +173 -0
- roam/languages/typescript_lang.py +323 -0
- roam/languages/visualforce_lang.py +151 -0
- roam/languages/yaml_lang.py +566 -0
- roam/mcp_server.py +4794 -0
- roam/output/__init__.py +0 -0
- roam/output/formatter.py +557 -0
- roam/output/mermaid.py +62 -0
- roam/output/sarif.py +897 -0
- roam/output/schema_registry.py +57 -0
- roam/plugins.py +208 -0
- roam/refactor/__init__.py +0 -0
- roam/refactor/codegen.py +106 -0
- roam/refactor/transforms.py +711 -0
- roam/rules/__init__.py +0 -0
- roam/rules/ast_match.py +238 -0
- roam/rules/builtin.py +617 -0
- roam/rules/dataflow.py +515 -0
- roam/rules/engine.py +943 -0
- roam/runtime/__init__.py +3 -0
- roam/runtime/hotspots.py +138 -0
- roam/runtime/trace_ingest.py +535 -0
- roam/search/__init__.py +1 -0
- roam/search/framework_packs.py +673 -0
- roam/search/index_embeddings.py +732 -0
- roam/search/onnx_embeddings.py +233 -0
- roam/search/tfidf.py +355 -0
- roam/security/__init__.py +1 -0
- roam/security/vuln_reach.py +246 -0
- roam/security/vuln_store.py +334 -0
- roam/surface_counts.py +158 -0
- roam/workspace/__init__.py +3 -0
- roam/workspace/aggregator.py +426 -0
- roam/workspace/api_scanner.py +538 -0
- roam/workspace/config.py +95 -0
- roam/workspace/db.py +136 -0
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
"""Unified per-file and per-symbol metrics command.
|
|
2
|
+
|
|
3
|
+
Consolidates complexity, fan-in/fan-out, PageRank, churn, test coverage,
|
|
4
|
+
layer depth, dead-code risk, LOC, and co-change data into a single
|
|
5
|
+
structured output. All data is read from the existing SQLite index.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import sqlite3
|
|
11
|
+
|
|
12
|
+
import click
|
|
13
|
+
|
|
14
|
+
from roam.commands.resolve import ensure_index, find_symbol
|
|
15
|
+
from roam.db.connection import batched_in, open_db
|
|
16
|
+
from roam.output.formatter import (
|
|
17
|
+
abbrev_kind,
|
|
18
|
+
format_table,
|
|
19
|
+
json_envelope,
|
|
20
|
+
loc,
|
|
21
|
+
to_json,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
# ---------------------------------------------------------------------------
|
|
25
|
+
# Health scoring
|
|
26
|
+
# ---------------------------------------------------------------------------
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _health_label(metrics: dict) -> str:
|
|
30
|
+
"""Derive a health label from collected metrics.
|
|
31
|
+
|
|
32
|
+
Heuristic:
|
|
33
|
+
- poor: high complexity, high dead-code risk, or very high churn
|
|
34
|
+
- fair: moderate issues
|
|
35
|
+
- good: everything within norms
|
|
36
|
+
"""
|
|
37
|
+
score = 0
|
|
38
|
+
cc = metrics.get("complexity", 0) or 0
|
|
39
|
+
fan_out = metrics.get("fan_out", 0) or 0
|
|
40
|
+
fan_in = metrics.get("fan_in", 0) or 0
|
|
41
|
+
churn = metrics.get("churn", 0) or 0
|
|
42
|
+
dead_code_risk = metrics.get("dead_code_risk", False)
|
|
43
|
+
|
|
44
|
+
if cc > 25:
|
|
45
|
+
score += 2
|
|
46
|
+
elif cc > 15:
|
|
47
|
+
score += 1
|
|
48
|
+
|
|
49
|
+
if fan_out > 15:
|
|
50
|
+
score += 2
|
|
51
|
+
elif fan_out > 10:
|
|
52
|
+
score += 1
|
|
53
|
+
|
|
54
|
+
if churn > 50:
|
|
55
|
+
score += 1
|
|
56
|
+
|
|
57
|
+
if dead_code_risk:
|
|
58
|
+
score += 1
|
|
59
|
+
|
|
60
|
+
if score >= 3:
|
|
61
|
+
return "poor"
|
|
62
|
+
if score >= 1:
|
|
63
|
+
return "fair"
|
|
64
|
+
return "good"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# ---------------------------------------------------------------------------
|
|
68
|
+
# Metric collection — symbol level
|
|
69
|
+
# ---------------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def collect_symbol_metrics(
|
|
73
|
+
conn: sqlite3.Connection,
|
|
74
|
+
symbol_id: int,
|
|
75
|
+
*,
|
|
76
|
+
include_comprehension: bool = True,
|
|
77
|
+
) -> dict:
|
|
78
|
+
"""Gather all available metrics for a single symbol.
|
|
79
|
+
|
|
80
|
+
Returns a flat dict with keys: complexity, fan_in, fan_out, pagerank,
|
|
81
|
+
betweenness, churn, commits, test_files, layer_depth, dead_code_risk,
|
|
82
|
+
loc, co_change_count.
|
|
83
|
+
"""
|
|
84
|
+
result: dict = {
|
|
85
|
+
"complexity": 0,
|
|
86
|
+
"fan_in": 0,
|
|
87
|
+
"fan_out": 0,
|
|
88
|
+
"pagerank": 0.0,
|
|
89
|
+
"betweenness": 0.0,
|
|
90
|
+
"closeness": 0.0,
|
|
91
|
+
"eigenvector": 0.0,
|
|
92
|
+
"clustering_coefficient": 0.0,
|
|
93
|
+
"debt_score": 0.0,
|
|
94
|
+
"churn": 0,
|
|
95
|
+
"commits": 0,
|
|
96
|
+
"test_files": 0,
|
|
97
|
+
"layer_depth": None,
|
|
98
|
+
"dead_code_risk": False,
|
|
99
|
+
"loc": 0,
|
|
100
|
+
"co_change_count": 0,
|
|
101
|
+
"information_scatter": 0,
|
|
102
|
+
"working_set_size": 0,
|
|
103
|
+
"comprehension_difficulty": 0.0,
|
|
104
|
+
"coverage_pct": None,
|
|
105
|
+
"covered_lines": 0,
|
|
106
|
+
"coverable_lines": 0,
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
# -- symbol_metrics (cognitive complexity, line_count) --
|
|
110
|
+
try:
|
|
111
|
+
sm = conn.execute(
|
|
112
|
+
"SELECT cognitive_complexity, line_count FROM symbol_metrics WHERE symbol_id = ?",
|
|
113
|
+
(symbol_id,),
|
|
114
|
+
).fetchone()
|
|
115
|
+
if sm:
|
|
116
|
+
result["complexity"] = sm["cognitive_complexity"] or 0
|
|
117
|
+
result["loc"] = sm["line_count"] or 0
|
|
118
|
+
except Exception:
|
|
119
|
+
pass
|
|
120
|
+
# Optional imported coverage columns (safe on older DB schemas)
|
|
121
|
+
try:
|
|
122
|
+
cov = conn.execute(
|
|
123
|
+
"SELECT coverage_pct, covered_lines, coverable_lines FROM symbol_metrics WHERE symbol_id = ?",
|
|
124
|
+
(symbol_id,),
|
|
125
|
+
).fetchone()
|
|
126
|
+
if cov:
|
|
127
|
+
result["coverage_pct"] = cov["coverage_pct"]
|
|
128
|
+
result["covered_lines"] = cov["covered_lines"] or 0
|
|
129
|
+
result["coverable_lines"] = cov["coverable_lines"] or 0
|
|
130
|
+
except Exception:
|
|
131
|
+
pass
|
|
132
|
+
|
|
133
|
+
# -- graph_metrics (pagerank, in_degree, out_degree, betweenness) --
|
|
134
|
+
try:
|
|
135
|
+
gm = conn.execute(
|
|
136
|
+
"SELECT pagerank, in_degree, out_degree, betweenness FROM graph_metrics WHERE symbol_id = ?",
|
|
137
|
+
(symbol_id,),
|
|
138
|
+
).fetchone()
|
|
139
|
+
if gm:
|
|
140
|
+
result["pagerank"] = gm["pagerank"] or 0.0
|
|
141
|
+
result["fan_in"] = gm["in_degree"] or 0
|
|
142
|
+
result["fan_out"] = gm["out_degree"] or 0
|
|
143
|
+
result["betweenness"] = gm["betweenness"] or 0.0
|
|
144
|
+
except Exception:
|
|
145
|
+
pass
|
|
146
|
+
# Optional SNA v2 columns (safe on older DB schemas)
|
|
147
|
+
try:
|
|
148
|
+
extra = conn.execute(
|
|
149
|
+
"SELECT closeness, eigenvector, clustering_coefficient, debt_score FROM graph_metrics WHERE symbol_id = ?",
|
|
150
|
+
(symbol_id,),
|
|
151
|
+
).fetchone()
|
|
152
|
+
if extra:
|
|
153
|
+
result["closeness"] = extra["closeness"] or 0.0
|
|
154
|
+
result["eigenvector"] = extra["eigenvector"] or 0.0
|
|
155
|
+
result["clustering_coefficient"] = extra["clustering_coefficient"] or 0.0
|
|
156
|
+
result["debt_score"] = extra["debt_score"] or 0.0
|
|
157
|
+
except Exception:
|
|
158
|
+
pass
|
|
159
|
+
|
|
160
|
+
# -- edges (fallback fan-in / fan-out from raw edges) --
|
|
161
|
+
if result["fan_in"] == 0 and result["fan_out"] == 0:
|
|
162
|
+
try:
|
|
163
|
+
fi = conn.execute(
|
|
164
|
+
"SELECT COUNT(*) FROM edges WHERE target_id = ?",
|
|
165
|
+
(symbol_id,),
|
|
166
|
+
).fetchone()
|
|
167
|
+
fo = conn.execute(
|
|
168
|
+
"SELECT COUNT(*) FROM edges WHERE source_id = ?",
|
|
169
|
+
(symbol_id,),
|
|
170
|
+
).fetchone()
|
|
171
|
+
result["fan_in"] = fi[0] if fi else 0
|
|
172
|
+
result["fan_out"] = fo[0] if fo else 0
|
|
173
|
+
except Exception:
|
|
174
|
+
pass
|
|
175
|
+
|
|
176
|
+
# -- dead_code_risk: fan_in == 0 for non-entry-point symbols --
|
|
177
|
+
sym_row = conn.execute(
|
|
178
|
+
"SELECT kind, is_exported, file_id FROM symbols WHERE id = ?",
|
|
179
|
+
(symbol_id,),
|
|
180
|
+
).fetchone()
|
|
181
|
+
if sym_row:
|
|
182
|
+
kind = sym_row["kind"] or ""
|
|
183
|
+
is_exported = sym_row["is_exported"]
|
|
184
|
+
if result["fan_in"] == 0 and kind in ("function", "method", "class"):
|
|
185
|
+
# Entry points (exported, main, __init__) are not dead code
|
|
186
|
+
if not is_exported:
|
|
187
|
+
result["dead_code_risk"] = True
|
|
188
|
+
|
|
189
|
+
# -- churn / commits from git_file_stats via file_id --
|
|
190
|
+
file_id = sym_row["file_id"]
|
|
191
|
+
try:
|
|
192
|
+
fs = conn.execute(
|
|
193
|
+
"SELECT commit_count, total_churn FROM file_stats WHERE file_id = ?",
|
|
194
|
+
(file_id,),
|
|
195
|
+
).fetchone()
|
|
196
|
+
if fs:
|
|
197
|
+
result["commits"] = fs["commit_count"] or 0
|
|
198
|
+
result["churn"] = fs["total_churn"] or 0
|
|
199
|
+
except Exception:
|
|
200
|
+
pass
|
|
201
|
+
|
|
202
|
+
# -- test files: count files with file_role='test' that reference
|
|
203
|
+
# the same file via file_edges --
|
|
204
|
+
try:
|
|
205
|
+
tf = conn.execute(
|
|
206
|
+
"SELECT COUNT(DISTINCT fe.source_file_id) "
|
|
207
|
+
"FROM file_edges fe "
|
|
208
|
+
"JOIN files f ON fe.source_file_id = f.id "
|
|
209
|
+
"WHERE fe.target_file_id = ? AND f.file_role = 'test'",
|
|
210
|
+
(file_id,),
|
|
211
|
+
).fetchone()
|
|
212
|
+
result["test_files"] = tf[0] if tf else 0
|
|
213
|
+
except Exception:
|
|
214
|
+
pass
|
|
215
|
+
|
|
216
|
+
# -- co_change_count --
|
|
217
|
+
try:
|
|
218
|
+
cc_row = conn.execute(
|
|
219
|
+
"SELECT SUM(cochange_count) AS total FROM git_cochange WHERE file_id_a = ? OR file_id_b = ?",
|
|
220
|
+
(file_id, file_id),
|
|
221
|
+
).fetchone()
|
|
222
|
+
result["co_change_count"] = cc_row["total"] or 0 if cc_row else 0
|
|
223
|
+
except Exception:
|
|
224
|
+
pass
|
|
225
|
+
|
|
226
|
+
# Comprehension difficulty metrics (#71):
|
|
227
|
+
# - information scatter: distinct files in 2-hop closure
|
|
228
|
+
# - working set size: symbols in 2-hop closure
|
|
229
|
+
# - composite score from fan-out, scatter, working set, complexity
|
|
230
|
+
if include_comprehension:
|
|
231
|
+
scatter, working_set = _comprehension_neighborhood(conn, symbol_id)
|
|
232
|
+
result["information_scatter"] = scatter
|
|
233
|
+
result["working_set_size"] = working_set
|
|
234
|
+
result["comprehension_difficulty"] = _comprehension_score(
|
|
235
|
+
fan_out=result["fan_out"],
|
|
236
|
+
information_scatter=scatter,
|
|
237
|
+
working_set_size=working_set,
|
|
238
|
+
complexity=result["complexity"],
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
return result
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _comprehension_neighborhood(conn: sqlite3.Connection, symbol_id: int, depth: int = 2) -> tuple[int, int]:
|
|
245
|
+
"""Compute (information_scatter, working_set_size) in N-hop call neighborhood."""
|
|
246
|
+
visited: set[int] = {symbol_id}
|
|
247
|
+
frontier: set[int] = {symbol_id}
|
|
248
|
+
|
|
249
|
+
for _ in range(max(1, depth)):
|
|
250
|
+
if not frontier:
|
|
251
|
+
break
|
|
252
|
+
ids = sorted(frontier)
|
|
253
|
+
# Explore both callers and callees so context reflects read/write surface.
|
|
254
|
+
out_rows = batched_in(
|
|
255
|
+
conn,
|
|
256
|
+
"SELECT target_id FROM edges WHERE source_id IN ({ph})",
|
|
257
|
+
ids,
|
|
258
|
+
)
|
|
259
|
+
in_rows = batched_in(
|
|
260
|
+
conn,
|
|
261
|
+
"SELECT source_id FROM edges WHERE target_id IN ({ph})",
|
|
262
|
+
ids,
|
|
263
|
+
)
|
|
264
|
+
neighbors = {int(r[0]) for r in out_rows if r[0] is not None}
|
|
265
|
+
neighbors.update(int(r[0]) for r in in_rows if r[0] is not None)
|
|
266
|
+
neighbors -= visited
|
|
267
|
+
if not neighbors:
|
|
268
|
+
break
|
|
269
|
+
visited.update(neighbors)
|
|
270
|
+
frontier = neighbors
|
|
271
|
+
|
|
272
|
+
if len(visited) <= 1:
|
|
273
|
+
return (0, 0)
|
|
274
|
+
|
|
275
|
+
others = sorted(v for v in visited if v != symbol_id)
|
|
276
|
+
file_rows = batched_in(
|
|
277
|
+
conn,
|
|
278
|
+
"SELECT DISTINCT file_id FROM symbols WHERE id IN ({ph})",
|
|
279
|
+
others,
|
|
280
|
+
)
|
|
281
|
+
scatter = len([r for r in file_rows if r[0] is not None])
|
|
282
|
+
working_set = len(others)
|
|
283
|
+
return (scatter, working_set)
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def _comprehension_score(*, fan_out: int, information_scatter: int, working_set_size: int, complexity: float) -> float:
|
|
287
|
+
"""Composite comprehension difficulty score (0-100)."""
|
|
288
|
+
fan_out_n = min(1.0, max(0.0, float(fan_out) / 12.0))
|
|
289
|
+
scatter_n = min(1.0, max(0.0, float(max(information_scatter - 1, 0)) / 8.0))
|
|
290
|
+
working_n = min(1.0, max(0.0, float(working_set_size) / 30.0))
|
|
291
|
+
complexity_n = min(1.0, max(0.0, float(complexity) / 30.0))
|
|
292
|
+
score = 100.0 * (0.35 * fan_out_n + 0.30 * scatter_n + 0.20 * working_n + 0.15 * complexity_n)
|
|
293
|
+
return round(score, 3)
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
# ---------------------------------------------------------------------------
|
|
297
|
+
# Metric collection — file level
|
|
298
|
+
# ---------------------------------------------------------------------------
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def collect_file_metrics(conn: sqlite3.Connection, file_id: int) -> dict:
|
|
302
|
+
"""Gather aggregate metrics for all symbols in a file.
|
|
303
|
+
|
|
304
|
+
Returns a dict with file-level aggregates plus a ``symbols`` list
|
|
305
|
+
with per-symbol breakdown.
|
|
306
|
+
"""
|
|
307
|
+
file_row = conn.execute(
|
|
308
|
+
"SELECT id, path, language, line_count, file_role FROM files WHERE id = ?",
|
|
309
|
+
(file_id,),
|
|
310
|
+
).fetchone()
|
|
311
|
+
if not file_row:
|
|
312
|
+
return {}
|
|
313
|
+
|
|
314
|
+
# Gather symbols in this file
|
|
315
|
+
sym_rows = conn.execute(
|
|
316
|
+
"SELECT s.id, s.name, s.kind, s.qualified_name, s.line_start, s.line_end, "
|
|
317
|
+
"COALESCE(sm.cognitive_complexity, 0) AS cognitive_complexity "
|
|
318
|
+
"FROM symbols s "
|
|
319
|
+
"LEFT JOIN symbol_metrics sm ON s.id = sm.symbol_id "
|
|
320
|
+
"WHERE s.file_id = ? ORDER BY s.line_start",
|
|
321
|
+
(file_id,),
|
|
322
|
+
).fetchall()
|
|
323
|
+
|
|
324
|
+
# Per-symbol metrics
|
|
325
|
+
symbol_metrics_list = []
|
|
326
|
+
total_complexity = 0.0
|
|
327
|
+
total_fan_in = 0
|
|
328
|
+
total_fan_out = 0
|
|
329
|
+
max_pagerank = 0.0
|
|
330
|
+
dead_count = 0
|
|
331
|
+
|
|
332
|
+
for sr in sym_rows:
|
|
333
|
+
sm = collect_symbol_metrics(conn, sr["id"], include_comprehension=False)
|
|
334
|
+
total_complexity += sm["complexity"]
|
|
335
|
+
total_fan_in += sm["fan_in"]
|
|
336
|
+
total_fan_out += sm["fan_out"]
|
|
337
|
+
max_pagerank = max(max_pagerank, sm["pagerank"])
|
|
338
|
+
if sm["dead_code_risk"]:
|
|
339
|
+
dead_count += 1
|
|
340
|
+
symbol_metrics_list.append(
|
|
341
|
+
{
|
|
342
|
+
"name": sr["name"],
|
|
343
|
+
"kind": sr["kind"],
|
|
344
|
+
"qualified_name": sr["qualified_name"],
|
|
345
|
+
"line_start": sr["line_start"],
|
|
346
|
+
"line_end": sr["line_end"],
|
|
347
|
+
**sm,
|
|
348
|
+
}
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
# File-level churn / commits
|
|
352
|
+
churn = 0
|
|
353
|
+
commits = 0
|
|
354
|
+
coverage_pct = None
|
|
355
|
+
covered_lines = 0
|
|
356
|
+
coverable_lines = 0
|
|
357
|
+
try:
|
|
358
|
+
fs = conn.execute(
|
|
359
|
+
"SELECT commit_count, total_churn FROM file_stats WHERE file_id = ?",
|
|
360
|
+
(file_id,),
|
|
361
|
+
).fetchone()
|
|
362
|
+
if fs:
|
|
363
|
+
commits = fs["commit_count"] or 0
|
|
364
|
+
churn = fs["total_churn"] or 0
|
|
365
|
+
except Exception:
|
|
366
|
+
pass
|
|
367
|
+
try:
|
|
368
|
+
cov = conn.execute(
|
|
369
|
+
"SELECT coverage_pct, covered_lines, coverable_lines FROM file_stats WHERE file_id = ?",
|
|
370
|
+
(file_id,),
|
|
371
|
+
).fetchone()
|
|
372
|
+
if cov:
|
|
373
|
+
coverage_pct = cov["coverage_pct"]
|
|
374
|
+
covered_lines = cov["covered_lines"] or 0
|
|
375
|
+
coverable_lines = cov["coverable_lines"] or 0
|
|
376
|
+
except Exception:
|
|
377
|
+
pass
|
|
378
|
+
|
|
379
|
+
# Test files referencing this file
|
|
380
|
+
test_files = 0
|
|
381
|
+
try:
|
|
382
|
+
tf = conn.execute(
|
|
383
|
+
"SELECT COUNT(DISTINCT fe.source_file_id) "
|
|
384
|
+
"FROM file_edges fe "
|
|
385
|
+
"JOIN files f ON fe.source_file_id = f.id "
|
|
386
|
+
"WHERE fe.target_file_id = ? AND f.file_role = 'test'",
|
|
387
|
+
(file_id,),
|
|
388
|
+
).fetchone()
|
|
389
|
+
test_files = tf[0] if tf else 0
|
|
390
|
+
except Exception:
|
|
391
|
+
pass
|
|
392
|
+
|
|
393
|
+
# Co-change count
|
|
394
|
+
co_change = 0
|
|
395
|
+
try:
|
|
396
|
+
cc_row = conn.execute(
|
|
397
|
+
"SELECT SUM(cochange_count) AS total FROM git_cochange WHERE file_id_a = ? OR file_id_b = ?",
|
|
398
|
+
(file_id, file_id),
|
|
399
|
+
).fetchone()
|
|
400
|
+
co_change = cc_row["total"] or 0 if cc_row else 0
|
|
401
|
+
except Exception:
|
|
402
|
+
pass
|
|
403
|
+
|
|
404
|
+
file_metrics = {
|
|
405
|
+
"complexity": round(total_complexity, 1),
|
|
406
|
+
"fan_in": total_fan_in,
|
|
407
|
+
"fan_out": total_fan_out,
|
|
408
|
+
"max_pagerank": round(max_pagerank, 6),
|
|
409
|
+
"churn": churn,
|
|
410
|
+
"commits": commits,
|
|
411
|
+
"test_files": test_files,
|
|
412
|
+
"dead_symbols": dead_count,
|
|
413
|
+
"loc": file_row["line_count"] or 0,
|
|
414
|
+
"symbol_count": len(sym_rows),
|
|
415
|
+
"co_change_count": co_change,
|
|
416
|
+
"coverage_pct": coverage_pct,
|
|
417
|
+
"covered_lines": covered_lines,
|
|
418
|
+
"coverable_lines": coverable_lines,
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return {
|
|
422
|
+
"file": file_row["path"],
|
|
423
|
+
"language": file_row["language"],
|
|
424
|
+
"file_role": file_row["file_role"],
|
|
425
|
+
"metrics": file_metrics,
|
|
426
|
+
"symbols": symbol_metrics_list,
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
# ---------------------------------------------------------------------------
|
|
431
|
+
# Target resolution
|
|
432
|
+
# ---------------------------------------------------------------------------
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def _resolve_target(conn: sqlite3.Connection, target: str) -> tuple[str, int | None, dict | None]:
|
|
436
|
+
"""Determine if target is a file or symbol and return (type, id, row).
|
|
437
|
+
|
|
438
|
+
Returns:
|
|
439
|
+
("file", file_id, file_row) or ("symbol", symbol_id, symbol_row)
|
|
440
|
+
or ("unknown", None, None)
|
|
441
|
+
"""
|
|
442
|
+
# Try file path first (exact match)
|
|
443
|
+
norm = target.replace("\\", "/")
|
|
444
|
+
row = conn.execute(
|
|
445
|
+
"SELECT id, path FROM files WHERE path = ?",
|
|
446
|
+
(norm,),
|
|
447
|
+
).fetchone()
|
|
448
|
+
if row:
|
|
449
|
+
return ("file", row["id"], row)
|
|
450
|
+
|
|
451
|
+
# Try partial file path match
|
|
452
|
+
row = conn.execute(
|
|
453
|
+
"SELECT id, path FROM files WHERE path LIKE ? ORDER BY path LIMIT 1",
|
|
454
|
+
(f"%{norm}%",),
|
|
455
|
+
).fetchone()
|
|
456
|
+
if row:
|
|
457
|
+
return ("file", row["id"], row)
|
|
458
|
+
|
|
459
|
+
# Try symbol lookup
|
|
460
|
+
sym = find_symbol(conn, target)
|
|
461
|
+
if sym:
|
|
462
|
+
return ("symbol", sym["id"], sym)
|
|
463
|
+
|
|
464
|
+
return ("unknown", None, None)
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
# ---------------------------------------------------------------------------
|
|
468
|
+
# CLI command
|
|
469
|
+
# ---------------------------------------------------------------------------
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
@click.command("metrics")
|
|
473
|
+
@click.argument("target")
|
|
474
|
+
@click.pass_context
|
|
475
|
+
def metrics(ctx, target):
|
|
476
|
+
"""Show unified metrics for a file or symbol.
|
|
477
|
+
|
|
478
|
+
TARGET can be a file path (e.g. src/app.py) or a symbol name
|
|
479
|
+
(e.g. create_user). Consolidates complexity, fan-in/fan-out,
|
|
480
|
+
SNA centrality vector (PageRank/betweenness/closeness/eigenvector/
|
|
481
|
+
clustering coefficient), composite debt score, churn, test coverage,
|
|
482
|
+
and comprehension difficulty into one view.
|
|
483
|
+
"""
|
|
484
|
+
json_mode = ctx.obj.get("json") if ctx.obj else False
|
|
485
|
+
token_budget = ctx.obj.get("budget", 0) if ctx.obj else 0
|
|
486
|
+
ensure_index()
|
|
487
|
+
|
|
488
|
+
with open_db(readonly=True) as conn:
|
|
489
|
+
target_type, target_id, target_row = _resolve_target(conn, target)
|
|
490
|
+
|
|
491
|
+
if target_type == "unknown":
|
|
492
|
+
msg = f'Target not found: "{target}"'
|
|
493
|
+
if json_mode:
|
|
494
|
+
click.echo(
|
|
495
|
+
to_json(
|
|
496
|
+
json_envelope(
|
|
497
|
+
"metrics",
|
|
498
|
+
summary={"verdict": "not found", "target": target},
|
|
499
|
+
error=msg,
|
|
500
|
+
)
|
|
501
|
+
)
|
|
502
|
+
)
|
|
503
|
+
else:
|
|
504
|
+
click.echo(f"VERDICT: not found -- {msg}")
|
|
505
|
+
click.echo(
|
|
506
|
+
" Tip: Use a file path or symbol name. Run `roam search {}` to find symbols.".format(target)
|
|
507
|
+
)
|
|
508
|
+
raise SystemExit(1)
|
|
509
|
+
|
|
510
|
+
if target_type == "file":
|
|
511
|
+
_output_file_metrics(conn, target_id, target, json_mode, token_budget)
|
|
512
|
+
else:
|
|
513
|
+
_output_symbol_metrics(conn, target_id, target, json_mode, token_budget)
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
# ---------------------------------------------------------------------------
|
|
517
|
+
# Output helpers
|
|
518
|
+
# ---------------------------------------------------------------------------
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
def _output_symbol_metrics(conn, symbol_id, target, json_mode, budget):
|
|
522
|
+
"""Produce output for a symbol target."""
|
|
523
|
+
sym_row = conn.execute(
|
|
524
|
+
"SELECT s.id, s.name, s.kind, s.qualified_name, s.line_start, "
|
|
525
|
+
"s.line_end, f.path AS file_path "
|
|
526
|
+
"FROM symbols s JOIN files f ON s.file_id = f.id "
|
|
527
|
+
"WHERE s.id = ?",
|
|
528
|
+
(symbol_id,),
|
|
529
|
+
).fetchone()
|
|
530
|
+
|
|
531
|
+
if not sym_row:
|
|
532
|
+
click.echo(f"Symbol not found: {target}")
|
|
533
|
+
raise SystemExit(1)
|
|
534
|
+
|
|
535
|
+
sm = collect_symbol_metrics(conn, symbol_id)
|
|
536
|
+
health = _health_label(sm)
|
|
537
|
+
|
|
538
|
+
display_name = sym_row["qualified_name"] or sym_row["name"]
|
|
539
|
+
location = loc(sym_row["file_path"], sym_row["line_start"])
|
|
540
|
+
|
|
541
|
+
if json_mode:
|
|
542
|
+
click.echo(
|
|
543
|
+
to_json(
|
|
544
|
+
json_envelope(
|
|
545
|
+
"metrics",
|
|
546
|
+
budget=budget,
|
|
547
|
+
summary={
|
|
548
|
+
"verdict": f"{display_name}: health={health}",
|
|
549
|
+
"target": display_name,
|
|
550
|
+
"target_type": "symbol",
|
|
551
|
+
"health": health,
|
|
552
|
+
},
|
|
553
|
+
target_type="symbol",
|
|
554
|
+
name=display_name,
|
|
555
|
+
kind=sym_row["kind"],
|
|
556
|
+
location=location,
|
|
557
|
+
metrics=sm,
|
|
558
|
+
)
|
|
559
|
+
)
|
|
560
|
+
)
|
|
561
|
+
return
|
|
562
|
+
|
|
563
|
+
click.echo(f"VERDICT: {display_name}: health={health}")
|
|
564
|
+
click.echo(f" type: {abbrev_kind(sym_row['kind'])} location: {location}")
|
|
565
|
+
click.echo()
|
|
566
|
+
click.echo(" Metrics:")
|
|
567
|
+
for key, val in sm.items():
|
|
568
|
+
label = key.replace("_", " ")
|
|
569
|
+
click.echo(f" {label:<20s} {val}")
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
def _output_file_metrics(conn, file_id, target, json_mode, budget):
|
|
573
|
+
"""Produce output for a file target."""
|
|
574
|
+
data = collect_file_metrics(conn, file_id)
|
|
575
|
+
if not data:
|
|
576
|
+
click.echo(f"File not found: {target}")
|
|
577
|
+
raise SystemExit(1)
|
|
578
|
+
|
|
579
|
+
fm = data["metrics"]
|
|
580
|
+
# Compute health for file level
|
|
581
|
+
file_health = _health_label(
|
|
582
|
+
{
|
|
583
|
+
"complexity": fm["complexity"],
|
|
584
|
+
"fan_out": fm["fan_out"],
|
|
585
|
+
"churn": fm["churn"],
|
|
586
|
+
"dead_code_risk": fm["dead_symbols"] > 0,
|
|
587
|
+
}
|
|
588
|
+
)
|
|
589
|
+
|
|
590
|
+
if json_mode:
|
|
591
|
+
click.echo(
|
|
592
|
+
to_json(
|
|
593
|
+
json_envelope(
|
|
594
|
+
"metrics",
|
|
595
|
+
budget=budget,
|
|
596
|
+
summary={
|
|
597
|
+
"verdict": f"{data['file']}: health={file_health}",
|
|
598
|
+
"target": data["file"],
|
|
599
|
+
"target_type": "file",
|
|
600
|
+
"health": file_health,
|
|
601
|
+
"symbol_count": fm["symbol_count"],
|
|
602
|
+
},
|
|
603
|
+
target_type="file",
|
|
604
|
+
file=data["file"],
|
|
605
|
+
language=data["language"],
|
|
606
|
+
file_role=data["file_role"],
|
|
607
|
+
metrics=fm,
|
|
608
|
+
symbols=[
|
|
609
|
+
{
|
|
610
|
+
"name": s["name"],
|
|
611
|
+
"kind": s["kind"],
|
|
612
|
+
"line_start": s["line_start"],
|
|
613
|
+
"complexity": s["complexity"],
|
|
614
|
+
"fan_in": s["fan_in"],
|
|
615
|
+
"fan_out": s["fan_out"],
|
|
616
|
+
"pagerank": round(s["pagerank"], 6),
|
|
617
|
+
"closeness": round(s["closeness"], 6),
|
|
618
|
+
"eigenvector": round(s["eigenvector"], 6),
|
|
619
|
+
"clustering_coefficient": round(s["clustering_coefficient"], 6),
|
|
620
|
+
"debt_score": round(s["debt_score"], 3),
|
|
621
|
+
"dead_code_risk": s["dead_code_risk"],
|
|
622
|
+
"loc": s["loc"],
|
|
623
|
+
"coverage_pct": s["coverage_pct"],
|
|
624
|
+
"covered_lines": s["covered_lines"],
|
|
625
|
+
"coverable_lines": s["coverable_lines"],
|
|
626
|
+
}
|
|
627
|
+
for s in data["symbols"]
|
|
628
|
+
],
|
|
629
|
+
)
|
|
630
|
+
)
|
|
631
|
+
)
|
|
632
|
+
return
|
|
633
|
+
|
|
634
|
+
click.echo(f"VERDICT: {data['file']}: health={file_health}")
|
|
635
|
+
click.echo(f" language: {data['language'] or 'unknown'} role: {data['file_role']}")
|
|
636
|
+
click.echo()
|
|
637
|
+
click.echo(" File Metrics:")
|
|
638
|
+
for key, val in fm.items():
|
|
639
|
+
label = key.replace("_", " ")
|
|
640
|
+
click.echo(f" {label:<20s} {val}")
|
|
641
|
+
|
|
642
|
+
if data["symbols"]:
|
|
643
|
+
click.echo()
|
|
644
|
+
click.echo(" Symbol Breakdown:")
|
|
645
|
+
rows = []
|
|
646
|
+
for s in data["symbols"]:
|
|
647
|
+
rows.append(
|
|
648
|
+
[
|
|
649
|
+
abbrev_kind(s["kind"]),
|
|
650
|
+
s["name"],
|
|
651
|
+
str(s["complexity"]),
|
|
652
|
+
str(s["fan_in"]),
|
|
653
|
+
str(s["fan_out"]),
|
|
654
|
+
f"{s['pagerank']:.4f}",
|
|
655
|
+
"Y" if s["dead_code_risk"] else "",
|
|
656
|
+
]
|
|
657
|
+
)
|
|
658
|
+
table = format_table(
|
|
659
|
+
["Kind", "Name", "CC", "In", "Out", "PageRank", "Dead?"],
|
|
660
|
+
rows,
|
|
661
|
+
)
|
|
662
|
+
for line in table.splitlines():
|
|
663
|
+
click.echo(f" {line}")
|