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,639 @@
1
+ """Effect classification and propagation for roam-code.
2
+
3
+ Classifies what functions DO (read DB, write DB, network I/O, filesystem
4
+ access, etc.) and propagates effects through the call graph so callers
5
+ inherit the transitive effects of their callees.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import re
11
+
12
+ # ---------------------------------------------------------------------------
13
+ # Effect taxonomy
14
+ # ---------------------------------------------------------------------------
15
+
16
+ PURE = "pure"
17
+ READS_DB = "reads_db"
18
+ WRITES_DB = "writes_db"
19
+ NETWORK = "network"
20
+ FILESYSTEM = "filesystem"
21
+ TIME = "time"
22
+ RANDOM = "random"
23
+ MUTATES_GLOBAL = "mutates_global"
24
+ CACHE = "cache"
25
+ QUEUE = "queue"
26
+ LOGGING = "logging"
27
+
28
+ ALL_EFFECTS = frozenset(
29
+ {
30
+ PURE,
31
+ READS_DB,
32
+ WRITES_DB,
33
+ NETWORK,
34
+ FILESYSTEM,
35
+ TIME,
36
+ RANDOM,
37
+ MUTATES_GLOBAL,
38
+ CACHE,
39
+ QUEUE,
40
+ LOGGING,
41
+ }
42
+ )
43
+
44
+ # ---------------------------------------------------------------------------
45
+ # Framework-aware pattern dictionaries
46
+ #
47
+ # Each entry: (compiled_regex, effect_type)
48
+ # Patterns are matched against function body text (between line_start and
49
+ # line_end). We compile once at import time for performance.
50
+ # ---------------------------------------------------------------------------
51
+
52
+
53
+ def _compile(patterns: list[tuple[str, str]]) -> list[tuple[re.Pattern, str]]:
54
+ """Compile (regex_str, effect) pairs."""
55
+ return [(re.compile(p, re.IGNORECASE), e) for p, e in patterns]
56
+
57
+
58
+ _PYTHON_PATTERNS = _compile(
59
+ [
60
+ # Database writes
61
+ (r"\.save\s*\(", WRITES_DB),
62
+ (r"\.create\s*\(", WRITES_DB),
63
+ (r"\.delete\s*\(", WRITES_DB),
64
+ (r"\.update\s*\(", WRITES_DB),
65
+ (r"\.bulk_create\s*\(", WRITES_DB),
66
+ (r"\.bulk_update\s*\(", WRITES_DB),
67
+ (r"\.execute\s*\(", WRITES_DB),
68
+ (r"\.executemany\s*\(", WRITES_DB),
69
+ (r"cursor\.", WRITES_DB),
70
+ (r"\.commit\s*\(", WRITES_DB),
71
+ (r"\.add\s*\(", WRITES_DB),
72
+ (r"session\.flush", WRITES_DB),
73
+ (r"\.insert\s*\(", WRITES_DB),
74
+ # Database reads
75
+ (r"\.objects\.", READS_DB),
76
+ (r"\.filter\s*\(", READS_DB),
77
+ (r"\.get\s*\(", READS_DB),
78
+ (r"\.all\s*\(", READS_DB),
79
+ (r"\.select\s*\(", READS_DB),
80
+ (r"\.fetchone\s*\(", READS_DB),
81
+ (r"\.fetchall\s*\(", READS_DB),
82
+ (r"\.fetchmany\s*\(", READS_DB),
83
+ (r"\.query\s*\(", READS_DB),
84
+ # Network
85
+ (r"requests\.\w+\s*\(", NETWORK),
86
+ (r"httpx\.\w+\s*\(", NETWORK),
87
+ (r"urllib\.", NETWORK),
88
+ (r"aiohttp\.", NETWORK),
89
+ (r"urlopen\s*\(", NETWORK),
90
+ (r"socket\.", NETWORK),
91
+ (r"grpc\.", NETWORK),
92
+ # Filesystem
93
+ (r"\bopen\s*\(", FILESYSTEM),
94
+ (r"Path\s*\(", FILESYSTEM),
95
+ (r"\bos\.(?:path|remove|rename|mkdir|rmdir|listdir|walk|unlink|stat)", FILESYSTEM),
96
+ (r"shutil\.", FILESYSTEM),
97
+ (r"pathlib\.", FILESYSTEM),
98
+ (r"\.read_text\s*\(", FILESYSTEM),
99
+ (r"\.write_text\s*\(", FILESYSTEM),
100
+ (r"\.read_bytes\s*\(", FILESYSTEM),
101
+ (r"\.write_bytes\s*\(", FILESYSTEM),
102
+ # Time
103
+ (r"time\.\w+\s*\(", TIME),
104
+ (r"datetime\.", TIME),
105
+ (r"sleep\s*\(", TIME),
106
+ # Random
107
+ (r"random\.\w+\s*\(", RANDOM),
108
+ (r"secrets\.", RANDOM),
109
+ (r"uuid\.", RANDOM),
110
+ # Global mutation
111
+ (r"\bglobal\s+\w+", MUTATES_GLOBAL),
112
+ (r"os\.environ\[", MUTATES_GLOBAL),
113
+ # Cache
114
+ (r"@cache\b", CACHE),
115
+ (r"@lru_cache", CACHE),
116
+ (r"@cached", CACHE),
117
+ (r"\.cache\.", CACHE),
118
+ (r"redis\.", CACHE),
119
+ (r"memcache", CACHE),
120
+ # Queue
121
+ (r"\.send_message\s*\(", QUEUE),
122
+ (r"\.publish\s*\(", QUEUE),
123
+ (r"\.put\s*\(", QUEUE),
124
+ (r"celery\.", QUEUE),
125
+ (r"\.delay\s*\(", QUEUE),
126
+ (r"\.apply_async\s*\(", QUEUE),
127
+ # Logging
128
+ (r"logger\.\w+\s*\(", LOGGING),
129
+ (r"logging\.\w+\s*\(", LOGGING),
130
+ (r"\blog\.\w+\s*\(", LOGGING),
131
+ (r"print\s*\(", LOGGING),
132
+ ]
133
+ )
134
+
135
+ _JAVASCRIPT_PATTERNS = _compile(
136
+ [
137
+ # Network
138
+ (r"\bfetch\s*\(", NETWORK),
139
+ (r"axios\.\w+\s*\(", NETWORK),
140
+ (r"XMLHttpRequest", NETWORK),
141
+ (r"\.ajax\s*\(", NETWORK),
142
+ (r"http\.\w+\s*\(", NETWORK),
143
+ (r"ws\.send\s*\(", NETWORK),
144
+ (r"WebSocket\s*\(", NETWORK),
145
+ # Database writes
146
+ (r"\.save\s*\(", WRITES_DB),
147
+ (r"\.create\s*\(", WRITES_DB),
148
+ (r"\.insertOne\s*\(", WRITES_DB),
149
+ (r"\.insertMany\s*\(", WRITES_DB),
150
+ (r"\.updateOne\s*\(", WRITES_DB),
151
+ (r"\.updateMany\s*\(", WRITES_DB),
152
+ (r"\.deleteOne\s*\(", WRITES_DB),
153
+ (r"\.deleteMany\s*\(", WRITES_DB),
154
+ (r"\.destroy\s*\(", WRITES_DB),
155
+ (r"\.execute\s*\(", WRITES_DB),
156
+ (r"\.query\s*\(", WRITES_DB),
157
+ (r"\.run\s*\(", WRITES_DB),
158
+ # Database reads
159
+ (r"\.find\s*\(", READS_DB),
160
+ (r"\.findOne\s*\(", READS_DB),
161
+ (r"\.findById\s*\(", READS_DB),
162
+ (r"\.findAll\s*\(", READS_DB),
163
+ (r"\.select\s*\(", READS_DB),
164
+ (r"\.where\s*\(", READS_DB),
165
+ # Filesystem
166
+ (r"fs\.\w+\s*\(", FILESYSTEM),
167
+ (r"readFile\w*\s*\(", FILESYSTEM),
168
+ (r"writeFile\w*\s*\(", FILESYSTEM),
169
+ (r"\.createReadStream\s*\(", FILESYSTEM),
170
+ (r"\.createWriteStream\s*\(", FILESYSTEM),
171
+ # Time
172
+ (r"setTimeout\s*\(", TIME),
173
+ (r"setInterval\s*\(", TIME),
174
+ (r"Date\.\w+\s*\(", TIME),
175
+ (r"new Date\s*\(", TIME),
176
+ # Random
177
+ (r"Math\.random\s*\(", RANDOM),
178
+ (r"crypto\.random", RANDOM),
179
+ # Global mutation
180
+ (r"globalThis\.", MUTATES_GLOBAL),
181
+ (r"window\.\w+\s*=", MUTATES_GLOBAL),
182
+ (r"process\.env\.", MUTATES_GLOBAL),
183
+ # Cache
184
+ (r"localStorage\.", CACHE),
185
+ (r"sessionStorage\.", CACHE),
186
+ (r"\.setItem\s*\(", CACHE),
187
+ (r"\.getItem\s*\(", CACHE),
188
+ # Queue
189
+ (r"\.emit\s*\(", QUEUE),
190
+ (r"\.publish\s*\(", QUEUE),
191
+ (r"\.postMessage\s*\(", QUEUE),
192
+ # Logging
193
+ (r"console\.\w+\s*\(", LOGGING),
194
+ ]
195
+ )
196
+
197
+ _PHP_PATTERNS = _compile(
198
+ [
199
+ # Database writes
200
+ (r"->save\s*\(", WRITES_DB),
201
+ (r"::create\s*\(", WRITES_DB),
202
+ (r"->insert\s*\(", WRITES_DB),
203
+ (r"->update\s*\(", WRITES_DB),
204
+ (r"->delete\s*\(", WRITES_DB),
205
+ (r"DB::insert\b", WRITES_DB),
206
+ (r"DB::update\b", WRITES_DB),
207
+ (r"DB::delete\b", WRITES_DB),
208
+ (r"DB::statement\b", WRITES_DB),
209
+ (r"->execute\s*\(", WRITES_DB),
210
+ (r"->exec\s*\(", WRITES_DB),
211
+ # Database reads
212
+ (r"DB::select\b", READS_DB),
213
+ (r"DB::table\b", READS_DB),
214
+ (r"->get\s*\(", READS_DB),
215
+ (r"->find\s*\(", READS_DB),
216
+ (r"->first\s*\(", READS_DB),
217
+ (r"->where\s*\(", READS_DB),
218
+ (r"->select\s*\(", READS_DB),
219
+ (r"->fetchAll\s*\(", READS_DB),
220
+ (r"->fetch\s*\(", READS_DB),
221
+ # Network
222
+ (r"curl_\w+\s*\(", NETWORK),
223
+ (r"file_get_contents\s*\(", NETWORK),
224
+ (r"Http::", NETWORK),
225
+ (r"Guzzle", NETWORK),
226
+ (r"->request\s*\(", NETWORK),
227
+ # Filesystem
228
+ (r"fopen\s*\(", FILESYSTEM),
229
+ (r"fwrite\s*\(", FILESYSTEM),
230
+ (r"fread\s*\(", FILESYSTEM),
231
+ (r"file_put_contents\s*\(", FILESYSTEM),
232
+ (r"unlink\s*\(", FILESYSTEM),
233
+ (r"mkdir\s*\(", FILESYSTEM),
234
+ (r"rmdir\s*\(", FILESYSTEM),
235
+ (r"is_file\s*\(", FILESYSTEM),
236
+ # Time
237
+ (r"time\s*\(", TIME),
238
+ (r"strtotime\s*\(", TIME),
239
+ (r"Carbon::", TIME),
240
+ (r"new DateTime\b", TIME),
241
+ (r"sleep\s*\(", TIME),
242
+ # Random
243
+ (r"rand\s*\(", RANDOM),
244
+ (r"mt_rand\s*\(", RANDOM),
245
+ (r"random_\w+\s*\(", RANDOM),
246
+ (r"Str::random\s*\(", RANDOM),
247
+ # Global mutation
248
+ (r"\$GLOBALS\[", MUTATES_GLOBAL),
249
+ (r"\$_SESSION\[", MUTATES_GLOBAL),
250
+ (r"putenv\s*\(", MUTATES_GLOBAL),
251
+ # Cache
252
+ (r"Cache::", CACHE),
253
+ (r"->remember\s*\(", CACHE),
254
+ (r"->forever\s*\(", CACHE),
255
+ (r"Redis::", CACHE),
256
+ # Queue
257
+ (r"dispatch\s*\(", QUEUE),
258
+ (r"Queue::", QUEUE),
259
+ (r"->onQueue\s*\(", QUEUE),
260
+ (r"Event::", QUEUE),
261
+ # Logging
262
+ (r"Log::", LOGGING),
263
+ (r"->info\s*\(", LOGGING),
264
+ (r"->error\s*\(", LOGGING),
265
+ (r"->warning\s*\(", LOGGING),
266
+ (r"error_log\s*\(", LOGGING),
267
+ ]
268
+ )
269
+
270
+ _GO_PATTERNS = _compile(
271
+ [
272
+ # Database
273
+ (r"\.Query\w*\s*\(", READS_DB),
274
+ (r"\.Exec\w*\s*\(", WRITES_DB),
275
+ (r"\.Prepare\s*\(", READS_DB),
276
+ (r"tx\.Commit\s*\(", WRITES_DB),
277
+ # Network
278
+ (r"http\.\w+\s*\(", NETWORK),
279
+ (r"net\.Dial\w*\s*\(", NETWORK),
280
+ (r"grpc\.", NETWORK),
281
+ # Filesystem
282
+ (r"os\.(?:Open|Create|Remove|Mkdir|ReadFile|WriteFile|Stat)", FILESYSTEM),
283
+ (r"ioutil\.", FILESYSTEM),
284
+ (r"io\.Read", FILESYSTEM),
285
+ # Time
286
+ (r"time\.Now\s*\(", TIME),
287
+ (r"time\.Sleep\s*\(", TIME),
288
+ # Random
289
+ (r"rand\.\w+\s*\(", RANDOM),
290
+ # Logging
291
+ (r"log\.\w+\s*\(", LOGGING),
292
+ (r"fmt\.Print", LOGGING),
293
+ ]
294
+ )
295
+
296
+ # Language -> pattern list mapping
297
+ _LANGUAGE_PATTERNS: dict[str, list[tuple[re.Pattern, str]]] = {
298
+ "python": _PYTHON_PATTERNS,
299
+ "javascript": _JAVASCRIPT_PATTERNS,
300
+ "typescript": _JAVASCRIPT_PATTERNS,
301
+ "tsx": _JAVASCRIPT_PATTERNS,
302
+ "jsx": _JAVASCRIPT_PATTERNS,
303
+ "php": _PHP_PATTERNS,
304
+ "go": _GO_PATTERNS,
305
+ }
306
+
307
+
308
+ # ---------------------------------------------------------------------------
309
+ # String/comment exclusion via tree-sitter
310
+ # ---------------------------------------------------------------------------
311
+
312
+ _STRING_COMMENT_TYPES = frozenset(
313
+ {
314
+ "string",
315
+ "string_literal",
316
+ "template_string",
317
+ "raw_string",
318
+ "comment",
319
+ "line_comment",
320
+ "block_comment",
321
+ "string_content",
322
+ "interpreted_string_literal",
323
+ "encapsed_string",
324
+ "heredoc_body",
325
+ "nowdoc_body",
326
+ }
327
+ )
328
+
329
+
330
+ def _collect_excluded_ranges(tree, source: bytes, line_start: int, line_end: int) -> list[tuple[int, int]]:
331
+ """Collect byte ranges of strings/comments within [line_start, line_end].
332
+
333
+ Returns list of (start_byte, end_byte) that should be excluded from
334
+ pattern matching.
335
+ """
336
+ ranges = []
337
+ if tree is None:
338
+ return ranges
339
+
340
+ # Convert line numbers to byte offsets for the body
341
+ lines = source.split(b"\n")
342
+ body_start_byte = sum(len(lines[i]) + 1 for i in range(min(line_start - 1, len(lines))))
343
+ body_end_byte = sum(len(lines[i]) + 1 for i in range(min(line_end, len(lines))))
344
+
345
+ def _walk(node):
346
+ # Skip nodes entirely outside the body range
347
+ if node.end_byte <= body_start_byte or node.start_byte >= body_end_byte:
348
+ return
349
+ if node.type in _STRING_COMMENT_TYPES:
350
+ ranges.append((node.start_byte - body_start_byte, node.end_byte - body_start_byte))
351
+ return
352
+ for child in node.children:
353
+ _walk(child)
354
+
355
+ _walk(tree.root_node)
356
+ return ranges
357
+
358
+
359
+ def _in_excluded(pos: int, excluded: list[tuple[int, int]]) -> bool:
360
+ """Check if byte position is inside any excluded range."""
361
+ for start, end in excluded:
362
+ if start <= pos < end:
363
+ return True
364
+ return False
365
+
366
+
367
+ # ---------------------------------------------------------------------------
368
+ # Classification
369
+ # ---------------------------------------------------------------------------
370
+
371
+
372
+ def classify_symbol_effects(
373
+ body_text: str,
374
+ language: str,
375
+ tree=None,
376
+ source: bytes | None = None,
377
+ line_start: int = 0,
378
+ line_end: int = 0,
379
+ ) -> set[str]:
380
+ """Classify the side effects of a function body.
381
+
382
+ Args:
383
+ body_text: The text of the function body (lines between line_start..line_end).
384
+ language: Programming language identifier.
385
+ tree: Optional tree-sitter parse tree for AST-aware string/comment filtering.
386
+ source: Full file source bytes (needed with tree for range mapping).
387
+ line_start: 1-based start line of the function.
388
+ line_end: 1-based end line of the function.
389
+
390
+ Returns:
391
+ Set of effect type strings (e.g. {"reads_db", "network"}).
392
+ Empty set if no effects detected (the function is considered pure).
393
+ """
394
+ patterns = _LANGUAGE_PATTERNS.get(language, [])
395
+ if not patterns:
396
+ return set()
397
+
398
+ # Build excluded ranges from tree-sitter AST if available
399
+ excluded: list[tuple[int, int]] = []
400
+ if tree is not None and source is not None and line_start > 0:
401
+ excluded = _collect_excluded_ranges(tree, source, line_start, line_end)
402
+
403
+ effects: set[str] = set()
404
+
405
+ for pattern, effect in patterns:
406
+ for match in pattern.finditer(body_text):
407
+ # If we have AST info, check if match is inside a string/comment
408
+ if excluded and _in_excluded(match.start(), excluded):
409
+ continue
410
+ effects.add(effect)
411
+ break # One match per pattern is enough
412
+
413
+ return effects
414
+
415
+
416
+ def classify_file_effects(
417
+ conn,
418
+ file_id: int,
419
+ source: bytes,
420
+ language: str,
421
+ tree=None,
422
+ ) -> dict[int, set[str]]:
423
+ """Classify effects for all symbols in a file.
424
+
425
+ Args:
426
+ conn: SQLite connection.
427
+ file_id: File ID in the database.
428
+ source: Full file source bytes.
429
+ language: Programming language.
430
+ tree: Optional tree-sitter parse tree.
431
+
432
+ Returns:
433
+ {symbol_id: set[str]} mapping symbol IDs to their direct effects.
434
+ """
435
+ if language not in _LANGUAGE_PATTERNS:
436
+ return {}
437
+
438
+ rows = conn.execute(
439
+ "SELECT id, kind, line_start, line_end FROM symbols "
440
+ "WHERE file_id = ? AND kind IN ('function', 'method', 'constructor')",
441
+ (file_id,),
442
+ ).fetchall()
443
+
444
+ if not rows:
445
+ return {}
446
+
447
+ lines = source.decode("utf-8", errors="replace").split("\n")
448
+ results: dict[int, set[str]] = {}
449
+
450
+ for row in rows:
451
+ sym_id = row["id"]
452
+ ls = row["line_start"] or 1
453
+ le = row["line_end"] or len(lines)
454
+
455
+ # Extract body text (1-based lines)
456
+ body_lines = lines[max(0, ls - 1) : le]
457
+ body_text = "\n".join(body_lines)
458
+
459
+ effects = classify_symbol_effects(
460
+ body_text,
461
+ language,
462
+ tree=tree,
463
+ source=source,
464
+ line_start=ls,
465
+ line_end=le,
466
+ )
467
+ if effects:
468
+ results[sym_id] = effects
469
+
470
+ return results
471
+
472
+
473
+ # ---------------------------------------------------------------------------
474
+ # Propagation
475
+ # ---------------------------------------------------------------------------
476
+
477
+
478
+ def propagate_effects(
479
+ G,
480
+ direct_effects: dict[int, set[str]],
481
+ ) -> dict[int, set[str]]:
482
+ """Propagate effects through the call graph (bottom-up).
483
+
484
+ For each node, its transitive effects = direct effects UNION
485
+ effects of all callees. Uses reverse topological sort where
486
+ possible, with iteration for cycles.
487
+
488
+ Args:
489
+ G: NetworkX DiGraph (symbol graph).
490
+ direct_effects: {symbol_id: set[effect_str]} from classification.
491
+
492
+ Returns:
493
+ {symbol_id: set[str]} with transitive effects for all nodes
494
+ that have at least one effect (direct or inherited).
495
+ """
496
+ import networkx as nx
497
+
498
+ # Initialize with direct effects
499
+ all_effects: dict[int, set[str]] = {}
500
+ for sid, effects in direct_effects.items():
501
+ if sid in G:
502
+ all_effects[sid] = set(effects)
503
+
504
+ # Try topological order on condensation (handles cycles)
505
+ try:
506
+ condensation = nx.condensation(G)
507
+ # node_mapping: condensation node -> set of original nodes
508
+ members = condensation.graph.get("mapping", {})
509
+ # Reverse: original -> condensation
510
+ orig_to_scc: dict[int, int] = {}
511
+ for orig, scc_id in members.items():
512
+ orig_to_scc[orig] = scc_id
513
+
514
+ # Process in reverse topological order (leaves first)
515
+ for scc_id in reversed(list(nx.topological_sort(condensation))):
516
+ scc_nodes = [n for n, s in orig_to_scc.items() if s == scc_id]
517
+
518
+ # Within an SCC, all nodes share the same effects
519
+ scc_effects: set[str] = set()
520
+ for n in scc_nodes:
521
+ scc_effects.update(all_effects.get(n, set()))
522
+
523
+ # Collect effects from successors (callees outside this SCC)
524
+ for succ_scc in condensation.successors(scc_id):
525
+ succ_nodes = [n for n, s in orig_to_scc.items() if s == succ_scc]
526
+ for sn in succ_nodes:
527
+ scc_effects.update(all_effects.get(sn, set()))
528
+
529
+ # Apply to all nodes in this SCC
530
+ if scc_effects:
531
+ for n in scc_nodes:
532
+ all_effects[n] = scc_effects.copy()
533
+
534
+ except Exception:
535
+ # Fallback: simple iterative propagation
536
+ changed = True
537
+ max_iters = 20
538
+ iteration = 0
539
+ while changed and iteration < max_iters:
540
+ changed = False
541
+ iteration += 1
542
+ for node in G.nodes():
543
+ current = all_effects.get(node, set())
544
+ for succ in G.successors(node):
545
+ succ_effects = all_effects.get(succ, set())
546
+ new = succ_effects - current
547
+ if new:
548
+ all_effects.setdefault(node, set()).update(new)
549
+ changed = True
550
+
551
+ return all_effects
552
+
553
+
554
+ # ---------------------------------------------------------------------------
555
+ # Storage
556
+ # ---------------------------------------------------------------------------
557
+
558
+
559
+ def store_effects(
560
+ conn,
561
+ all_effects: dict[int, set[str]],
562
+ direct_effects: dict[int, set[str]],
563
+ ):
564
+ """Persist classified effects to the symbol_effects table.
565
+
566
+ Args:
567
+ conn: SQLite connection (writable).
568
+ all_effects: {symbol_id: set[str]} — full (transitive) effects.
569
+ direct_effects: {symbol_id: set[str]} — only direct effects.
570
+ """
571
+ conn.execute("DELETE FROM symbol_effects")
572
+
573
+ rows = []
574
+ for sym_id, effects in all_effects.items():
575
+ direct = direct_effects.get(sym_id, set())
576
+ for effect in effects:
577
+ source = "direct" if effect in direct else "transitive"
578
+ rows.append((sym_id, effect, source))
579
+
580
+ if rows:
581
+ conn.executemany(
582
+ "INSERT INTO symbol_effects (symbol_id, effect_type, source) VALUES (?, ?, ?)",
583
+ rows,
584
+ )
585
+
586
+
587
+ # ---------------------------------------------------------------------------
588
+ # Indexer integration entry point
589
+ # ---------------------------------------------------------------------------
590
+
591
+
592
+ def compute_and_store_effects(conn, root, G=None):
593
+ """Full effects pipeline: classify per file, propagate, store.
594
+
595
+ Called from the indexer after graph construction.
596
+ """
597
+ from roam.index.parser import parse_file
598
+
599
+ # 1. Classify direct effects for all files
600
+ direct_effects: dict[int, set[str]] = {}
601
+
602
+ files = conn.execute("SELECT id, path, language FROM files").fetchall()
603
+ for file_row in files:
604
+ file_id = file_row["id"]
605
+ language = file_row["language"]
606
+
607
+ if language not in _LANGUAGE_PATTERNS:
608
+ continue
609
+
610
+ full_path = root / file_row["path"]
611
+ try:
612
+ with open(full_path, "rb") as f:
613
+ source = f.read()
614
+ except OSError:
615
+ continue
616
+
617
+ # Parse for AST-aware filtering
618
+ tree, parsed_source, lang = parse_file(full_path, language)
619
+
620
+ effects = classify_file_effects(
621
+ conn,
622
+ file_id,
623
+ source,
624
+ language,
625
+ tree=tree,
626
+ )
627
+ direct_effects.update(effects)
628
+
629
+ if not direct_effects:
630
+ return
631
+
632
+ # 2. Propagate through call graph
633
+ if G is not None:
634
+ all_effects = propagate_effects(G, direct_effects)
635
+ else:
636
+ all_effects = dict(direct_effects)
637
+
638
+ # 3. Store
639
+ store_effects(conn, all_effects, direct_effects)