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,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "graph"
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class GraphCache
9
+ Entry = Struct.new(:stamp, :graph)
10
+
11
+ def initialize
12
+ @entries = {}
13
+ end
14
+
15
+ def load(dir)
16
+ path = File.expand_path(dir)
17
+ stamp = stamp(path)
18
+ entry = @entries[path]
19
+ entry = @entries[path] = Entry.new(stamp, Graph.load(path)) unless entry && entry.stamp == stamp
20
+ entry.graph
21
+ end
22
+
23
+ private
24
+
25
+ def stamp(dir)
26
+ files = Dir.children(dir).map { |name| File.join(dir, name) }.select { |path| File.file?(path) }
27
+ files.to_h { |path| [path, [File.size(path), File.mtime(path)]] }
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "runtime_analysis/errors"
5
+
6
+ module RuntimeAnalysis
7
+ module Query
8
+ module Index
9
+ class Reading
10
+ class Declared
11
+ Route = Struct.new(:verb, :path, :action)
12
+ FILES = "declared.*.json"
13
+ PID = /\.(\d+)\.json\z/
14
+
15
+ attr_reader :routes, :jobs, :mailers, :channels, :hooks
16
+
17
+ def self.newest(dir, runs)
18
+ said = Dir.glob(File.join(dir, FILES)).filter_map { |file| read(file, runs) }
19
+ said.max_by(&:first)&.last
20
+ end
21
+
22
+ def self.read(file, runs)
23
+ said = JSON.parse(File.read(file))
24
+ [started(file, runs), new(said)] if said.instance_of?(Hash)
25
+ rescue SystemCallError, Errors.unreadable_json
26
+ nil
27
+ end
28
+
29
+ def self.started(file, runs)
30
+ runs[file[PID, 1].to_i]&.started_at.to_s
31
+ end
32
+
33
+ def initialize(said)
34
+ @routes = said["routes"]&.map { |route| Route.new(*route) }
35
+ @jobs, @mailers, @channels = said.values_at("jobs", "mailers", "channels")
36
+ @hooks = said["hooks"].to_h
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Query
5
+ module Index
6
+ class Reading
7
+ class Dict
8
+ MISSING = ""
9
+
10
+ def initialize(pid:)
11
+ @pid = pid
12
+ @strings = {}
13
+ @paths = {}
14
+ end
15
+
16
+ def define_path(id, parent_id, callee, call_file, call_line)
17
+ @paths[id] = Path.new(id, @paths[parent_id], callee, call_file, call_line)
18
+ end
19
+
20
+ def path(id)
21
+ @paths.fetch(id, UNKNOWN_PATH)
22
+ end
23
+
24
+ def tree(number_or_whole_name)
25
+ number_or_whole_name.instance_of?(Integer) ? "#{@pid}-#{number_or_whole_name}" : number_or_whole_name
26
+ end
27
+
28
+ def define(id, string)
29
+ @strings[id] = string
30
+ end
31
+
32
+ def resolve(id)
33
+ @strings.fetch(id, MISSING)
34
+ end
35
+
36
+ def resolve_line(value)
37
+ value if value && value >= 0
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through rows_spec.rb and path_spec.rb, which read frames off decoded rows.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Reading
9
+ Frame = Struct.new(:klass, :class_method, :name, :file, :line)
10
+
11
+ class Frame
12
+ def identity
13
+ "#{klass}#{class_method ? "." : "#"}#{name}"
14
+ end
15
+
16
+ def main?
17
+ klass == MAIN
18
+ end
19
+
20
+ def location
21
+ line ? "#{file}:#{line}" : file
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through tree_builder_spec.rb, whose builder links every node it makes.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Reading
9
+ class Node
10
+ attr_reader :call, :parent, :children, :depth, :values
11
+ attr_accessor :return, :unwind, :kwargs, :last_step, :tree, :answered_for
12
+
13
+ def initialize(call, parent)
14
+ @call = call
15
+ @children = []
16
+ @last_step = step
17
+ attach(parent)
18
+ end
19
+
20
+ def attach(parent)
21
+ @parent = parent
22
+ @depth = parent ? parent.depth + 1 : 0
23
+ parent&.children&.push(self)
24
+ end
25
+
26
+ def value(role, key, print)
27
+ @values ||= Values.new
28
+ values.set(role, key, print)
29
+ end
30
+
31
+ def step = call.step
32
+ def callee = call.callee
33
+ def container = call.container
34
+ def method_identity = callee.identity
35
+ def raised? = !unwind.nil?
36
+ def returned? = !self.return.nil?
37
+
38
+ def return_klass
39
+ self.return&.klass
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Query
5
+ module Index
6
+ class Reading
7
+ Path = Struct.new(:id, :parent, :callee, :call_file, :call_line)
8
+
9
+ class Path
10
+ def lineage
11
+ @lineage ||= parent ? [*parent.lineage, parent].freeze : [].freeze
12
+ end
13
+
14
+ def container(call_file, call_line)
15
+ parent ? parent.callee : Frame.new(MAIN, false, MAIN, call_file, call_line)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Query
5
+ module Index
6
+ class Reading
7
+ class Reruns
8
+ def initialize(trees)
9
+ @runs = trees.select { |tree| replaceable?(tree) }.group_by(&:entry_point)
10
+ .transform_values { |recorded| recorded.map(&:run) }
11
+ end
12
+
13
+ def superseded?(tree)
14
+ replaceable?(tree) && @runs.fetch(tree.entry_point).any? { |run| stands_in?(run, tree.run) }
15
+ end
16
+
17
+ private
18
+
19
+ def replaceable?(tree)
20
+ tree.spec? && !tree.run&.started_at.to_s.empty?
21
+ end
22
+
23
+ def stands_in?(later, run)
24
+ later.started_at > run.started_at && later.tier.to_i >= run.tier.to_i
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/recording"
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Index
8
+ class Reading
9
+ module Rows
10
+ DICT, RECORD, RETURN, KWARGS, UNWIND, ENTRY, PATH, CALL, ANSWERED, FORMAT, INNER_ENTRY, VALUE =
11
+ %i[dict call_before_paths return kwargs unwind entry path call answered format inner_entry value]
12
+ .map { |name| Recording.opcode(name) }
13
+ RSPEC_LOCATION_PREFIX = "./"
14
+
15
+ DECODERS = { DICT => :decode_dict, RECORD => :decode_call, RETURN => :decode_return,
16
+ KWARGS => :decode_kwargs, UNWIND => :decode_unwind, ENTRY => :decode_entry,
17
+ PATH => :decode_path, CALL => :decode_path_call, ANSWERED => :decode_answered,
18
+ FORMAT => :decode_format, INNER_ENTRY => :decode_inner_entry,
19
+ VALUE => :decode_value }.freeze
20
+ KWARG = 2
21
+ BY_VALUE = 1
22
+
23
+ module_function
24
+
25
+ def decode(row, dict)
26
+ decoder = DECODERS[row.first]
27
+ public_send(decoder, row, dict) if decoder
28
+ end
29
+
30
+ def decode_format((_, version), _dict) = Recording.check_format(version)
31
+
32
+ def decode_dict((_, id, string), dict)
33
+ dict.define(id, string)
34
+ nil
35
+ end
36
+
37
+ def decode_call((_, tree, step, file, line, *rest), dict)
38
+ Call.new(uuid: dict.tree(tree), step: step, call_file: dict.resolve(file),
39
+ call_line: dict.resolve_line(line), container: frame(rest.shift(5), dict),
40
+ callee: frame(rest.shift(5), dict), **tail(rest, dict))
41
+ end
42
+
43
+ def decode_path((_, id, parent, *rest), dict)
44
+ callee = frame(rest.shift(5), dict)
45
+ dict.define_path(id, parent, callee, *site(rest, dict))
46
+ nil
47
+ end
48
+
49
+ def decode_path_call((_, tree, step, path, *rest), dict)
50
+ path_call(dict.tree(tree), step, dict.path(path), site(rest.shift(2), dict), tail(rest, dict))
51
+ end
52
+
53
+ def site((file, line), dict)
54
+ [dict.resolve(file), dict.resolve_line(line)]
55
+ end
56
+
57
+ def path_call(uuid, step, path, (file, line), tail)
58
+ Call.new(uuid: uuid, step: step, call_file: file, call_line: line, path: path,
59
+ container: path.container(file, line), callee: path.callee, **tail)
60
+ end
61
+
62
+ def decode_unwind((_, tree, step, klass, file, line, missing, receiver), dict)
63
+ Unwind.new(uuid: dict.tree(tree), step: step, klass: dict.resolve(klass), file: named(file, dict),
64
+ line: (line if line&.positive?), missing: named(missing, dict), receiver: named(receiver, dict))
65
+ end
66
+
67
+ def named(id, dict)
68
+ dict.resolve(id) if id && id >= 0
69
+ end
70
+
71
+ def decode_entry((_, tree, identity), dict)
72
+ Entry.new(uuid: dict.tree(tree), identity: dict.resolve(identity).delete_prefix(RSPEC_LOCATION_PREFIX))
73
+ end
74
+
75
+ def decode_inner_entry((_, tree, identity), dict)
76
+ InnerEntry.new(uuid: dict.tree(tree), identity: dict.resolve(identity))
77
+ end
78
+
79
+ def decode_value((_, tree, step, role, key, how, print), dict)
80
+ return unless print
81
+
82
+ Value.new(uuid: dict.tree(tree), step: step, role: role, key: role == KWARG ? dict.resolve(key) : key,
83
+ print: [how == BY_VALUE, print])
84
+ end
85
+
86
+ def decode_answered((_, tree, step, name), dict)
87
+ Answered.new(uuid: dict.tree(tree), step: step, name: dict.resolve(name))
88
+ end
89
+
90
+ def frame((klass, class_method, name, file, line), dict)
91
+ Frame.new(dict.resolve(klass), class_method, dict.resolve(name), dict.resolve(file),
92
+ dict.resolve_line(line))
93
+ end
94
+
95
+ def tail((tier, argc, *rest), dict)
96
+ case tier
97
+ when 2 then { tier: 2, argc: argc, args: classes(rest, dict) }
98
+ when 3 then { tier: 3, argc: argc, receiver_oid: rest.shift, args: pairs(rest, dict) }
99
+ else { tier: 1, args: [] }
100
+ end
101
+ end
102
+
103
+ def classes(ids, dict)
104
+ ids.map { |id| Arg.new(dict.resolve(id)) }
105
+ end
106
+
107
+ def pairs(fields, dict)
108
+ fields.each_slice(2).map { |klass, oid| Arg.new(dict.resolve(klass), oid) }
109
+ end
110
+
111
+ def decode_return((_, tree, step, klass, oid, _count, *elements), dict)
112
+ Return.new(uuid: dict.tree(tree), step: step, klass: dict.resolve(klass), oid: oid,
113
+ elements: pairs(elements, dict))
114
+ end
115
+
116
+ def decode_kwargs((_, tree, step, tier, count, *fields), dict)
117
+ Kwargs.new(uuid: dict.tree(tree), step: step, tier: tier, kw_argc: count,
118
+ kwargs: kwarg_list(fields, tier >= 3 ? 3 : 2, dict))
119
+ end
120
+
121
+ def kwarg_list(fields, width, dict)
122
+ fields.each_slice(width).map { |name, klass, oid| Kwarg.new(dict.resolve(name), dict.resolve(klass), oid) }
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "runtime_analysis/errors"
5
+ require_relative "dict"
6
+ require_relative "rows"
7
+
8
+ module RuntimeAnalysis
9
+ module Query
10
+ module Index
11
+ class Reading
12
+ class Stream
13
+ SEGMENT = /\Aquery_parameters\.json\.(\d+)\.(\d+)\.(\d+)\z/
14
+ FALLBACK = "query_parameters.json"
15
+ HELD = "[#{Recording.opcode(:held)},".freeze
16
+
17
+ attr_reader :pid, :tid, :paths
18
+
19
+ Segment = Struct.new(:pid, :tid, :seq, :path)
20
+
21
+ def self.discover(dir)
22
+ fallback_streams(dir) + native_streams(dir).sort_by { |stream| [stream.pid, stream.tid] }
23
+ end
24
+
25
+ def self.native_streams(dir)
26
+ segments = Dir.children(dir).filter_map { |name| segment(dir, name) }
27
+ segments.group_by { |seg| [seg.pid, seg.tid] }.map { |(pid, tid), segs| from_segments(pid, tid, segs) }
28
+ end
29
+
30
+ def self.fallback_streams(dir)
31
+ path = File.join(dir, FALLBACK)
32
+ File.exist?(path) ? [new(pid: nil, tid: nil, paths: [path])] : []
33
+ end
34
+
35
+ def self.segment(dir, name)
36
+ match = SEGMENT.match(name)
37
+ Segment.new(*match.captures.map(&:to_i), File.join(dir, name)) if match
38
+ end
39
+
40
+ def self.held?(path)
41
+ File.foreach(path).any? { |row| row.start_with?(HELD) }
42
+ end
43
+
44
+ def self.from_segments(pid, tid, segments)
45
+ new(pid: pid, tid: tid, paths: segments.sort_by(&:seq).map(&:path))
46
+ end
47
+
48
+ def initialize(pid:, tid:, paths:)
49
+ @pid = pid
50
+ @tid = tid
51
+ @paths = paths
52
+ end
53
+
54
+ def each_row(&)
55
+ dict = Dict.new(pid: pid)
56
+ paths.each { |path| each_line(path) { |raw| yield_decoded(raw, dict, &) } }
57
+ end
58
+
59
+ private
60
+
61
+ def each_line(path)
62
+ File.foreach(path) do |line|
63
+ row = parse(line)
64
+ yield row if row
65
+ end
66
+ end
67
+
68
+ def parse(line)
69
+ JSON.parse(line)
70
+ rescue Errors.unreadable_json
71
+ nil
72
+ end
73
+
74
+ def yield_decoded(raw, dict)
75
+ decoded = Rows.decode(raw, dict)
76
+ yield decoded if decoded
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Query
5
+ module Index
6
+ class Reading
7
+ class Tree
8
+ SPEC_ENTRY = %r{\A(\./)?(spec|test)/|_(spec|test)\.rb:\d+\z}
9
+
10
+ attr_reader :uuid, :nodes, :roots, :inner_entries
11
+ attr_accessor :entry_point, :run
12
+
13
+ def initialize(uuid)
14
+ @uuid = uuid
15
+ @nodes = []
16
+ @roots = []
17
+ @by_step = {}
18
+ @inner_entries = []
19
+ end
20
+
21
+ def inner_entry(identity)
22
+ inner_entries << identity unless inner_entries.include?(identity)
23
+ end
24
+
25
+ def mentions?(text)
26
+ [entry_point.to_s, *inner_entries, *roots.map(&:method_identity)].any? { |named| named.include?(text) }
27
+ end
28
+
29
+ def pid
30
+ uuid.split("-").first.to_i
31
+ end
32
+
33
+ def spec?
34
+ SPEC_ENTRY.match?(entry_point)
35
+ end
36
+
37
+ def run?
38
+ !entry_point.nil? && !spec?
39
+ end
40
+
41
+ def unknown?
42
+ entry_point.nil?
43
+ end
44
+
45
+ def by_step(step)
46
+ @by_step[step]
47
+ end
48
+
49
+ def add(node)
50
+ nodes << node
51
+ @by_step[node.step] = node
52
+ roots << node if node.parent.nil?
53
+ end
54
+
55
+ def depth
56
+ nodes.map(&:depth).max.to_i + 1
57
+ end
58
+
59
+ def classes
60
+ nodes.map { |node| node.callee.klass }.uniq
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,135 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "node"
4
+ require_relative "tree"
5
+ require_relative "values"
6
+
7
+ module RuntimeAnalysis
8
+ module Query
9
+ module Index
10
+ class Reading
11
+ class TreeBuilder
12
+ def initialize
13
+ @trees = {}
14
+ @stacks = {}
15
+ end
16
+
17
+ HANDLERS = { Call => :add_call, Return => :add_return, Unwind => :add_unwind, Kwargs => :add_kwargs,
18
+ Entry => :add_entry, InnerEntry => :add_inner_entry, Value => :add_value,
19
+ Answered => :add_answered }.freeze
20
+
21
+ def trees
22
+ @trees.each_value { |tree| finish(tree) }
23
+ @trees.values
24
+ end
25
+
26
+ def <<(row)
27
+ __send__(HANDLERS.fetch(row.class), row)
28
+ self
29
+ end
30
+
31
+ private
32
+
33
+ def tree_for(uuid)
34
+ @trees[uuid] ||= Tree.new(uuid)
35
+ end
36
+
37
+ def stack_for(uuid)
38
+ @stacks[uuid] ||= []
39
+ end
40
+
41
+ def node_for(row)
42
+ @trees[row.uuid]&.by_step(row.step)
43
+ end
44
+
45
+ def add_call(row)
46
+ push(Node.new(row, row.path ? open_path(row) : open_frame(row)))
47
+ end
48
+
49
+ def push(node)
50
+ tree_for(node.call.uuid).add(node)
51
+ stack_for(node.call.uuid).push(node)
52
+ end
53
+
54
+ def open_path(row)
55
+ stack = stack_for(row.uuid)
56
+ lineage = row.path.lineage
57
+ stack.slice!(shared_depth(stack, lineage)..)
58
+ infer(row, lineage.drop(stack.size))
59
+ stack.last
60
+ end
61
+
62
+ def shared_depth(stack, lineage)
63
+ stack.zip(lineage).take_while { |open, path| path && open.call.path.id == path.id }.size
64
+ end
65
+
66
+ def infer(row, missing)
67
+ missing.each_with_index { |path, index| add_inferred(row, path, index.succ.fdiv(missing.size.succ)) }
68
+ end
69
+
70
+ def add_inferred(row, path, part_of_gap)
71
+ call = inferred_call(row.uuid, row.step - 1 + part_of_gap, path)
72
+ push(Node.new(call, stack_for(row.uuid).last))
73
+ end
74
+
75
+ def inferred_call(uuid, step, path)
76
+ Reading.row(:call, uuid: uuid, step: step, call_file: path.call_file, call_line: path.call_line, path: path,
77
+ container: path.container(path.call_file, path.call_line), callee: path.callee,
78
+ tier: 1, args: [], inferred: true)
79
+ end
80
+
81
+ def open_frame(row)
82
+ stack = stack_for(row.uuid)
83
+ stack.pop(above(stack) { |open| open.callee.eql?(row.container) })
84
+ stack.last
85
+ end
86
+
87
+ def above(stack)
88
+ stack.reverse_each.take_while { |open| !yield(open) }.size
89
+ end
90
+
91
+ def add_return(row)
92
+ close(row) { |node| node.return = row }
93
+ end
94
+
95
+ def add_unwind(row)
96
+ close(row) { |node| node.unwind = row }
97
+ end
98
+
99
+ def add_kwargs(row)
100
+ node_for(row)&.kwargs = row
101
+ end
102
+
103
+ def add_answered(row)
104
+ node_for(row)&.answered_for = row.name
105
+ end
106
+
107
+ def add_entry(row)
108
+ tree_for(row.uuid).entry_point = row.identity
109
+ end
110
+
111
+ def add_inner_entry(row)
112
+ tree_for(row.uuid).inner_entry(row.identity)
113
+ end
114
+
115
+ def add_value(row)
116
+ node_for(row)&.value(row.role, row.key, row.print)
117
+ end
118
+
119
+ def close(row)
120
+ node = node_for(row)
121
+ return unless node
122
+
123
+ yield node
124
+ stack = stack_for(row.uuid)
125
+ stack.pop(above(stack) { |open| open.equal?(node) }.succ)
126
+ end
127
+
128
+ def finish(tree)
129
+ tree.nodes.reverse_each { |node| node.parent&.last_step = [node.parent.last_step, node.last_step].max }
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end