runtime_analysis 1.0.0

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 (267) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +115 -0
  3. data/LICENSE.txt +96 -0
  4. data/README.md +164 -0
  5. data/editor/nvim/README.md +278 -0
  6. data/editor/nvim/lua/runtime_analysis/connected.lua +565 -0
  7. data/editor/nvim/lua/runtime_analysis/go_to_class.lua +63 -0
  8. data/editor/nvim/lua/runtime_analysis/inline.lua +138 -0
  9. data/editor/nvim/lua/runtime_analysis/lens.lua +50 -0
  10. data/editor/nvim/lua/runtime_analysis/lsp.lua +92 -0
  11. data/editor/nvim/lua/runtime_analysis/menu.lua +98 -0
  12. data/editor/nvim/lua/runtime_analysis/preview.lua +70 -0
  13. data/editor/nvim/lua/runtime_analysis/project.lua +28 -0
  14. data/editor/nvim/lua/runtime_analysis/rerun.lua +39 -0
  15. data/editor/nvim/lua/runtime_analysis/text.lua +23 -0
  16. data/editor/nvim/lua/runtime_analysis/value_popup.lua +45 -0
  17. data/editor/nvim/plugin/runtime_analysis.lua +71 -0
  18. data/examples/trackers/PROMPT.md +86 -0
  19. data/examples/trackers/honeybadger.rb +19 -0
  20. data/examples/trackers/sentry.rb +16 -0
  21. data/ext/runtime_analysis/extconf.rb +68 -0
  22. data/ext/runtime_analysis/fast.c +775 -0
  23. data/ext/runtime_analysis/fast.h +59 -0
  24. data/ext/runtime_analysis/hold.c +560 -0
  25. data/ext/runtime_analysis/init.c +36 -0
  26. data/ext/runtime_analysis/names.c +78 -0
  27. data/ext/runtime_analysis/names.h +28 -0
  28. data/ext/runtime_analysis/novelty.c +139 -0
  29. data/ext/runtime_analysis/novelty.h +22 -0
  30. data/ext/runtime_analysis/os.c +73 -0
  31. data/ext/runtime_analysis/os.h +32 -0
  32. data/ext/runtime_analysis/ring.c +445 -0
  33. data/ext/runtime_analysis/rows.c +304 -0
  34. data/ext/runtime_analysis/sink.c +485 -0
  35. data/ext/runtime_analysis/sink.h +240 -0
  36. data/ext/runtime_analysis/state.c +254 -0
  37. data/ext/runtime_analysis/state.h +57 -0
  38. data/ext/runtime_analysis/wrapper.c +278 -0
  39. data/ext/runtime_analysis/wrapper.h +24 -0
  40. data/lib/runtime_analysis/errors.rb +34 -0
  41. data/lib/runtime_analysis/failsafe.rb +123 -0
  42. data/lib/runtime_analysis/host/controller_entry_point.rb +18 -0
  43. data/lib/runtime_analysis/host/declared.rb +152 -0
  44. data/lib/runtime_analysis/host/inject.rb +90 -0
  45. data/lib/runtime_analysis/host/job_runners.rb +27 -0
  46. data/lib/runtime_analysis/host/middleware.rb +23 -0
  47. data/lib/runtime_analysis/host/minitest_integration.rb +22 -0
  48. data/lib/runtime_analysis/host/railtie.rb +42 -0
  49. data/lib/runtime_analysis/host/reports.rb +54 -0
  50. data/lib/runtime_analysis/host/rspec_integration.rb +17 -0
  51. data/lib/runtime_analysis/host/web.rb +22 -0
  52. data/lib/runtime_analysis/host.rb +34 -0
  53. data/lib/runtime_analysis/inject.rb +5 -0
  54. data/lib/runtime_analysis/minitest.rb +9 -0
  55. data/lib/runtime_analysis/native/build.rb +17 -0
  56. data/lib/runtime_analysis/native/byte_size.rb +27 -0
  57. data/lib/runtime_analysis/native/call_site_logger/definitions.rb +51 -0
  58. data/lib/runtime_analysis/native/call_site_logger/held.rb +35 -0
  59. data/lib/runtime_analysis/native/call_site_logger/rows.rb +110 -0
  60. data/lib/runtime_analysis/native/call_site_logger/segments.rb +100 -0
  61. data/lib/runtime_analysis/native/call_site_logger/streams.rb +110 -0
  62. data/lib/runtime_analysis/native/call_site_logger.rb +80 -0
  63. data/lib/runtime_analysis/native/rotation_policy.rb +28 -0
  64. data/lib/runtime_analysis/native/sanitizer_runtime.rb +50 -0
  65. data/lib/runtime_analysis/native/sink.rb +12 -0
  66. data/lib/runtime_analysis/native.rb +74 -0
  67. data/lib/runtime_analysis/platform.rb +30 -0
  68. data/lib/runtime_analysis/query/index/anonymise.rb +98 -0
  69. data/lib/runtime_analysis/query/index/graph.rb +107 -0
  70. data/lib/runtime_analysis/query/index/graph_cache.rb +32 -0
  71. data/lib/runtime_analysis/query/index/reading/declared.rb +42 -0
  72. data/lib/runtime_analysis/query/index/reading/dict.rb +43 -0
  73. data/lib/runtime_analysis/query/index/reading/frame.rb +27 -0
  74. data/lib/runtime_analysis/query/index/reading/node.rb +45 -0
  75. data/lib/runtime_analysis/query/index/reading/path.rb +21 -0
  76. data/lib/runtime_analysis/query/index/reading/reruns.rb +30 -0
  77. data/lib/runtime_analysis/query/index/reading/rows.rb +128 -0
  78. data/lib/runtime_analysis/query/index/reading/stream.rb +82 -0
  79. data/lib/runtime_analysis/query/index/reading/tree.rb +66 -0
  80. data/lib/runtime_analysis/query/index/reading/tree_builder.rb +135 -0
  81. data/lib/runtime_analysis/query/index/reading/values.rb +49 -0
  82. data/lib/runtime_analysis/query/index/reading.rb +80 -0
  83. data/lib/runtime_analysis/query/index/shapes.rb +142 -0
  84. data/lib/runtime_analysis/query/index/signatures/active_support.rb +31 -0
  85. data/lib/runtime_analysis/query/index/signatures/core.json +1 -0
  86. data/lib/runtime_analysis/query/index/signatures/layers.rb +29 -0
  87. data/lib/runtime_analysis/query/index/signatures/lineage.rb +57 -0
  88. data/lib/runtime_analysis/query/index/signatures/overloads.rb +32 -0
  89. data/lib/runtime_analysis/query/index/signatures/rbs/entry.rb +95 -0
  90. data/lib/runtime_analysis/query/index/signatures/rbs/export.rb +76 -0
  91. data/lib/runtime_analysis/query/index/signatures/rbs/library.rb +43 -0
  92. data/lib/runtime_analysis/query/index/signatures/rbs/overload.rb +66 -0
  93. data/lib/runtime_analysis/query/index/signatures/rbs/project.rb +69 -0
  94. data/lib/runtime_analysis/query/index/signatures/rbs/types.rb +79 -0
  95. data/lib/runtime_analysis/query/index/signatures/rbs.rb +57 -0
  96. data/lib/runtime_analysis/query/index/signatures/sorbet/sig.rb +48 -0
  97. data/lib/runtime_analysis/query/index/signatures/sorbet/signed.rb +69 -0
  98. data/lib/runtime_analysis/query/index/signatures/sorbet/table.rb +43 -0
  99. data/lib/runtime_analysis/query/index/signatures/sorbet/types.rb +96 -0
  100. data/lib/runtime_analysis/query/index/signatures/sorbet.rb +150 -0
  101. data/lib/runtime_analysis/query/index/signatures/sources.rb +80 -0
  102. data/lib/runtime_analysis/query/index/signatures/written.rb +45 -0
  103. data/lib/runtime_analysis/query/index/signatures.rb +93 -0
  104. data/lib/runtime_analysis/query/index/source_named_graph.rb +84 -0
  105. data/lib/runtime_analysis/query/index/static/change_set.rb +20 -0
  106. data/lib/runtime_analysis/query/index/static/changes.rb +80 -0
  107. data/lib/runtime_analysis/query/index/static/code_visitor.rb +144 -0
  108. data/lib/runtime_analysis/query/index/static/klass.rb +13 -0
  109. data/lib/runtime_analysis/query/index/static/method.rb +19 -0
  110. data/lib/runtime_analysis/query/index/static/params.rb +52 -0
  111. data/lib/runtime_analysis/query/index/static/source.rb +69 -0
  112. data/lib/runtime_analysis/query/index/static/stub.rb +14 -0
  113. data/lib/runtime_analysis/query/index/static/stub_visitor.rb +133 -0
  114. data/lib/runtime_analysis/query/index/static/value.rb +17 -0
  115. data/lib/runtime_analysis/query/index/static/values.rb +71 -0
  116. data/lib/runtime_analysis/query/index/static/visibility.rb +40 -0
  117. data/lib/runtime_analysis/query/index/static.rb +85 -0
  118. data/lib/runtime_analysis/query/index.rb +32 -0
  119. data/lib/runtime_analysis/query/nodes.rb +60 -0
  120. data/lib/runtime_analysis/query/project/config.rb +108 -0
  121. data/lib/runtime_analysis/query/project.rb +40 -0
  122. data/lib/runtime_analysis/query/report/document.rb +84 -0
  123. data/lib/runtime_analysis/query/report/finding.rb +15 -0
  124. data/lib/runtime_analysis/query/report/json_renderer.rb +51 -0
  125. data/lib/runtime_analysis/query/report/list.rb +15 -0
  126. data/lib/runtime_analysis/query/report/location.rb +14 -0
  127. data/lib/runtime_analysis/query/report/one_row.rb +14 -0
  128. data/lib/runtime_analysis/query/report/outline.rb +15 -0
  129. data/lib/runtime_analysis/query/report/paint.rb +34 -0
  130. data/lib/runtime_analysis/query/report/related.rb +11 -0
  131. data/lib/runtime_analysis/query/report/table.rb +15 -0
  132. data/lib/runtime_analysis/query/report/text.rb +13 -0
  133. data/lib/runtime_analysis/query/report/text_renderer.rb +102 -0
  134. data/lib/runtime_analysis/query/report.rb +35 -0
  135. data/lib/runtime_analysis/query/surfaces/class_value.rb +17 -0
  136. data/lib/runtime_analysis/query/surfaces/cli/answer.rb +32 -0
  137. data/lib/runtime_analysis/query/surfaces/cli/command_line.rb +77 -0
  138. data/lib/runtime_analysis/query/surfaces/cli/usage.rb +32 -0
  139. data/lib/runtime_analysis/query/surfaces/cli.rb +101 -0
  140. data/lib/runtime_analysis/query/surfaces/crystallise/lexical.rb +44 -0
  141. data/lib/runtime_analysis/query/surfaces/crystallise/notation.rb +33 -0
  142. data/lib/runtime_analysis/query/surfaces/crystallise/observed.rb +102 -0
  143. data/lib/runtime_analysis/query/surfaces/crystallise/outline.rb +111 -0
  144. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_file.rb +100 -0
  145. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_member.rb +50 -0
  146. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_notation.rb +44 -0
  147. data/lib/runtime_analysis/query/surfaces/crystallise/seen.rb +74 -0
  148. data/lib/runtime_analysis/query/surfaces/crystallise/sig_text.rb +49 -0
  149. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_edit.rb +106 -0
  150. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_notation.rb +48 -0
  151. data/lib/runtime_analysis/query/surfaces/crystallise/subject.rb +55 -0
  152. data/lib/runtime_analysis/query/surfaces/crystallise.rb +94 -0
  153. data/lib/runtime_analysis/query/surfaces/mcp.rb +89 -0
  154. data/lib/runtime_analysis/query/surfaces/record/commands.rb +45 -0
  155. data/lib/runtime_analysis/query/surfaces/record/environment.rb +48 -0
  156. data/lib/runtime_analysis/query/surfaces/record/ignore.rb +62 -0
  157. data/lib/runtime_analysis/query/surfaces/record/question.rb +55 -0
  158. data/lib/runtime_analysis/query/surfaces/record/run.rb +30 -0
  159. data/lib/runtime_analysis/query/surfaces/record.rb +122 -0
  160. data/lib/runtime_analysis/query/surfaces/rerun.rb +87 -0
  161. data/lib/runtime_analysis/query/surfaces/setup/agents.rb +76 -0
  162. data/lib/runtime_analysis/query/surfaces/setup/changes.rb +107 -0
  163. data/lib/runtime_analysis/query/surfaces/setup/project.rb +80 -0
  164. data/lib/runtime_analysis/query/surfaces/setup/steps.md +205 -0
  165. data/lib/runtime_analysis/query/surfaces/setup.rb +98 -0
  166. data/lib/runtime_analysis/query/surfaces.rb +14 -0
  167. data/lib/runtime_analysis/query/tools/base.rb +86 -0
  168. data/lib/runtime_analysis/query/tools/method_lookup.rb +37 -0
  169. data/lib/runtime_analysis/query/tools/namespaces.rb +36 -0
  170. data/lib/runtime_analysis/query/tools/origins.rb +47 -0
  171. data/lib/runtime_analysis/query/tools/stubs.rb +53 -0
  172. data/lib/runtime_analysis/query/tools/unknown_method.rb +11 -0
  173. data/lib/runtime_analysis/query/tools/verify_mock.rb +145 -0
  174. data/lib/runtime_analysis/query/tools/what_calls.rb +67 -0
  175. data/lib/runtime_analysis/query/tools/what_it_receives.rb +142 -0
  176. data/lib/runtime_analysis/query/tools/where_nil_comes_from.rb +105 -0
  177. data/lib/runtime_analysis/query/tools/who_actually_answered.rb +82 -0
  178. data/lib/runtime_analysis/query/tools.rb +34 -0
  179. data/lib/runtime_analysis/query.rb +33 -0
  180. data/lib/runtime_analysis/recording/blob.rb +13 -0
  181. data/lib/runtime_analysis/recording/format.rb +32 -0
  182. data/lib/runtime_analysis/recording/opcodes.rb +30 -0
  183. data/lib/runtime_analysis/recording/raise_ids.rb +15 -0
  184. data/lib/runtime_analysis/recording/ruby_sink.rb +176 -0
  185. data/lib/runtime_analysis/recording/run_manifest.rb +110 -0
  186. data/lib/runtime_analysis/recording/seen/nothing.rb +15 -0
  187. data/lib/runtime_analysis/recording/seen/tally.rb +48 -0
  188. data/lib/runtime_analysis/recording/seen.rb +63 -0
  189. data/lib/runtime_analysis/recording/sink.rb +19 -0
  190. data/lib/runtime_analysis/recording/source_blob.rb +20 -0
  191. data/lib/runtime_analysis/recording.rb +52 -0
  192. data/lib/runtime_analysis/rspec.rb +8 -0
  193. data/lib/runtime_analysis/ruby_parsing/packages.rb +17 -0
  194. data/lib/runtime_analysis/ruby_parsing.rb +98 -0
  195. data/lib/runtime_analysis/trace/configuration/capturing.rb +32 -0
  196. data/lib/runtime_analysis/trace/configuration/per_run.rb +31 -0
  197. data/lib/runtime_analysis/trace/configuration/tier.rb +27 -0
  198. data/lib/runtime_analysis/trace/configuration.rb +115 -0
  199. data/lib/runtime_analysis/trace/entries.rb +47 -0
  200. data/lib/runtime_analysis/trace/hold/raises.rb +28 -0
  201. data/lib/runtime_analysis/trace/hold/requests.rb +47 -0
  202. data/lib/runtime_analysis/trace/hold/telling.rb +45 -0
  203. data/lib/runtime_analysis/trace/hold/unwound.rb +30 -0
  204. data/lib/runtime_analysis/trace/hold.rb +142 -0
  205. data/lib/runtime_analysis/trace/inclusion.rb +46 -0
  206. data/lib/runtime_analysis/trace/installer/alias_repair.rb +68 -0
  207. data/lib/runtime_analysis/trace/installer/answers.rb +24 -0
  208. data/lib/runtime_analysis/trace/installer/body_sync.rb +25 -0
  209. data/lib/runtime_analysis/trace/installer/built_classes.rb +43 -0
  210. data/lib/runtime_analysis/trace/installer/called_names.rb +33 -0
  211. data/lib/runtime_analysis/trace/installer/class_builders.rb +50 -0
  212. data/lib/runtime_analysis/trace/installer/deferral.rb +42 -0
  213. data/lib/runtime_analysis/trace/installer/hook.rb +170 -0
  214. data/lib/runtime_analysis/trace/installer/included_gems.rb +53 -0
  215. data/lib/runtime_analysis/trace/installer/probe.rb +28 -0
  216. data/lib/runtime_analysis/trace/installer/reflection.rb +50 -0
  217. data/lib/runtime_analysis/trace/installer/registry.rb +78 -0
  218. data/lib/runtime_analysis/trace/installer/sorbet.rb +29 -0
  219. data/lib/runtime_analysis/trace/installer/wrapping/ivar_writes.rb +62 -0
  220. data/lib/runtime_analysis/trace/installer/wrapping.rb +66 -0
  221. data/lib/runtime_analysis/trace/installer.rb +149 -0
  222. data/lib/runtime_analysis/trace/mechanism/block_entries.rb +29 -0
  223. data/lib/runtime_analysis/trace/mechanism/frame_readers.rb +61 -0
  224. data/lib/runtime_analysis/trace/mechanism/native_asks.rb +36 -0
  225. data/lib/runtime_analysis/trace/mechanism/native_sync.rb +28 -0
  226. data/lib/runtime_analysis/trace/mechanism/pure_wrapper.rb +41 -0
  227. data/lib/runtime_analysis/trace/mechanism/ruby_wrapper.rb +141 -0
  228. data/lib/runtime_analysis/trace/mechanism/wrapper.rb +79 -0
  229. data/lib/runtime_analysis/trace/mechanism/wrapper_shape.rb +40 -0
  230. data/lib/runtime_analysis/trace/mechanism.rb +28 -0
  231. data/lib/runtime_analysis/trace/nil_origin/answers.rb +24 -0
  232. data/lib/runtime_analysis/trace/nil_origin/beyond.rb +182 -0
  233. data/lib/runtime_analysis/trace/nil_origin/index.rb +164 -0
  234. data/lib/runtime_analysis/trace/nil_origin/lifetime.rb +29 -0
  235. data/lib/runtime_analysis/trace/nil_origin/named.rb +48 -0
  236. data/lib/runtime_analysis/trace/nil_origin/objects.rb +78 -0
  237. data/lib/runtime_analysis/trace/nil_origin/provenance.rb +136 -0
  238. data/lib/runtime_analysis/trace/nil_origin/receiver.rb +121 -0
  239. data/lib/runtime_analysis/trace/nil_origin/rows.rb +43 -0
  240. data/lib/runtime_analysis/trace/nil_origin/source.rb +315 -0
  241. data/lib/runtime_analysis/trace/nil_origin/stored.rb +159 -0
  242. data/lib/runtime_analysis/trace/nil_origin/told.rb +152 -0
  243. data/lib/runtime_analysis/trace/nil_origin/values.rb +47 -0
  244. data/lib/runtime_analysis/trace/nil_origin/weighed.rb +70 -0
  245. data/lib/runtime_analysis/trace/nil_origin/writers.rb +79 -0
  246. data/lib/runtime_analysis/trace/nil_origin.rb +278 -0
  247. data/lib/runtime_analysis/trace/novelty.rb +17 -0
  248. data/lib/runtime_analysis/trace/pure/novelty.rb +43 -0
  249. data/lib/runtime_analysis/trace/pure/state.rb +94 -0
  250. data/lib/runtime_analysis/trace/pure.rb +15 -0
  251. data/lib/runtime_analysis/trace/recorder/class_name.rb +63 -0
  252. data/lib/runtime_analysis/trace/recorder/fingerprint.rb +39 -0
  253. data/lib/runtime_analysis/trace/recorder/frame_key.rb +27 -0
  254. data/lib/runtime_analysis/trace/recorder/paths.rb +89 -0
  255. data/lib/runtime_analysis/trace/recorder/raise_site.rb +36 -0
  256. data/lib/runtime_analysis/trace/recorder/reachability.rb +46 -0
  257. data/lib/runtime_analysis/trace/recorder/row_emission.rb +137 -0
  258. data/lib/runtime_analysis/trace/recorder/safe_steps.rb +84 -0
  259. data/lib/runtime_analysis/trace/recorder/tree_steps.rb +42 -0
  260. data/lib/runtime_analysis/trace/recorder/unwinding.rb +23 -0
  261. data/lib/runtime_analysis/trace/recorder.rb +149 -0
  262. data/lib/runtime_analysis/trace/state.rb +16 -0
  263. data/lib/runtime_analysis/trace.rb +142 -0
  264. data/lib/runtime_analysis/version.rb +6 -0
  265. data/lib/runtime_analysis/web.rb +6 -0
  266. data/lib/runtime_analysis.rb +125 -0
  267. metadata +330 -0
@@ -0,0 +1,145 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+ require_relative "origins"
5
+ require_relative "stubs"
6
+ require "runtime_analysis/query/index"
7
+
8
+ module RuntimeAnalysis
9
+ module Query
10
+ module Tools
11
+ class VerifyMock < Base
12
+ tool "verify_mock", "Stubs whose return the real collaborator never produced, from recorded runs."
13
+ argument :spec, "A spec file or directory", positional: true
14
+
15
+ def call(spec: "spec")
16
+ stubs = Stubs.new(static, project).under(spec)
17
+ doc = document("verify_mock #{spec}")
18
+ stubs.each { |resolved| check(doc, resolved) }
19
+ doc.instance_variable_set(:@summary, summary(stubs.size, doc))
20
+ doc
21
+ end
22
+
23
+ private
24
+
25
+ def shapes
26
+ @shapes ||= Index.shapes(graph)
27
+ end
28
+
29
+ def summary(total, doc)
30
+ errors = doc.findings.count { |f| f.severity == :error }
31
+ unverified = doc.findings.count { |f| f.severity == :info }
32
+ "#{plural(total, "stub")} \u00b7 #{plural(errors, "false positive")} \u00b7 #{unverified} unverified"
33
+ end
34
+
35
+ def check(doc, resolved)
36
+ shape = shapes.return_shape(resolved.identity)
37
+ return unverified(doc, resolved) if shape.empty?
38
+
39
+ false_positive(doc, resolved, shape) unless matches?(resolved.stub.returns, shape)
40
+ end
41
+
42
+ def false_positive(doc, resolved, shape)
43
+ doc.finding(severity: :error, message: "stub #{resolved.identity} is a false positive",
44
+ location: location(resolved.stub), detail: detail(resolved, shape), fix: fix(resolved, shape))
45
+ end
46
+
47
+ def unverified(doc, resolved)
48
+ doc.finding(severity: :info, message: "stub #{resolved.identity} is unverified: the real method never ran",
49
+ location: location(resolved.stub))
50
+ end
51
+
52
+ def location(stub)
53
+ Report.location(file: stub.file, line: stub.line)
54
+ end
55
+
56
+ def matches?(value, shape)
57
+ return true if value.nil? || value.unknown?
58
+
59
+ class_seen?(value.klass, shape.classes.keys) && elements_match?(value, shape)
60
+ end
61
+
62
+ def elements_match?(value, shape)
63
+ return true if shape.elements.empty?
64
+
65
+ unseen_elements(value, shape).empty?
66
+ end
67
+
68
+ def unseen_elements(value, shape)
69
+ value.elements.uniq.reject { |klass| klass == "unknown" || class_seen?(klass, shape.elements.classes.keys) }
70
+ end
71
+
72
+ def class_seen?(klass, observed)
73
+ observed.any? { |seen| seen == klass || seen.end_with?("::#{klass}") }
74
+ end
75
+
76
+ def detail(resolved, shape)
77
+ value = resolved.stub.returns
78
+ ["your stub returns #{value.source} #{describe_value(value)}",
79
+ "the real one returns #{describe_shape(shape, holders(resolved))}",
80
+ Origins.phrase(graph.nodes_of(resolved.identity)),
81
+ *mismatch_line(value, shape)]
82
+ end
83
+
84
+ def describe_value(value)
85
+ value.elements.empty? ? value.klass : "#{value.klass}<#{value.elements.join(", ")}>"
86
+ end
87
+
88
+ def describe_shape(shape, holders)
89
+ classes = Origins.classes(shape.classes)
90
+ return "#{classes}#{later_messages(shape)}" unless elements?(shape)
91
+
92
+ "#{collection(classes, shape, holders)} #{receives(shape.elements)}"
93
+ end
94
+
95
+ def collection(classes, shape, holders)
96
+ return "#{article(classes)} whose elements receive" if shape.classes.one?
97
+
98
+ "#{classes}, and the elements of the #{holders.join(" or ")} receive"
99
+ end
100
+
101
+ def later_messages(shape)
102
+ ", which receives #{receives(shape)}" unless shape.messages.empty?
103
+ end
104
+
105
+ def holders(resolved)
106
+ holding = graph.nodes_of(resolved.identity).select { |node| holds_elements?(node) }
107
+ Origins.ordered(holding.map { |node| node.return.klass }.tally).map(&:first)
108
+ end
109
+
110
+ def holds_elements?(node)
111
+ node.returned? && node.return.elements.any?
112
+ end
113
+
114
+ def elements?(shape)
115
+ !shape.elements.messages.empty?
116
+ end
117
+
118
+ def receives(shape)
119
+ shape.message_names.map { |name| "##{name}" }.join(", ")
120
+ end
121
+
122
+ def article(noun)
123
+ "#{noun.start_with?(/[AEIOU]/) ? "an" : "a"} #{noun}"
124
+ end
125
+
126
+ def mismatch_line(value, shape)
127
+ unseen = elements?(shape) ? unseen_elements(value, shape) : []
128
+ return if unseen.empty?
129
+
130
+ "#{unseen.map { |klass| article(klass) }.join(" or ")} was never seen answering any of them"
131
+ end
132
+
133
+ def fix(resolved, shape)
134
+ target = elements?(shape) ? element_target(resolved, shape) : shape.classes.keys.join(" or ")
135
+ "return #{target}, as the real method did"
136
+ end
137
+
138
+ def element_target(resolved, shape)
139
+ collections = holders(resolved).map { |klass| article(klass) }.join(" or ")
140
+ "#{collections} of #{shape.elements.classes.keys.join(" or ")}"
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/query/tools/base"
4
+ require_relative "method_lookup"
5
+
6
+ module RuntimeAnalysis
7
+ module Query
8
+ module Tools
9
+ class WhatCalls < Base
10
+ tool "what_calls", "Every caller of a method from real runs, with where and how often."
11
+ argument :method, "Method as Klass#name or Klass.name", required: true, positional: true
12
+
13
+ DYNAMIC = /\b(public_send|__send__|send|try)\b/
14
+
15
+ def call(method:)
16
+ identity = MethodLookup.resolve(graph, method)
17
+ callers = callers_of(identity)
18
+ document(identity, summary(callers)).table(columns, callers.map { |edge| row(edge) })
19
+ end
20
+
21
+ private
22
+
23
+ def callers_of(identity)
24
+ caller_edges(identity).sort_by { |edge| -edge.count }
25
+ end
26
+
27
+ def summary(callers)
28
+ "#{plural(callers.size, "caller")} in #{runs_phrase(run_count(callers))}"
29
+ end
30
+
31
+ def columns
32
+ [{ name: "caller" }, { name: "site" }, { name: "calls" }, { name: "note" }]
33
+ end
34
+
35
+ def row(edge)
36
+ node = edge.nodes.first
37
+ [edge.caller, site(node), edge.count, note(edge, node)]
38
+ end
39
+
40
+ def note(edge, node)
41
+ notes = []
42
+ notes << "via #{dispatch(node)}" if dispatch(node)
43
+ notes << spec_note(edge) if edge.caller.end_with?("_spec.rb")
44
+ notes.join(" \u00b7 ")
45
+ end
46
+
47
+ def spec_note(edge)
48
+ own = File.basename(edge.caller) == "#{underscore(graph.method(edge.callee).klass)}_spec.rb"
49
+ own ? "its own spec" : "spec"
50
+ end
51
+
52
+ def underscore(klass)
53
+ klass.split("::").last.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
54
+ end
55
+
56
+ def dispatch(node)
57
+ line = project.source_line(node.call.call_file, node.call.call_line)
58
+ line && line[DYNAMIC]
59
+ end
60
+
61
+ def run_count(edges)
62
+ edges.flat_map(&:nodes).map { |node| node.tree.pid }.uniq.size
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/query/tools/base"
4
+ require_relative "method_lookup"
5
+ require "runtime_analysis/query/tools/origins"
6
+ require "runtime_analysis/query/index"
7
+
8
+ module RuntimeAnalysis
9
+ module Query
10
+ module Tools
11
+ class WhatItReceives < Base
12
+ tool "what_it_receives", "The observed shape of each argument at a method's call sites."
13
+ argument :method, "Method as Klass#name or Klass.name", required: true, positional: true
14
+ argument :by_caller, "Break the classes down per calling method", type: :boolean
15
+
16
+ Parameter = Struct.new(:label, :shape, :given)
17
+ SITE_COLUMNS = [{ name: "class" }, { name: "call site" }, { name: "calls" }].freeze
18
+ CALLER_COLUMNS = [{ name: "caller" }, { name: "argument" }, { name: "classes" }, { name: "calls" }].freeze
19
+
20
+ def call(method:, by_caller: false)
21
+ identity = MethodLookup.resolve(graph, method)
22
+ nodes = graph.nodes_of(identity)
23
+ taken = parameters(identity, nodes)
24
+ doc = taken.reduce(document(identity, summary(nodes))) { |said, parameter| describe(said, parameter, nodes) }
25
+ by_caller ? callers(doc, nodes, taken) : doc
26
+ end
27
+
28
+ private
29
+
30
+ def shapes
31
+ @shapes ||= Index.shapes(graph)
32
+ end
33
+
34
+ def summary(nodes)
35
+ sites = nodes.map { |node| site(node) }.uniq.size
36
+ "#{plural(nodes.size, "call")} \u00b7 #{plural(sites, "call site")}"
37
+ end
38
+
39
+ def parameters(identity, nodes)
40
+ names = parameter_names(identity)
41
+ positions(nodes).map { |position| positional(identity, position, names.at(position)) } +
42
+ kwarg_names(nodes).map { |name| keyword(identity, name) }
43
+ end
44
+
45
+ def positions(nodes)
46
+ nodes.map { |node| node.call.args.size }.max.times
47
+ end
48
+
49
+ def kwarg_names(nodes)
50
+ nodes.flat_map { |node| kwargs_of(node).map(&:name) }.uniq
51
+ end
52
+
53
+ def kwargs_of(node)
54
+ node.kwargs ? node.kwargs.kwargs : []
55
+ end
56
+
57
+ def positional(identity, position, name)
58
+ Parameter.new(arg_label(position, name), shapes.argument_shape(identity, position),
59
+ ->(node) { node.call.args.at(position)&.klass })
60
+ end
61
+
62
+ def keyword(identity, name)
63
+ Parameter.new("#{name}:", shapes.kwarg_shape(identity, name),
64
+ ->(node) { kwargs_of(node).find { |kwarg| kwarg.name == name }&.klass })
65
+ end
66
+
67
+ def arg_label(position, name)
68
+ name ? "#{name} (arg #{position + 1})" : "arg #{position + 1}"
69
+ end
70
+
71
+ def describe(doc, parameter, nodes)
72
+ doc.text(parameter.label).table([{ name: "k" }, { name: "v" }], details(parameter.shape))
73
+ sites(doc, parameter, nodes).text(" \u00b7 #{Origins.phrase(nodes)}")
74
+ end
75
+
76
+ def sites(doc, parameter, nodes)
77
+ parameter.shape.classes.size > 1 ? doc.table(SITE_COLUMNS, sites_by_class(nodes, parameter.given)) : doc
78
+ end
79
+
80
+ def sites_by_class(nodes, given)
81
+ sent = nodes.filter_map { |node| [given.call(node), site(node)] if given.call(node) }
82
+ rarest_first(sent.tally, sent.map(&:first).tally).map { |(klass, at), calls| [" #{klass}", at, calls] }
83
+ end
84
+
85
+ def rarest_first(sites, totals)
86
+ sites.sort_by.with_index { |((klass, _), calls), met| [totals.fetch(klass), klass, -calls, met] }
87
+ end
88
+
89
+ def details(shape)
90
+ rows = [[" classes", Origins.classes(shape.classes)], [" receives", received(shape)]]
91
+ never = never_sent(shape)
92
+ rows << [" never", never] if never
93
+ rows
94
+ end
95
+
96
+ NEVER_SHOWN = 6
97
+
98
+ def never_sent(shape)
99
+ classes = app_classes(shape)
100
+ return nil unless classes.size == 1
101
+
102
+ unsent = unsent_methods(classes.first, shape.messages.keys)
103
+ abbreviate(unsent) unless unsent.empty?
104
+ end
105
+
106
+ def app_classes(shape)
107
+ shape.classes.keys.reject { |klass| klass.start_with?("RSpec::") }
108
+ end
109
+
110
+ def unsent_methods(klass, sent)
111
+ static.public_methods_of(klass).reject(&:class_method).map(&:name) - sent - ["initialize"]
112
+ end
113
+
114
+ def abbreviate(names)
115
+ shown = names.first(NEVER_SHOWN).map { |name| "##{name}" }
116
+ shown << "\u2026 #{names.size - NEVER_SHOWN} more" if names.size > NEVER_SHOWN
117
+ shown.join(" ")
118
+ end
119
+
120
+ def received(shape)
121
+ shape.messages.empty? ? "(no app messages)" : Origins.messages(shape.messages)
122
+ end
123
+
124
+ def callers(doc, nodes, taken)
125
+ rows = busiest_first(nodes).flat_map { |caller, made| caller_rows(caller, made, taken) }
126
+ doc.text("by caller").table(CALLER_COLUMNS, rows)
127
+ end
128
+
129
+ def busiest_first(nodes)
130
+ nodes.group_by { |node| node.container.identity }.sort_by.with_index { |(_, made), met| [-made.size, met] }
131
+ end
132
+
133
+ def caller_rows(caller, made, taken)
134
+ taken.filter_map do |parameter|
135
+ classes = made.filter_map(&parameter.given).tally
136
+ [caller, parameter.label, Origins.classes(classes), classes.values.sum] unless classes.empty?
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/query/tools/base"
4
+ require_relative "method_lookup"
5
+ require "runtime_analysis/query/tools/origins"
6
+
7
+ module RuntimeAnalysis
8
+ module Query
9
+ module Tools
10
+ class WhereNilComesFrom < Base
11
+ tool "where_nil_comes_from",
12
+ "Methods that returned nil and arguments that received it, with the errors that followed."
13
+ argument :method, "Method as Klass#name or Klass.name; omit for the whole app", positional: true
14
+
15
+ def call(method: nil)
16
+ return whole_app if method.nil?
17
+
18
+ identity = MethodLookup.resolve(graph, method)
19
+ one_method(document(identity, "nil returns and nil arguments"), identity)
20
+ end
21
+
22
+ private
23
+
24
+ def whole_app
25
+ doc = document("nil across the app", "methods that returned nil, most often first")
26
+ doc.table(columns, app_rows.sort_by { |_, returned, received| [-returned, -received] })
27
+ end
28
+
29
+ def app_rows
30
+ rows = graph.methods.map { |method| app_row(method.identity) }
31
+ rows.reject { |_, returned, received| returned.zero? && received.zero? }
32
+ end
33
+
34
+ def app_row(identity)
35
+ [identity, nil_returns(identity).size, nil_args(identity).size]
36
+ end
37
+
38
+ def columns
39
+ [{ name: "method" }, { name: "nil returns" }, { name: "nil arguments" }]
40
+ end
41
+
42
+ def one_method(doc, identity)
43
+ nils = nil_returns(identity)
44
+ nil_summary(doc, identity, nils) if nils.any?
45
+ nil_arguments(doc, identity)
46
+ doc.text("no nil returned or received") if nils.empty? && nil_args(identity).empty?
47
+ doc
48
+ end
49
+
50
+ def nil_returns(identity)
51
+ graph.nodes_of(identity).select { |node| node.return_klass == "NilClass" }
52
+ end
53
+
54
+ def nil_args(identity)
55
+ graph.nodes_of(identity).select { |node| node.call.args.any? { |arg| arg.klass == "NilClass" } }
56
+ end
57
+
58
+ def nil_summary(doc, identity, nils)
59
+ doc.text("#{identity} returned nil")
60
+ doc.text(" #{share(nils, graph.nodes_of(identity))}")
61
+ unwinds(doc, nils)
62
+ others(doc, graph.nodes_of(identity))
63
+ end
64
+
65
+ def share(nils, all)
66
+ spec, run, unknown = Origins.split(nils)
67
+ all_spec, all_run, all_unknown = Origins.split(all)
68
+ placed = "#{run} of #{all_run} app runs \u00b7 #{spec} of #{all_spec} spec calls"
69
+ all_unknown.zero? ? placed : "#{placed} \u00b7 #{unknown} of #{Origins.unplaced(all_unknown, "call")}"
70
+ end
71
+
72
+ def unwinds(doc, nils)
73
+ raised = nils.select { |node| node.parent&.raised? }
74
+ raised.group_by { |node| unwind_key(node) }.each do |key, group|
75
+ doc.text(unwind_line(key, group.size, nils.size))
76
+ end
77
+ end
78
+
79
+ def unwind_line((klass, caller, at), count, total)
80
+ " #{count} of #{total} unwound with #{klass} in #{caller} (#{at})"
81
+ end
82
+
83
+ def unwind_key(node)
84
+ [node.parent.unwind.klass, node.parent.method_identity, site(node)]
85
+ end
86
+
87
+ def others(doc, all)
88
+ classes = all.select(&:returned?).map(&:return_klass).tally.except("NilClass")
89
+ doc.text(" otherwise returned #{Origins.classes(classes)}") if classes.any?
90
+ end
91
+
92
+ def nil_arguments(doc, identity)
93
+ nil_args(identity).group_by { |node| positions_with_nil(node) }.each do |positions, nodes|
94
+ labels = positions.map { |position| "arg #{position + 1}" }.join(", ")
95
+ doc.text("#{identity} received nil at #{labels} in #{plural(nodes.size, "call")}")
96
+ end
97
+ end
98
+
99
+ def positions_with_nil(node)
100
+ node.call.args.each_index.select { |index| node.call.args.fetch(index).klass == "NilClass" }
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/query/tools/base"
4
+ require_relative "method_lookup"
5
+ require "runtime_analysis/query/tools/origins"
6
+
7
+ module RuntimeAnalysis
8
+ module Query
9
+ module Tools
10
+ class WhoActuallyAnswered < Base
11
+ tool "who_actually_answered", "Which classes received the message at a call site, from real runs."
12
+ argument :target, "A call site as file:line, or a method to list its call sites",
13
+ required: true, positional: true
14
+
15
+ SITE = /\A(.+):(\d+)\z/
16
+
17
+ def call(target:)
18
+ match = SITE.match(target)
19
+ match ? at_site(match[1], match[2].to_i) : sites_of(MethodLookup.resolve(graph, target))
20
+ end
21
+
22
+ private
23
+
24
+ def at_site(file, line)
25
+ doc = document(project.source_line(file, line) || "#{file}:#{line}", "#{file}:#{line}")
26
+ nodes = nodes_at(file, line)
27
+ doc.table(columns, answer_rows(nodes), header: true)
28
+ never(unplaced(doc, nodes), nodes)
29
+ end
30
+
31
+ def unplaced(doc, nodes)
32
+ unknown = Origins.split(nodes).last
33
+ unknown.zero? ? doc : doc.text("#{Origins.unplaced(unknown, "call")}, counted in neither column")
34
+ end
35
+
36
+ def never(doc, nodes)
37
+ never = never_answered(nodes)
38
+ never.empty? ? doc : doc.text("never answered here #{never.join(", ")}")
39
+ end
40
+
41
+ def never_answered(nodes)
42
+ return [] if nodes.empty?
43
+
44
+ static.implementers_of(nodes.first.callee.name).sort - answered(nodes)
45
+ end
46
+
47
+ def answered(nodes)
48
+ nodes.map { |node| node.callee.klass }
49
+ end
50
+
51
+ def columns
52
+ [{ name: "answered by" }, { name: "app runs" }, { name: "spec calls" }]
53
+ end
54
+
55
+ def nodes_at(file, line)
56
+ graph.trees.flat_map(&:nodes).select do |node|
57
+ relative(node.call.call_file) == file && node.call.call_line == line
58
+ end
59
+ end
60
+
61
+ def answer_rows(nodes)
62
+ rows = nodes.group_by(&:method_identity).map { |identity, group| answer_row(identity, group) }
63
+ rows.sort_by.with_index { |(_, run, spec), recorded| [-run, -spec, recorded] }
64
+ end
65
+
66
+ def answer_row(identity, group)
67
+ spec, run = Origins.split(group)
68
+ [identity, run, spec]
69
+ end
70
+
71
+ def sites_of(identity)
72
+ doc = document(identity, "call sites; ask again with one of them")
73
+ doc.table([{ name: "site" }, { name: "calls" }], site_rows(identity).sort_by { |_, calls| -calls })
74
+ end
75
+
76
+ def site_rows(identity)
77
+ graph.nodes_of(identity).group_by { |node| site(node) }.map { |at, group| [at, group.size] }
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Query
5
+ module Tools
6
+ Argument = Struct.new(:name, :type, :required, :description, :positional, keyword_init: true)
7
+
8
+ @registry = {}
9
+
10
+ class << self
11
+ attr_reader :registry
12
+
13
+ def register(tool)
14
+ registry[tool.tool_name] = tool
15
+ end
16
+
17
+ def all
18
+ registry.values
19
+ end
20
+
21
+ def find(name)
22
+ registry.fetch(name) { raise ArgumentError, "unknown tool #{name.inspect}" }
23
+ end
24
+
25
+ def define(&declaration)
26
+ Class.new(Base, &declaration)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ require_relative "tools/base"
34
+ Dir[File.join(__dir__, "tools", "*.rb")].grep_v(/_spec\.rb\z/).sort.each { |file| require file }
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: without_the_recorder_spec.rb asserts each entry, loaded with no recorder
4
+
5
+ require_relative "version"
6
+ require_relative "query/index"
7
+ require_relative "query/report"
8
+ require_relative "query/project"
9
+ require_relative "recording"
10
+ require_relative "query/tools"
11
+ require_relative "query/surfaces"
12
+
13
+ module RuntimeAnalysis
14
+ module Query
15
+ def self.load(dir) = Index.load(dir)
16
+
17
+ def self.segments(dir) = Index.segments(dir)
18
+
19
+ def self.recording_format
20
+ Recording.format_header.last
21
+ end
22
+
23
+ def self.cli(argv)
24
+ require "runtime_analysis/query/surfaces/cli"
25
+ Surfaces.cli(argv)
26
+ end
27
+
28
+ def self.setup(argv, out)
29
+ require "runtime_analysis/query/surfaces/setup"
30
+ Surfaces.setup(argv, out)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest"
4
+
5
+ module RuntimeAnalysis
6
+ module Recording
7
+ module Blob
8
+ module_function
9
+
10
+ def id(content) = Digest::SHA1.hexdigest("blob #{content.bytesize}\0#{content}")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "opcodes"
4
+
5
+ module RuntimeAnalysis
6
+ module Recording
7
+ module Format
8
+ OPCODE = Opcodes.fetch(:format)
9
+
10
+ VERSION = 1
11
+
12
+ MEANING = 3
13
+
14
+ class Newer < StandardError; end
15
+
16
+ module_function
17
+
18
+ def opcode = OPCODE
19
+ def version = VERSION
20
+ def meaning = MEANING
21
+ def header = [OPCODE, VERSION]
22
+ def newer = Newer
23
+
24
+ def check(version)
25
+ return if version <= VERSION
26
+
27
+ raise Newer, "this recording is in format #{version}, and this runtime_analysis reads up to format " \
28
+ "#{VERSION}. It was recorded by a newer version of the gem: upgrade the one reading it."
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through recording_spec.rb, which reads every number by Recording.opcode, and ruby_sink_spec.rb.
4
+
5
+ module RuntimeAnalysis
6
+ module Recording
7
+ module Opcodes
8
+ DICT = 0
9
+ CALL_BEFORE_PATHS = 1
10
+ RETURN = 2
11
+ KWARGS = 3
12
+ UNWIND = 4
13
+ ENTRY = 5
14
+ PATH = 6
15
+ CALL = 7
16
+ ANSWERED = 8
17
+ FORMAT = 9
18
+ HELD = 10
19
+ INNER_ENTRY = 11
20
+ VALUE = 12
21
+ IVARS = 13
22
+
23
+ NAMED = { dict: DICT, call_before_paths: CALL_BEFORE_PATHS, return: RETURN, kwargs: KWARGS, unwind: UNWIND,
24
+ entry: ENTRY, path: PATH, call: CALL, answered: ANSWERED, format: FORMAT, held: HELD,
25
+ inner_entry: INNER_ENTRY, value: VALUE, ivars: IVARS }.freeze
26
+
27
+ def self.fetch(name) = NAMED.fetch(name)
28
+ end
29
+ end
30
+ end