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,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../record_spec.rb, which asks it for each kind of project.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Surfaces
8
+ class Record
9
+ class Question
10
+ ASK = "ra: which command should `ra record %<name>s` run?%<offer>s "
11
+ TOLD = "ra: `ra record %<name>s` runs %<offer>s"
12
+ NEEDED = "record needs a command: ra record %<name>s -- <command>"
13
+ RAILS_TESTS = "bin/rails test"
14
+
15
+ def initialize(project, out, input)
16
+ @project = project
17
+ @out = out
18
+ @input = input
19
+ end
20
+
21
+ def call(name)
22
+ offer = { "test" => tests, "dev" => server }[name]
23
+ answer = @input.tty? ? asked(name, offer) : told(name, offer)
24
+ answer or raise ArgumentError, format(NEEDED, name: name)
25
+ end
26
+
27
+ private
28
+
29
+ def asked(name, offer)
30
+ @out.print format(ASK, name: name, offer: (" [#{offer}]" if offer))
31
+ typed = @input.gets.to_s.strip
32
+ typed.empty? ? offer : typed
33
+ end
34
+
35
+ def told(name, offer)
36
+ @out.puts format(TOLD, name: name, offer: offer) if offer
37
+ offer
38
+ end
39
+
40
+ def tests
41
+ return RAILS_TESTS if @project.rails? && @project.framework == :minitest
42
+
43
+ Setup.record_command(@project.framework)
44
+ end
45
+
46
+ def server
47
+ return "bin/dev" if @project.exist?("bin/dev")
48
+
49
+ "bin/rails server" if @project.rails?
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/errors"
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Surfaces
8
+ class Record
9
+ module Run
10
+ SIGNALLED = 128
11
+
12
+ module_function
13
+
14
+ def call(env, line, chdir:)
15
+ wait_through_interrupts(Process.spawn(env, line, chdir: chdir))
16
+ rescue Errors.missing_file => e
17
+ raise ArgumentError, e
18
+ end
19
+
20
+ def wait_through_interrupts(pid)
21
+ status = Process.wait2(pid).last
22
+ status.exitstatus || (SIGNALLED + status.termsig)
23
+ rescue Interrupt
24
+ retry
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "shellwords"
4
+ require_relative "setup"
5
+ require_relative "record/environment"
6
+ require_relative "record/commands"
7
+ require_relative "record/ignore"
8
+ require_relative "record/question"
9
+ require_relative "record/run"
10
+
11
+ module RuntimeAnalysis
12
+ module Query
13
+ module Surfaces
14
+ class Record
15
+ TESTS = "test"
16
+ LABEL = "record"
17
+ RECORDING = "ra: recording `%<line>s` into %<recording>s"
18
+ SAVED = "ra: saved for `ra record %<name>s`, in %<file>s\n Change it with: ra record %<name>s -- <command>"
19
+ IGNORED = "ra: git ignores %<recording>s here from now on, through %<file>s, which is never committed"
20
+ RECORDED = "ra: recorded. `ra tools` lists what you can ask about it."
21
+ NOTHING = "ra: nothing was recorded. No Ruby program the command started loaded code from app or lib."
22
+ CONFIGURED = "ra: nothing new in %<recording>s. This project's own configuration decides what it records."
23
+ USAGE = <<~TEXT
24
+ Usage: ra record [name] [-- command]
25
+
26
+ Runs one of the project's commands with recording on, in a project that has not added the gem.
27
+ ra record the tests, asking once which command runs them
28
+ ra record dev the server, asked for the same way
29
+ ra record dev -- bin/dev saves the command for a name, and runs it
30
+ ra record -- <command> runs a command once and saves nothing
31
+ TEXT
32
+
33
+ def self.command(argv, line, out)
34
+ command = after_dashes(argv)
35
+ line.parse!(argv)
36
+ return help(out) if line.help?
37
+
38
+ new(line.root, line.recording, out: out).call(argv.shift, command)
39
+ end
40
+
41
+ def self.after_dashes(argv)
42
+ argv.slice!(argv.index("--") || argv.size..).drop(1)
43
+ end
44
+
45
+ def self.help(out)
46
+ out.puts USAGE
47
+ 0
48
+ end
49
+
50
+ def self.environment(root, recording)
51
+ Environment.new(root, recording)
52
+ end
53
+
54
+ def initialize(root, recording, out:, input: $stdin, env: ENV)
55
+ @root = root
56
+ @env = env
57
+ @recording = where_the_recorder_writes(recording)
58
+ @out = out
59
+ @input = input
60
+ end
61
+
62
+ def where_the_recorder_writes(recording)
63
+ given = @env["RUNTIME_ANALYSIS_OUTPUT"]
64
+ given ? File.expand_path(given, @root) : recording
65
+ end
66
+
67
+ def call(name = nil, command = [])
68
+ return record(Shellwords.join(command), LABEL) if name.nil? && command.any?
69
+
70
+ name ||= TESTS
71
+ record(command.empty? ? known(name) : save(name, Shellwords.join(command)), name)
72
+ end
73
+
74
+ private
75
+
76
+ def environment
77
+ @environment ||= Environment.new(@root, @recording, env: @env)
78
+ end
79
+
80
+ def commands
81
+ @commands ||= Commands.new(Commands.file(@env), @root)
82
+ end
83
+
84
+ def known(name)
85
+ commands[name] || save(name, Question.new(Setup.project(@root), @out, @input).call(name))
86
+ end
87
+
88
+ def save(name, line)
89
+ commands[name] = line
90
+ @out.puts format(SAVED, name: name, file: commands.file)
91
+ line
92
+ end
93
+
94
+ def record(line, label)
95
+ @out.puts format(RECORDING, line: line, recording: shown)
96
+ ignore
97
+ before = runs
98
+ Run.call(environment.to_h(label), line, chdir: @root).tap { @out.puts outcome(before) }
99
+ end
100
+
101
+ def ignore
102
+ file = Ignore.new(@root, @recording).call
103
+ @out.puts format(IGNORED, recording: shown, file: file) if file
104
+ end
105
+
106
+ def outcome(before)
107
+ return RECORDED unless runs.eql?(before)
108
+
109
+ environment.set_up? ? format(CONFIGURED, recording: shown) : NOTHING
110
+ end
111
+
112
+ def runs
113
+ Dir[File.join(@recording, "run.*.json")]
114
+ end
115
+
116
+ def shown
117
+ @recording.delete_prefix("#{@root}/")
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "record"
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Surfaces
8
+ class Rerun
9
+ COMMAND = %w[bundle exec rspec].freeze
10
+ LABEL = "rerun"
11
+
12
+ def self.command(argv, line, graphs, out)
13
+ command = argv.slice!(argv.index("--") || argv.size..).drop(1)
14
+ line.parse!(argv)
15
+ file = argv.shift or raise ArgumentError, "rerun needs a file"
16
+ on(line, graphs, out).call(file, command)
17
+ end
18
+
19
+ def self.on(line, graphs, out)
20
+ environment = Record.environment(line.root, line.recording)
21
+ new(-> { graphs.load(line.recording) }, Project.new(line.root), environment: environment, out: out)
22
+ end
23
+
24
+ def initialize(recording, project, environment:, out:, run: Kernel.public_method(:system))
25
+ @recording = recording
26
+ @project = project
27
+ @environment = environment
28
+ @out = out
29
+ @run = run
30
+ end
31
+
32
+ def call(file, command = [])
33
+ file = @project.relative(File.expand_path(file, @project.root))
34
+ examples = examples_for(file)
35
+ examples.empty? ? nothing_reaches(file) : record(file, examples, command)
36
+ end
37
+
38
+ private
39
+
40
+ def record(file, examples, command)
41
+ @out.puts "ra: recording #{examples == [file] ? file : "#{counted(examples)} #{file}"}"
42
+ @run.call(@environment.to_h(LABEL), *runner(command), *examples, chdir: @project.root) ? 0 : 1
43
+ end
44
+
45
+ def nothing_reaches(file)
46
+ @out.puts "ra: no recorded example reaches #{file}"
47
+ 1
48
+ end
49
+
50
+ def counted(examples)
51
+ examples.size == 1 ? "1 example that reaches" : "#{examples.size} examples that reach"
52
+ end
53
+
54
+ def runner(command)
55
+ command.empty? ? COMMAND : command
56
+ end
57
+
58
+ def examples_for(file)
59
+ spec?(file) ? [file] : recorded(file)
60
+ end
61
+
62
+ def spec?(file)
63
+ @project.spec_file?(file) || file.start_with?("spec/", "test/")
64
+ end
65
+
66
+ def recorded(file)
67
+ entries = closest_first(reaching(file)).map { |tree| tree.entry_point.delete_prefix("./") }
68
+ entries.uniq.select { |example| exists?(example) }
69
+ end
70
+
71
+ def closest_first(trees)
72
+ trees.sort_by { |tree| tree.nodes.size }
73
+ end
74
+
75
+ def reaching(file)
76
+ @recording.call.trees.select(&:spec?).select do |tree|
77
+ tree.nodes.any? { |node| @project.relative(node.callee.file) == file }
78
+ end
79
+ end
80
+
81
+ def exists?(example)
82
+ File.exist?(File.expand_path(example.sub(/:\d+\z/, ""), @project.root))
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../setup_spec.rb, with stand-in executables on a PATH of its own.
4
+
5
+ require "runtime_analysis/platform"
6
+
7
+ module RuntimeAnalysis
8
+ module Query
9
+ module Surfaces
10
+ class Setup
11
+ class Agents
12
+ PASTE = "Run `ra setup --print` and follow the steps it prints."
13
+
14
+ Agent = Struct.new(:name, :executable, :prompted)
15
+ KNOWN = [
16
+ Agent.new("Claude Code", "claude", ->(prompt) { ["claude", prompt] }),
17
+ Agent.new("Codex", "codex"), Agent.new("Gemini CLI", "gemini"), Agent.new("Cursor", "cursor-agent"),
18
+ Agent.new("Aider", "aider"), Agent.new("opencode", "opencode")
19
+ ].freeze
20
+
21
+ def initialize(out, path, start)
22
+ @out = out
23
+ @path = path.split(Platform.search_path_separator)
24
+ @start = start
25
+ end
26
+
27
+ def any?
28
+ found.any?
29
+ end
30
+
31
+ def hand_over(name)
32
+ agent = chosen(name)
33
+ return unaided unless agent
34
+
35
+ others(agent)
36
+ agent.prompted ? start(agent) : paste(agent)
37
+ end
38
+
39
+ private
40
+
41
+ def chosen(name)
42
+ named(name) || found.first
43
+ end
44
+
45
+ def unaided
46
+ @out.puts "No coding agent found on your PATH. These are the steps:\n\n#{Setup.steps}"
47
+ end
48
+
49
+ def found
50
+ @found ||= KNOWN.select { |agent| @path.any? { |dir| File.executable?(File.join(dir, agent.executable)) } }
51
+ end
52
+
53
+ def named(name)
54
+ return unless name
55
+
56
+ found.find { |agent| agent.executable == name } or raise ArgumentError, "#{name} is not on your PATH"
57
+ end
58
+
59
+ def others(agent)
60
+ rest = found - [agent]
61
+ @out.puts "Also found: #{rest.map(&:name).join(", ")}. Pick one with --agent NAME." if rest.any?
62
+ end
63
+
64
+ def start(agent)
65
+ @out.puts "Starting #{agent.name} with the setup steps. `ra setup --print` shows them."
66
+ @start.call(*agent.prompted.call(Setup.steps))
67
+ end
68
+
69
+ def paste(agent)
70
+ @out.puts "#{agent.name} is installed. Start it in this directory and give it this:\n\n #{PASTE}"
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../setup_spec.rb, which reads every file these change.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Surfaces
8
+ class Setup
9
+ class Changes
10
+ Row = Struct.new(:status, :file, :note)
11
+
12
+ ADD = ["bundle", "add", "runtime_analysis", "--group", "development, test"].freeze
13
+ INITIALIZER = "config/initializers/runtime_analysis.rb"
14
+ IGNORED = %r{^/?tmp(/\*?)?$|runtime_analysis}
15
+ ROUTES = "config/routes.rb"
16
+ MOUNT = "to watch a running server, mount RuntimeAnalysis::Web behind your auth: ra setup --print"
17
+ NO_HELPER = "no spec_helper.rb or test_helper.rb"
18
+ NO_GEMFILE = "there is none. Add runtime_analysis however this project takes gems"
19
+ CONFIGURATION = <<~RUBY
20
+ if ENV["RUNTIME_ANALYSIS_ENABLED"]
21
+ RuntimeAnalysis.configure do |c|
22
+ c.included = %%w[%<code>s]
23
+ c.recording_dir = ENV.fetch("RUNTIME_ANALYSIS_OUTPUT", "tmp/runtime_analysis")
24
+ c.tier = ENV.fetch("RUNTIME_ANALYSIS_TIER", "raises")
25
+ end
26
+ RuntimeAnalysis.start!
27
+ at_exit { RuntimeAnalysis.finish! }
28
+ end
29
+ RUBY
30
+
31
+ def initialize(project, run)
32
+ @project = project
33
+ @run = run
34
+ end
35
+
36
+ def rows
37
+ named_before_configuring = naming
38
+ [gemfile, configuration, named_before_configuring, gitignore, endpoint].compact
39
+ end
40
+
41
+ private
42
+
43
+ def has?(file, text)
44
+ @project.read(file).include?(text)
45
+ end
46
+
47
+ def gemfile
48
+ return Row.new(:you, "Gemfile", NO_GEMFILE) unless @project.exist?("Gemfile")
49
+ return Row.new(:already, "Gemfile", "has runtime_analysis") if has?("Gemfile", "runtime_analysis")
50
+
51
+ add
52
+ end
53
+
54
+ def add
55
+ return Row.new(:done, "Gemfile", "added runtime_analysis") if @run.call(*ADD, chdir: @project.root)
56
+
57
+ Row.new(:you, "Gemfile", %(run: bundle add runtime_analysis --group "development, test"))
58
+ end
59
+
60
+ def configuration
61
+ place = @project.rails? ? INITIALIZER : @project.helper
62
+ return Row.new(:you, NO_HELPER, "configure recording before your code loads: ra setup --print") unless place
63
+ return Row.new(:already, place, "configures recording") if has?(place, "RuntimeAnalysis.configure")
64
+
65
+ configure(place)
66
+ Row.new(:done, place, "records when RUNTIME_ANALYSIS_ENABLED is set")
67
+ end
68
+
69
+ def configure(place)
70
+ settings = format(CONFIGURATION, code: @project.code.join(" "))
71
+ return @project.write(place, settings) if @project.rails?
72
+
73
+ @project.prepend_below_opening_comments(place, %(require "runtime_analysis"\n#{settings}))
74
+ end
75
+
76
+ def naming
77
+ helper = @project.helper
78
+ return Row.new(:you, NO_HELPER, "name each test in the recording: ra setup --print") unless helper
79
+ return Row.new(:already, helper, "names each test") if has?(helper, hook)
80
+
81
+ @project.prepend_below_opening_comments(helper, "#{hook}\n")
82
+ Row.new(:done, helper, "names each test in the recording")
83
+ end
84
+
85
+ def hook
86
+ %(require "runtime_analysis/#{@project.framework}")
87
+ end
88
+
89
+ def endpoint
90
+ return unless @project.rails?
91
+ return Row.new(:already, ROUTES, "mounts the stats endpoint") if has?(ROUTES, "RuntimeAnalysis::Web")
92
+
93
+ Row.new(:optional, ROUTES, MOUNT)
94
+ end
95
+
96
+ def gitignore
97
+ ignored = @project.read(".gitignore").match?(IGNORED)
98
+ return Row.new(:already, ".gitignore", "ignores the recording") if ignored
99
+
100
+ @project.append(".gitignore", "tmp/runtime_analysis/\n")
101
+ Row.new(:done, ".gitignore", "ignores tmp/runtime_analysis/")
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through ../setup_spec.rb, which sets up projects of each kind.
4
+
5
+ module RuntimeAnalysis
6
+ module Query
7
+ module Surfaces
8
+ class Setup
9
+ class Project
10
+ HELPERS = { rspec: "spec/spec_helper.rb", minitest: "test/test_helper.rb" }.freeze
11
+ NAMES = { rspec: "RSpec", minitest: "Minitest" }.freeze
12
+ BUNDLE = %w[Gemfile Gemfile.lock].freeze
13
+
14
+ attr_reader :root
15
+
16
+ def initialize(root)
17
+ @root = root
18
+ end
19
+
20
+ def rails?
21
+ exist?("config/application.rb")
22
+ end
23
+
24
+ def framework
25
+ HELPERS.keys.find { |name| exist?(HELPERS.fetch(name)) }
26
+ end
27
+
28
+ def helper
29
+ HELPERS[framework]
30
+ end
31
+
32
+ def bundled?
33
+ BUNDLE.any? { |file| read(file).include?("runtime_analysis") }
34
+ end
35
+
36
+ def code
37
+ %w[app lib].select { |dir| File.directory?(path(dir)) }
38
+ end
39
+
40
+ def to_s
41
+ [("Rails" if rails?), NAMES[framework], ("code in #{code.join(" and ")}" if code.any?)].compact.join(", ")
42
+ end
43
+
44
+ def exist?(file)
45
+ File.exist?(path(file))
46
+ end
47
+
48
+ def read(file)
49
+ exist?(file) ? File.read(path(file)) : ""
50
+ end
51
+
52
+ def write(file, text)
53
+ FileUtils.mkdir_p(File.dirname(path(file)))
54
+ File.write(path(file), text)
55
+ end
56
+
57
+ def append(file, text)
58
+ write(file, "#{ended(read(file))}#{text}")
59
+ end
60
+
61
+ def prepend_below_opening_comments(file, text)
62
+ lines = ended(read(file)).lines
63
+ opening = lines.take_while { |line| line.match?(/\A\s*(#.*)?\n\z/) }
64
+ write(file, "#{opening.join}#{text}\n#{lines.drop(opening.size).join}")
65
+ end
66
+
67
+ private
68
+
69
+ def ended(text)
70
+ text.empty? || text.end_with?("\n") ? text : "#{text}\n"
71
+ end
72
+
73
+ def path(file)
74
+ File.join(@root, file)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end