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.
Files changed (257) hide show
  1. mulle_roam_code-0.0.1.dist-info/METADATA +1590 -0
  2. mulle_roam_code-0.0.1.dist-info/RECORD +257 -0
  3. mulle_roam_code-0.0.1.dist-info/WHEEL +5 -0
  4. mulle_roam_code-0.0.1.dist-info/entry_points.txt +3 -0
  5. mulle_roam_code-0.0.1.dist-info/licenses/LICENSE +21 -0
  6. mulle_roam_code-0.0.1.dist-info/top_level.txt +1 -0
  7. roam/__init__.py +8 -0
  8. roam/__main__.py +5 -0
  9. roam/analysis/__init__.py +1 -0
  10. roam/analysis/effects.py +639 -0
  11. roam/analysis/taint.py +689 -0
  12. roam/api.py +238 -0
  13. roam/bridges/__init__.py +3 -0
  14. roam/bridges/base.py +49 -0
  15. roam/bridges/bridge_config.py +218 -0
  16. roam/bridges/bridge_protobuf.py +352 -0
  17. roam/bridges/bridge_rest_api.py +242 -0
  18. roam/bridges/bridge_salesforce.py +198 -0
  19. roam/bridges/bridge_template.py +247 -0
  20. roam/bridges/registry.py +51 -0
  21. roam/catalog/__init__.py +0 -0
  22. roam/catalog/detectors.py +2242 -0
  23. roam/catalog/fixes.py +86 -0
  24. roam/catalog/smells.py +599 -0
  25. roam/catalog/tasks.py +1260 -0
  26. roam/cli.py +548 -0
  27. roam/commands/__init__.py +0 -0
  28. roam/commands/changed_files.py +139 -0
  29. roam/commands/cmd_adversarial.py +699 -0
  30. roam/commands/cmd_affected.py +432 -0
  31. roam/commands/cmd_affected_tests.py +375 -0
  32. roam/commands/cmd_agent_context.py +168 -0
  33. roam/commands/cmd_agent_export.py +890 -0
  34. roam/commands/cmd_agent_plan.py +315 -0
  35. roam/commands/cmd_ai_ratio.py +765 -0
  36. roam/commands/cmd_ai_readiness.py +775 -0
  37. roam/commands/cmd_alerts.py +409 -0
  38. roam/commands/cmd_annotate.py +210 -0
  39. roam/commands/cmd_api_changes.py +796 -0
  40. roam/commands/cmd_api_drift.py +781 -0
  41. roam/commands/cmd_attest.py +913 -0
  42. roam/commands/cmd_auth_gaps.py +799 -0
  43. roam/commands/cmd_bisect.py +216 -0
  44. roam/commands/cmd_breaking.py +464 -0
  45. roam/commands/cmd_budget.py +413 -0
  46. roam/commands/cmd_bus_factor.py +403 -0
  47. roam/commands/cmd_capsule.py +275 -0
  48. roam/commands/cmd_check_rules.py +574 -0
  49. roam/commands/cmd_clean.py +147 -0
  50. roam/commands/cmd_closure.py +294 -0
  51. roam/commands/cmd_clusters.py +457 -0
  52. roam/commands/cmd_codeowners.py +600 -0
  53. roam/commands/cmd_complexity.py +364 -0
  54. roam/commands/cmd_config.py +310 -0
  55. roam/commands/cmd_context.py +1151 -0
  56. roam/commands/cmd_conventions.py +697 -0
  57. roam/commands/cmd_coupling.py +462 -0
  58. roam/commands/cmd_coverage_gaps.py +638 -0
  59. roam/commands/cmd_cut.py +235 -0
  60. roam/commands/cmd_dark_matter.py +127 -0
  61. roam/commands/cmd_dashboard.py +365 -0
  62. roam/commands/cmd_dead.py +1585 -0
  63. roam/commands/cmd_debt.py +731 -0
  64. roam/commands/cmd_deps.py +121 -0
  65. roam/commands/cmd_describe.py +959 -0
  66. roam/commands/cmd_dev_profile.py +580 -0
  67. roam/commands/cmd_diagnose.py +377 -0
  68. roam/commands/cmd_diff.py +633 -0
  69. roam/commands/cmd_digest.py +214 -0
  70. roam/commands/cmd_doc_staleness.py +334 -0
  71. roam/commands/cmd_docs_coverage.py +193 -0
  72. roam/commands/cmd_doctor.py +319 -0
  73. roam/commands/cmd_drift.py +377 -0
  74. roam/commands/cmd_duplicates.py +559 -0
  75. roam/commands/cmd_effects.py +362 -0
  76. roam/commands/cmd_endpoints.py +768 -0
  77. roam/commands/cmd_entry_points.py +358 -0
  78. roam/commands/cmd_fan.py +311 -0
  79. roam/commands/cmd_file.py +334 -0
  80. roam/commands/cmd_fingerprint.py +172 -0
  81. roam/commands/cmd_fitness.py +723 -0
  82. roam/commands/cmd_fn_coupling.py +295 -0
  83. roam/commands/cmd_forecast.py +366 -0
  84. roam/commands/cmd_grep.py +282 -0
  85. roam/commands/cmd_guard.py +434 -0
  86. roam/commands/cmd_health.py +922 -0
  87. roam/commands/cmd_hooks.py +458 -0
  88. roam/commands/cmd_hotspots.py +606 -0
  89. roam/commands/cmd_impact.py +228 -0
  90. roam/commands/cmd_index.py +102 -0
  91. roam/commands/cmd_ingest_trace.py +143 -0
  92. roam/commands/cmd_init.py +166 -0
  93. roam/commands/cmd_intent.py +459 -0
  94. roam/commands/cmd_invariants.py +301 -0
  95. roam/commands/cmd_layers.py +387 -0
  96. roam/commands/cmd_map.py +275 -0
  97. roam/commands/cmd_math.py +213 -0
  98. roam/commands/cmd_mcp_setup.py +135 -0
  99. roam/commands/cmd_metrics.py +663 -0
  100. roam/commands/cmd_migration_safety.py +712 -0
  101. roam/commands/cmd_minimap.py +519 -0
  102. roam/commands/cmd_missing_index.py +885 -0
  103. roam/commands/cmd_module.py +204 -0
  104. roam/commands/cmd_mutate.py +400 -0
  105. roam/commands/cmd_n1.py +967 -0
  106. roam/commands/cmd_onboard.py +740 -0
  107. roam/commands/cmd_orchestrate.py +180 -0
  108. roam/commands/cmd_orphan_routes.py +754 -0
  109. roam/commands/cmd_over_fetch.py +633 -0
  110. roam/commands/cmd_owner.py +299 -0
  111. roam/commands/cmd_partition.py +714 -0
  112. roam/commands/cmd_path_coverage.py +564 -0
  113. roam/commands/cmd_patterns.py +689 -0
  114. roam/commands/cmd_plan.py +616 -0
  115. roam/commands/cmd_plan_refactor.py +432 -0
  116. roam/commands/cmd_pr_diff.py +292 -0
  117. roam/commands/cmd_pr_risk.py +677 -0
  118. roam/commands/cmd_preflight.py +706 -0
  119. roam/commands/cmd_relate.py +405 -0
  120. roam/commands/cmd_report.py +304 -0
  121. roam/commands/cmd_reset.py +117 -0
  122. roam/commands/cmd_risk.py +533 -0
  123. roam/commands/cmd_rules.py +319 -0
  124. roam/commands/cmd_safe_delete.py +224 -0
  125. roam/commands/cmd_safe_zones.py +295 -0
  126. roam/commands/cmd_schema.py +166 -0
  127. roam/commands/cmd_search.py +295 -0
  128. roam/commands/cmd_search_semantic.py +101 -0
  129. roam/commands/cmd_secrets.py +636 -0
  130. roam/commands/cmd_semantic_diff.py +538 -0
  131. roam/commands/cmd_simulate.py +296 -0
  132. roam/commands/cmd_simulate_departure.py +632 -0
  133. roam/commands/cmd_sketch.py +160 -0
  134. roam/commands/cmd_smells.py +179 -0
  135. roam/commands/cmd_snapshot.py +50 -0
  136. roam/commands/cmd_spectral.py +183 -0
  137. roam/commands/cmd_split.py +432 -0
  138. roam/commands/cmd_suggest_refactoring.py +486 -0
  139. roam/commands/cmd_suggest_reviewers.py +491 -0
  140. roam/commands/cmd_supply_chain.py +831 -0
  141. roam/commands/cmd_symbol.py +135 -0
  142. roam/commands/cmd_syntax_check.py +301 -0
  143. roam/commands/cmd_test_gaps.py +574 -0
  144. roam/commands/cmd_testmap.py +308 -0
  145. roam/commands/cmd_tour.py +377 -0
  146. roam/commands/cmd_trace.py +304 -0
  147. roam/commands/cmd_trend.py +461 -0
  148. roam/commands/cmd_trends.py +929 -0
  149. roam/commands/cmd_understand.py +763 -0
  150. roam/commands/cmd_uses.py +170 -0
  151. roam/commands/cmd_verify.py +812 -0
  152. roam/commands/cmd_verify_imports.py +435 -0
  153. roam/commands/cmd_vibe_check.py +932 -0
  154. roam/commands/cmd_visualize.py +361 -0
  155. roam/commands/cmd_vuln_map.py +139 -0
  156. roam/commands/cmd_vuln_reach.py +289 -0
  157. roam/commands/cmd_vulns.py +414 -0
  158. roam/commands/cmd_watch.py +759 -0
  159. roam/commands/cmd_weather.py +118 -0
  160. roam/commands/cmd_why.py +325 -0
  161. roam/commands/cmd_ws.py +755 -0
  162. roam/commands/cmd_xlang.py +170 -0
  163. roam/commands/context_helpers.py +1137 -0
  164. roam/commands/gate_presets.py +222 -0
  165. roam/commands/graph_helpers.py +93 -0
  166. roam/commands/metrics_history.py +298 -0
  167. roam/commands/next_steps.py +107 -0
  168. roam/commands/resolve.py +302 -0
  169. roam/competitor_site_data.py +2229 -0
  170. roam/coverage_reports.py +462 -0
  171. roam/db/__init__.py +0 -0
  172. roam/db/connection.py +359 -0
  173. roam/db/queries.py +151 -0
  174. roam/db/schema.py +335 -0
  175. roam/exit_codes.py +100 -0
  176. roam/graph/__init__.py +43 -0
  177. roam/graph/anomaly.py +493 -0
  178. roam/graph/builder.py +61 -0
  179. roam/graph/clusters.py +286 -0
  180. roam/graph/cycles.py +210 -0
  181. roam/graph/dark_matter.py +207 -0
  182. roam/graph/diff.py +467 -0
  183. roam/graph/fingerprint.py +297 -0
  184. roam/graph/layers.py +134 -0
  185. roam/graph/pagerank.py +171 -0
  186. roam/graph/partition.py +437 -0
  187. roam/graph/pathfinding.py +119 -0
  188. roam/graph/propagation.py +208 -0
  189. roam/graph/simulate.py +286 -0
  190. roam/graph/spectral.py +260 -0
  191. roam/index/__init__.py +5 -0
  192. roam/index/complexity.py +1108 -0
  193. roam/index/discovery.py +338 -0
  194. roam/index/file_roles.py +586 -0
  195. roam/index/git_stats.py +577 -0
  196. roam/index/incremental.py +75 -0
  197. roam/index/indexer.py +1191 -0
  198. roam/index/parser.py +461 -0
  199. roam/index/relations.py +537 -0
  200. roam/index/symbols.py +71 -0
  201. roam/index/test_conventions.py +508 -0
  202. roam/languages/__init__.py +21 -0
  203. roam/languages/apex_lang.py +241 -0
  204. roam/languages/aura_lang.py +228 -0
  205. roam/languages/base.py +111 -0
  206. roam/languages/c_lang.py +458 -0
  207. roam/languages/csharp_lang.py +1215 -0
  208. roam/languages/foxpro_lang.py +1554 -0
  209. roam/languages/generic_lang.py +702 -0
  210. roam/languages/go_lang.py +392 -0
  211. roam/languages/hcl_lang.py +398 -0
  212. roam/languages/java_lang.py +418 -0
  213. roam/languages/javascript_lang.py +799 -0
  214. roam/languages/kotlin_lang.py +147 -0
  215. roam/languages/objc_lang.py +401 -0
  216. roam/languages/php_lang.py +696 -0
  217. roam/languages/python_lang.py +850 -0
  218. roam/languages/registry.py +283 -0
  219. roam/languages/ruby_lang.py +429 -0
  220. roam/languages/rust_lang.py +545 -0
  221. roam/languages/sfxml_lang.py +276 -0
  222. roam/languages/swift_lang.py +173 -0
  223. roam/languages/typescript_lang.py +323 -0
  224. roam/languages/visualforce_lang.py +151 -0
  225. roam/languages/yaml_lang.py +566 -0
  226. roam/mcp_server.py +4794 -0
  227. roam/output/__init__.py +0 -0
  228. roam/output/formatter.py +557 -0
  229. roam/output/mermaid.py +62 -0
  230. roam/output/sarif.py +897 -0
  231. roam/output/schema_registry.py +57 -0
  232. roam/plugins.py +208 -0
  233. roam/refactor/__init__.py +0 -0
  234. roam/refactor/codegen.py +106 -0
  235. roam/refactor/transforms.py +711 -0
  236. roam/rules/__init__.py +0 -0
  237. roam/rules/ast_match.py +238 -0
  238. roam/rules/builtin.py +617 -0
  239. roam/rules/dataflow.py +515 -0
  240. roam/rules/engine.py +943 -0
  241. roam/runtime/__init__.py +3 -0
  242. roam/runtime/hotspots.py +138 -0
  243. roam/runtime/trace_ingest.py +535 -0
  244. roam/search/__init__.py +1 -0
  245. roam/search/framework_packs.py +673 -0
  246. roam/search/index_embeddings.py +732 -0
  247. roam/search/onnx_embeddings.py +233 -0
  248. roam/search/tfidf.py +355 -0
  249. roam/security/__init__.py +1 -0
  250. roam/security/vuln_reach.py +246 -0
  251. roam/security/vuln_store.py +334 -0
  252. roam/surface_counts.py +158 -0
  253. roam/workspace/__init__.py +3 -0
  254. roam/workspace/aggregator.py +426 -0
  255. roam/workspace/api_scanner.py +538 -0
  256. roam/workspace/config.py +95 -0
  257. roam/workspace/db.py +136 -0
@@ -0,0 +1,564 @@
1
+ """Find critical paths through the call graph that have zero test protection."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import fnmatch
6
+ from collections import deque
7
+
8
+ import click
9
+
10
+ from roam.commands.changed_files import is_test_file
11
+ from roam.commands.resolve import ensure_index
12
+ from roam.db.connection import open_db
13
+ from roam.output.formatter import abbrev_kind, json_envelope, loc, to_json
14
+
15
+ # ---------------------------------------------------------------------------
16
+ # Entry point discovery
17
+ # ---------------------------------------------------------------------------
18
+
19
+
20
+ def _find_entry_points(conn, from_pattern):
21
+ """Find symbols with outgoing edges but no incoming edges (call graph roots).
22
+
23
+ If from_pattern is provided, only symbols whose file path matches the
24
+ fnmatch glob are included.
25
+ """
26
+ rows = conn.execute(
27
+ "SELECT s.id, s.name, s.kind, f.path AS file_path, s.line_start "
28
+ "FROM symbols s "
29
+ "JOIN files f ON s.file_id = f.id "
30
+ "WHERE s.kind IN ('function', 'method') "
31
+ "AND s.id IN (SELECT source_id FROM edges) "
32
+ "AND s.id NOT IN (SELECT target_id FROM edges) "
33
+ "ORDER BY f.path, s.line_start"
34
+ ).fetchall()
35
+
36
+ entries = []
37
+ for r in rows:
38
+ if from_pattern and not fnmatch.fnmatch(r["file_path"], from_pattern):
39
+ continue
40
+ entries.append(
41
+ {
42
+ "id": r["id"],
43
+ "name": r["name"],
44
+ "kind": r["kind"],
45
+ "file": r["file_path"],
46
+ "line": r["line_start"] or 0,
47
+ }
48
+ )
49
+ return entries
50
+
51
+
52
+ # ---------------------------------------------------------------------------
53
+ # Sink discovery
54
+ # ---------------------------------------------------------------------------
55
+
56
+
57
+ def _find_sinks_from_effects(conn, to_pattern):
58
+ """Find sink symbols from symbol_effects table (writes_db, network, filesystem)."""
59
+ try:
60
+ rows = conn.execute(
61
+ "SELECT DISTINCT se.symbol_id, s.name, s.kind, f.path AS file_path, "
62
+ " s.line_start, se.effect_type "
63
+ "FROM symbol_effects se "
64
+ "JOIN symbols s ON se.symbol_id = s.id "
65
+ "JOIN files f ON s.file_id = f.id "
66
+ "WHERE se.source = 'direct' "
67
+ "AND se.effect_type IN ('writes_db', 'network', 'filesystem')"
68
+ ).fetchall()
69
+ except Exception:
70
+ return {}, {}
71
+
72
+ sink_ids = {}
73
+ sink_effects = {}
74
+ for r in rows:
75
+ if to_pattern and not fnmatch.fnmatch(r["file_path"], to_pattern):
76
+ continue
77
+ sid = r["symbol_id"]
78
+ sink_ids[sid] = {
79
+ "id": sid,
80
+ "name": r["name"],
81
+ "kind": r["kind"],
82
+ "file": r["file_path"],
83
+ "line": r["line_start"] or 0,
84
+ }
85
+ # Keep strongest effect type per symbol
86
+ sink_effects[sid] = r["effect_type"]
87
+
88
+ return sink_ids, sink_effects
89
+
90
+
91
+ def _find_sinks_fallback(conn, to_pattern):
92
+ """Fallback: leaf nodes with incoming but no outgoing edges."""
93
+ rows = conn.execute(
94
+ "SELECT s.id, s.name, s.kind, f.path AS file_path, s.line_start "
95
+ "FROM symbols s "
96
+ "JOIN files f ON s.file_id = f.id "
97
+ "WHERE s.kind IN ('function', 'method') "
98
+ "AND s.id IN (SELECT target_id FROM edges) "
99
+ "AND s.id NOT IN (SELECT source_id FROM edges) "
100
+ "ORDER BY f.path, s.line_start"
101
+ ).fetchall()
102
+
103
+ sink_ids = {}
104
+ for r in rows:
105
+ if to_pattern and not fnmatch.fnmatch(r["file_path"], to_pattern):
106
+ continue
107
+ sink_ids[r["id"]] = {
108
+ "id": r["id"],
109
+ "name": r["name"],
110
+ "kind": r["kind"],
111
+ "file": r["file_path"],
112
+ "line": r["line_start"] or 0,
113
+ }
114
+ return sink_ids
115
+
116
+
117
+ # ---------------------------------------------------------------------------
118
+ # BFS path finding
119
+ # ---------------------------------------------------------------------------
120
+
121
+
122
+ def _find_paths(conn, entry_id, sink_ids, max_depth):
123
+ """BFS from entry_id, return list of paths (as node ID lists) that reach any sink."""
124
+ paths = []
125
+ queue = deque()
126
+ queue.append((entry_id, [entry_id]))
127
+ visited = {entry_id}
128
+
129
+ while queue:
130
+ current, path = queue.popleft()
131
+ if len(path) > max_depth:
132
+ continue
133
+ if current in sink_ids and len(path) > 1:
134
+ paths.append(path)
135
+ continue # don't continue traversal past a sink
136
+
137
+ callees = conn.execute(
138
+ "SELECT target_id FROM edges WHERE source_id = ?",
139
+ (current,),
140
+ ).fetchall()
141
+ for row in callees:
142
+ tid = row["target_id"]
143
+ if tid not in visited:
144
+ visited.add(tid)
145
+ queue.append((tid, path + [tid]))
146
+
147
+ return paths
148
+
149
+
150
+ # ---------------------------------------------------------------------------
151
+ # Test coverage check
152
+ # ---------------------------------------------------------------------------
153
+
154
+
155
+ def _build_tested_set(conn):
156
+ """Return a set of symbol IDs that are directly called by test code."""
157
+ tested = set()
158
+
159
+ # Symbols that live in test files are inherently test symbols
160
+ test_file_rows = conn.execute("SELECT f.id, f.path FROM files f").fetchall()
161
+ test_file_ids = {r["id"] for r in test_file_rows if is_test_file(r["path"])}
162
+
163
+ if not test_file_ids:
164
+ return tested
165
+
166
+ # All symbols that are targets of edges originating from test symbols
167
+ for file_id in test_file_ids:
168
+ rows = conn.execute(
169
+ "SELECT e.target_id FROM edges e JOIN symbols s ON e.source_id = s.id WHERE s.file_id = ?",
170
+ (file_id,),
171
+ ).fetchall()
172
+ for r in rows:
173
+ tested.add(r["target_id"])
174
+
175
+ # Also include symbols that live inside test files themselves
176
+ for file_id in test_file_ids:
177
+ rows = conn.execute(
178
+ "SELECT id FROM symbols WHERE file_id = ?",
179
+ (file_id,),
180
+ ).fetchall()
181
+ for r in rows:
182
+ tested.add(r["id"])
183
+
184
+ return tested
185
+
186
+
187
+ # ---------------------------------------------------------------------------
188
+ # Path risk classification
189
+ # ---------------------------------------------------------------------------
190
+
191
+ _DESTRUCTIVE_EFFECTS = {"writes_db"}
192
+
193
+
194
+ def _classify_risk(path_ids, tested_set, sink_effects):
195
+ """Return a risk label for a single path.
196
+
197
+ CRITICAL — zero tested nodes AND sink has destructive effect (writes_db)
198
+ HIGH — zero tested nodes
199
+ MEDIUM — only entry point tested, rest untested
200
+ LOW — most nodes tested
201
+ """
202
+ tested_count = sum(1 for nid in path_ids if nid in tested_set)
203
+ total = len(path_ids)
204
+ sink_id = path_ids[-1]
205
+ sink_effect = sink_effects.get(sink_id, "")
206
+
207
+ if tested_count == 0:
208
+ if sink_effect in _DESTRUCTIVE_EFFECTS:
209
+ return "CRITICAL"
210
+ return "HIGH"
211
+ if tested_count == 1 and path_ids[0] in tested_set and total > 1:
212
+ return "MEDIUM"
213
+ ratio = tested_count / total
214
+ if ratio >= 0.5:
215
+ return "LOW"
216
+ return "MEDIUM"
217
+
218
+
219
+ # ---------------------------------------------------------------------------
220
+ # Greedy set cover: optimal test insertion points
221
+ # ---------------------------------------------------------------------------
222
+
223
+
224
+ def _suggest_test_points(untested_paths, tested_set):
225
+ """Return an ordered list of symbols to test for maximum path coverage.
226
+
227
+ Uses greedy set cover: at each step pick the node that covers the most
228
+ currently-uncovered paths.
229
+ """
230
+ if not untested_paths:
231
+ return []
232
+
233
+ # Count how many untested paths each node (that is itself untested) appears in
234
+ node_path_count = {}
235
+ for path in untested_paths:
236
+ for nid in path:
237
+ if nid not in tested_set:
238
+ node_path_count[nid] = node_path_count.get(nid, 0) + 1
239
+
240
+ suggestions = []
241
+ remaining_paths = list(range(len(untested_paths)))
242
+
243
+ while remaining_paths and node_path_count:
244
+ # Pick node covering most remaining uncovered paths
245
+ best_node = max(node_path_count, key=lambda n: node_path_count[n])
246
+ best_count = node_path_count[best_node]
247
+ if best_count == 0:
248
+ break
249
+
250
+ suggestions.append((best_node, best_count))
251
+
252
+ # Remove paths covered by best_node
253
+ new_remaining = []
254
+ newly_covered = set()
255
+ for pi in remaining_paths:
256
+ path = untested_paths[pi]
257
+ if best_node in path:
258
+ newly_covered.add(pi)
259
+ # Also mark all untested nodes in that path as covering fewer paths
260
+ else:
261
+ new_remaining.append(pi)
262
+ remaining_paths = new_remaining
263
+
264
+ # Update counts: remove covered paths contribution
265
+ for pi in newly_covered:
266
+ path = untested_paths[pi]
267
+ for nid in path:
268
+ if nid in node_path_count:
269
+ node_path_count[nid] = max(0, node_path_count[nid] - 1)
270
+
271
+ # Remove the chosen node so it isn't selected again
272
+ del node_path_count[best_node]
273
+
274
+ return suggestions
275
+
276
+
277
+ # ---------------------------------------------------------------------------
278
+ # CLI command
279
+ # ---------------------------------------------------------------------------
280
+
281
+
282
+ @click.command("path-coverage")
283
+ @click.option(
284
+ "--from",
285
+ "from_pattern",
286
+ default=None,
287
+ help="Glob to filter entry points by file path (e.g. 'api/*').",
288
+ )
289
+ @click.option("--to", "to_pattern", default=None, help="Glob to filter sinks by file path (e.g. 'db*').")
290
+ @click.option("--max-depth", default=8, show_default=True, help="Maximum BFS depth for path search.")
291
+ @click.pass_context
292
+ def path_coverage(ctx, from_pattern, to_pattern, max_depth):
293
+ """Find critical untested paths from entry points to sensitive sinks.
294
+
295
+ Traces call graph paths from entry points (functions with no callers) to
296
+ sensitive sinks (functions with side effects like DB writes or network I/O)
297
+ and identifies which paths have zero test coverage. Suggests the minimal
298
+ set of test insertion points that would cover the most untested paths.
299
+
300
+ \b
301
+ Examples:
302
+ roam path-coverage
303
+ roam path-coverage --from "api/*"
304
+ roam path-coverage --to "db*"
305
+ roam path-coverage --max-depth 5
306
+ """
307
+ json_mode = ctx.obj.get("json") if ctx.obj else False
308
+ ensure_index()
309
+
310
+ with open_db(readonly=True) as conn:
311
+ # --- 1. Entry points ---
312
+ entries = _find_entry_points(conn, from_pattern)
313
+
314
+ # --- 2. Sinks ---
315
+ sink_info, sink_effects = _find_sinks_from_effects(conn, to_pattern)
316
+ if not sink_info:
317
+ # Fallback: use leaf nodes
318
+ sink_info = _find_sinks_fallback(conn, to_pattern)
319
+ sink_effects = {}
320
+
321
+ if not entries or not sink_info:
322
+ _no_paths_output(
323
+ json_mode,
324
+ len(entries),
325
+ len(sink_info),
326
+ from_pattern,
327
+ to_pattern,
328
+ )
329
+ return
330
+
331
+ sink_id_set = set(sink_info.keys())
332
+
333
+ # --- 3. BFS path search ---
334
+ all_paths = []
335
+ for entry in entries:
336
+ paths = _find_paths(conn, entry["id"], sink_id_set, max_depth)
337
+ all_paths.extend(paths)
338
+
339
+ # Deduplicate paths (same node sequence)
340
+ seen_paths = set()
341
+ unique_paths = []
342
+ for p in all_paths:
343
+ key = tuple(p)
344
+ if key not in seen_paths:
345
+ seen_paths.add(key)
346
+ unique_paths.append(p)
347
+
348
+ if not unique_paths:
349
+ _no_paths_output(
350
+ json_mode,
351
+ len(entries),
352
+ len(sink_info),
353
+ from_pattern,
354
+ to_pattern,
355
+ )
356
+ return
357
+
358
+ # --- 4. Test coverage check ---
359
+ tested_set = _build_tested_set(conn)
360
+
361
+ # --- 5. Score and classify paths ---
362
+ # Build symbol info lookup for all nodes in paths
363
+ all_node_ids = set()
364
+ for path in unique_paths:
365
+ all_node_ids.update(path)
366
+
367
+ sym_info = {}
368
+ for nid in all_node_ids:
369
+ row = conn.execute(
370
+ "SELECT s.id, s.name, s.kind, f.path AS file_path, s.line_start "
371
+ "FROM symbols s JOIN files f ON s.file_id = f.id WHERE s.id = ?",
372
+ (nid,),
373
+ ).fetchone()
374
+ if row:
375
+ sym_info[nid] = {
376
+ "id": row["id"],
377
+ "name": row["name"],
378
+ "kind": row["kind"],
379
+ "file": row["file_path"],
380
+ "line": row["line_start"] or 0,
381
+ }
382
+
383
+ classified_paths = []
384
+ untested_paths_for_cover = []
385
+
386
+ for path_ids in unique_paths:
387
+ risk = _classify_risk(path_ids, tested_set, sink_effects)
388
+ tested_in_path = [nid for nid in path_ids if nid in tested_set]
389
+ tested_count = len(tested_in_path)
390
+ sink_id = path_ids[-1]
391
+ sink_effect = sink_effects.get(sink_id, "")
392
+
393
+ nodes = []
394
+ for nid in path_ids:
395
+ info = sym_info.get(nid, {})
396
+ nodes.append(
397
+ {
398
+ "id": nid,
399
+ "name": info.get("name", "?"),
400
+ "kind": info.get("kind", "?"),
401
+ "file": info.get("file", "?"),
402
+ "line": info.get("line", 0),
403
+ "tested": nid in tested_set,
404
+ }
405
+ )
406
+
407
+ classified_paths.append(
408
+ {
409
+ "risk": risk,
410
+ "nodes": nodes,
411
+ "tested_count": tested_count,
412
+ "total_count": len(path_ids),
413
+ "sink_effect": sink_effect,
414
+ "path_ids": path_ids,
415
+ }
416
+ )
417
+
418
+ if tested_count == 0:
419
+ untested_paths_for_cover.append(path_ids)
420
+
421
+ # Sort: CRITICAL first, then HIGH, MEDIUM, LOW
422
+ _RISK_ORDER = {"CRITICAL": 0, "HIGH": 1, "MEDIUM": 2, "LOW": 3}
423
+ classified_paths.sort(key=lambda p: _RISK_ORDER.get(p["risk"], 4))
424
+
425
+ # --- 6. Suggest test insertion points ---
426
+ suggestion_ids = _suggest_test_points(untested_paths_for_cover, tested_set)
427
+
428
+ suggestions = []
429
+ for nid, paths_covered in suggestion_ids[:10]:
430
+ info = sym_info.get(nid, {})
431
+ suggestions.append(
432
+ {
433
+ "symbol": info.get("name", "?"),
434
+ "file": info.get("file", "?"),
435
+ "line": info.get("line", 0),
436
+ "paths_covered": paths_covered,
437
+ }
438
+ )
439
+
440
+ # --- Summary counts ---
441
+ total_paths = len(classified_paths)
442
+ counts = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0}
443
+ for cp in classified_paths:
444
+ counts[cp["risk"]] = counts.get(cp["risk"], 0) + 1
445
+
446
+ untested_paths_count = counts["CRITICAL"] + counts["HIGH"]
447
+
448
+ critical_high = counts["CRITICAL"] + counts["HIGH"]
449
+ if counts["CRITICAL"] > 0:
450
+ verdict = (
451
+ f"{counts['CRITICAL']} critical path{'s' if counts['CRITICAL'] != 1 else ''} with zero test coverage"
452
+ )
453
+ elif counts["HIGH"] > 0:
454
+ verdict = f"{counts['HIGH']} high-risk path{'s' if counts['HIGH'] != 1 else ''} with zero test coverage"
455
+ elif critical_high == 0 and total_paths > 0:
456
+ verdict = f"{total_paths} path{'s' if total_paths != 1 else ''} found, all partially tested"
457
+ else:
458
+ verdict = f"{total_paths} path{'s' if total_paths != 1 else ''} found"
459
+
460
+ # --- Output ---
461
+ if json_mode:
462
+ # Strip internal path_ids before serialising
463
+ paths_clean = []
464
+ for cp in classified_paths:
465
+ paths_clean.append({k: v for k, v in cp.items() if k != "path_ids"})
466
+
467
+ click.echo(
468
+ to_json(
469
+ json_envelope(
470
+ "path-coverage",
471
+ summary={
472
+ "verdict": verdict,
473
+ "total_paths": total_paths,
474
+ "untested_paths": untested_paths_count,
475
+ "critical": counts["CRITICAL"],
476
+ "high": counts["HIGH"],
477
+ },
478
+ paths=paths_clean,
479
+ suggestions=suggestions,
480
+ entry_points_found=len(entries),
481
+ sinks_found=len(sink_info),
482
+ )
483
+ )
484
+ )
485
+ return
486
+
487
+ # --- Text output ---
488
+ click.echo(f"VERDICT: {verdict}")
489
+ click.echo()
490
+
491
+ if not classified_paths:
492
+ click.echo("No entry-to-sink paths found in the call graph.")
493
+ return
494
+
495
+ display_paths = classified_paths[:20]
496
+ for i, cp in enumerate(display_paths, 1):
497
+ tested_str = f"{cp['tested_count']}/{cp['total_count']} nodes tested"
498
+ click.echo(f"PATH {i} [{cp['risk']}]:")
499
+ for j, node in enumerate(cp["nodes"]):
500
+ tested_label = "TESTED" if node["tested"] else "UNTESTED"
501
+ kind_abbr = abbrev_kind(node["kind"])
502
+ location = loc(node["file"], node["line"])
503
+ sink_label = ""
504
+ if j == len(cp["nodes"]) - 1 and cp["sink_effect"]:
505
+ sink_label = f", sink: {cp['sink_effect']}"
506
+ if j == 0:
507
+ click.echo(f" {kind_abbr} {node['name']} {location} ({tested_label}{sink_label})")
508
+ else:
509
+ click.echo(f" -> {kind_abbr} {node['name']} {location} ({tested_label}{sink_label})")
510
+ click.echo(f" Risk: {tested_str}")
511
+ click.echo()
512
+
513
+ if len(classified_paths) > 20:
514
+ click.echo(f"(+{len(classified_paths) - 20} more paths)")
515
+ click.echo()
516
+
517
+ if suggestions:
518
+ click.echo("OPTIMAL TEST POINTS:")
519
+ for rank, s in enumerate(suggestions, 1):
520
+ click.echo(
521
+ f" {rank}. {s['symbol']} ({s['file']}:{s['line']}) "
522
+ f"-- covers {s['paths_covered']} untested path{'s' if s['paths_covered'] != 1 else ''}"
523
+ )
524
+ else:
525
+ click.echo("No test insertion suggestions (all paths have some coverage).")
526
+
527
+
528
+ def _no_paths_output(json_mode, entry_count, sink_count, from_pattern, to_pattern):
529
+ """Emit a graceful message when no entry→sink paths can be found."""
530
+ if from_pattern or to_pattern:
531
+ note = "No paths found matching the specified filters."
532
+ elif entry_count == 0:
533
+ note = "No entry points found (no functions with outgoing but no incoming edges)."
534
+ elif sink_count == 0:
535
+ note = "No sinks found (no functions with side effects or leaf nodes)."
536
+ else:
537
+ note = "No paths found between entry points and sinks."
538
+
539
+ verdict = "no critical paths found"
540
+
541
+ if json_mode:
542
+ click.echo(
543
+ to_json(
544
+ json_envelope(
545
+ "path-coverage",
546
+ summary={
547
+ "verdict": verdict,
548
+ "total_paths": 0,
549
+ "untested_paths": 0,
550
+ "critical": 0,
551
+ "high": 0,
552
+ },
553
+ paths=[],
554
+ suggestions=[],
555
+ entry_points_found=entry_count,
556
+ sinks_found=sink_count,
557
+ note=note,
558
+ )
559
+ )
560
+ )
561
+ else:
562
+ click.echo(f"VERDICT: {verdict}")
563
+ click.echo()
564
+ click.echo(note)