rails-ai-context 5.30.2 → 5.32.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 (222) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +868 -0
  3. data/CONTEXT.md +7 -3
  4. data/README.md +1 -1
  5. data/docs/ARCHITECTURE.md +3 -3
  6. data/docs/CLI.md +1 -1
  7. data/docs/COMPATIBILITY.md +50 -14
  8. data/docs/CONFIGURATION.md +4 -1
  9. data/docs/FAQ.md +1 -1
  10. data/docs/GUIDE.md +14 -8
  11. data/docs/INTROSPECTORS.md +50 -23
  12. data/docs/SECURITY.md +56 -1
  13. data/docs/TOOLS.md +58 -12
  14. data/lib/generators/rails_ai_context/install/install_generator.rb +1 -0
  15. data/lib/rails_ai_context/action_filters.rb +121 -48
  16. data/lib/rails_ai_context/app_commands.rb +2 -2
  17. data/lib/rails_ai_context/app_kind.rb +65 -8
  18. data/lib/rails_ai_context/ast_cache.rb +53 -18
  19. data/lib/rails_ai_context/base_mixins.rb +46 -16
  20. data/lib/rails_ai_context/boot_manager.rb +5 -1
  21. data/lib/rails_ai_context/cli/entry_boot.rb +17 -2
  22. data/lib/rails_ai_context/cli/tool_runner.rb +15 -1
  23. data/lib/rails_ai_context/concern_macros.rb +791 -111
  24. data/lib/rails_ai_context/concern_membership.rb +25 -5
  25. data/lib/rails_ai_context/concern_paths.rb +63 -17
  26. data/lib/rails_ai_context/config_yaml.rb +40 -0
  27. data/lib/rails_ai_context/configuration.rb +3 -3
  28. data/lib/rails_ai_context/data/docs/index.json +324 -0
  29. data/lib/rails_ai_context/database_yml.rb +136 -0
  30. data/lib/rails_ai_context/doctor.rb +30 -37
  31. data/lib/rails_ai_context/erb_source.rb +40 -0
  32. data/lib/rails_ai_context/fingerprinter.rb +48 -22
  33. data/lib/rails_ai_context/fixture_keys.rb +42 -1
  34. data/lib/rails_ai_context/gem_lock.rb +275 -47
  35. data/lib/rails_ai_context/git_ignore.rb +13 -11
  36. data/lib/rails_ai_context/hydrators/hydration_formatter.rb +8 -2
  37. data/lib/rails_ai_context/hydrators/schema_hint_builder.rb +10 -5
  38. data/lib/rails_ai_context/introspector.rb +10 -1
  39. data/lib/rails_ai_context/introspectors/action_mailbox_introspector.rb +27 -19
  40. data/lib/rails_ai_context/introspectors/action_presence.rb +11 -8
  41. data/lib/rails_ai_context/introspectors/action_resolver.rb +38 -26
  42. data/lib/rails_ai_context/introspectors/action_text_introspector.rb +2 -2
  43. data/lib/rails_ai_context/introspectors/active_record_settings.rb +56 -0
  44. data/lib/rails_ai_context/introspectors/active_storage_introspector.rb +1 -1
  45. data/lib/rails_ai_context/introspectors/active_support_introspector.rb +98 -22
  46. data/lib/rails_ai_context/introspectors/admin_resources.rb +101 -0
  47. data/lib/rails_ai_context/introspectors/anyway_configs.rb +64 -0
  48. data/lib/rails_ai_context/introspectors/apartment_config.rb +47 -0
  49. data/lib/rails_ai_context/introspectors/api_introspector.rb +125 -26
  50. data/lib/rails_ai_context/introspectors/asset_pipeline_introspector.rb +24 -19
  51. data/lib/rails_ai_context/introspectors/ast_walk.rb +18 -0
  52. data/lib/rails_ai_context/introspectors/auth_introspector.rb +71 -18
  53. data/lib/rails_ai_context/introspectors/autoload_introspector.rb +62 -15
  54. data/lib/rails_ai_context/introspectors/base.rb +38 -0
  55. data/lib/rails_ai_context/introspectors/call_site_expansion.rb +349 -39
  56. data/lib/rails_ai_context/introspectors/component_introspector.rb +26 -14
  57. data/lib/rails_ai_context/introspectors/config_introspector.rb +22 -9
  58. data/lib/rails_ai_context/introspectors/connection_pool_introspector.rb +1 -4
  59. data/lib/rails_ai_context/introspectors/controller_filters.rb +335 -31
  60. data/lib/rails_ai_context/introspectors/controller_introspector.rb +315 -129
  61. data/lib/rails_ai_context/introspectors/controller_settings.rb +182 -0
  62. data/lib/rails_ai_context/introspectors/convention_introspector.rb +18 -15
  63. data/lib/rails_ai_context/introspectors/database_stats_introspector.rb +1 -1
  64. data/lib/rails_ai_context/introspectors/declared_constant.rb +108 -23
  65. data/lib/rails_ai_context/introspectors/eager_load.rb +12 -7
  66. data/lib/rails_ai_context/introspectors/engine_introspector.rb +13 -5
  67. data/lib/rails_ai_context/introspectors/env_config_introspector.rb +62 -17
  68. data/lib/rails_ai_context/introspectors/env_introspector.rb +8 -6
  69. data/lib/rails_ai_context/introspectors/env_references.rb +21 -4
  70. data/lib/rails_ai_context/introspectors/frontend_framework_introspector.rb +37 -22
  71. data/lib/rails_ai_context/introspectors/gem_introspector.rb +3 -1
  72. data/lib/rails_ai_context/introspectors/gemfile_gems.rb +53 -5
  73. data/lib/rails_ai_context/introspectors/grape_endpoints.rb +109 -0
  74. data/lib/rails_ai_context/introspectors/helper_names.rb +12 -1
  75. data/lib/rails_ai_context/introspectors/i18n_introspector.rb +101 -46
  76. data/lib/rails_ai_context/introspectors/includers.rb +6 -1
  77. data/lib/rails_ai_context/introspectors/initializer_introspector.rb +2 -5
  78. data/lib/rails_ai_context/introspectors/job_introspector.rb +548 -103
  79. data/lib/rails_ai_context/introspectors/listener_registration.rb +11 -1
  80. data/lib/rails_ai_context/introspectors/listeners/associations_listener.rb +51 -4
  81. data/lib/rails_ai_context/introspectors/listeners/autoload_ignore_listener.rb +31 -0
  82. data/lib/rails_ai_context/introspectors/listeners/autoload_paths_listener.rb +4 -47
  83. data/lib/rails_ai_context/introspectors/listeners/base_listener.rb +80 -14
  84. data/lib/rails_ai_context/introspectors/listeners/branch_conditions.rb +156 -0
  85. data/lib/rails_ai_context/introspectors/listeners/callbacks_listener.rb +85 -23
  86. data/lib/rails_ai_context/introspectors/listeners/class_definition_listener.rb +35 -6
  87. data/lib/rails_ai_context/introspectors/listeners/component_structure_listener.rb +30 -2
  88. data/lib/rails_ai_context/introspectors/listeners/conditional_macro_listener.rb +19 -0
  89. data/lib/rails_ai_context/introspectors/listeners/config_assignment_listener.rb +255 -10
  90. data/lib/rails_ai_context/introspectors/listeners/constant_reference_listener.rb +34 -0
  91. data/lib/rails_ai_context/introspectors/listeners/constructor_macro_listener.rb +123 -0
  92. data/lib/rails_ai_context/introspectors/listeners/definition_file_paths_listener.rb +66 -0
  93. data/lib/rails_ai_context/introspectors/listeners/enums_listener.rb +2 -2
  94. data/lib/rails_ai_context/introspectors/listeners/env_access_listener.rb +31 -0
  95. data/lib/rails_ai_context/introspectors/listeners/filter_macro_listener.rb +54 -0
  96. data/lib/rails_ai_context/introspectors/listeners/fixture_paths_listener.rb +62 -0
  97. data/lib/rails_ai_context/introspectors/listeners/gemfile_dsl_listener.rb +42 -1
  98. data/lib/rails_ai_context/introspectors/listeners/generic_macro_listener.rb +28 -2
  99. data/lib/rails_ai_context/introspectors/listeners/grape_api_listener.rb +137 -0
  100. data/lib/rails_ai_context/introspectors/listeners/http_client_call_listener.rb +50 -0
  101. data/lib/rails_ai_context/introspectors/listeners/i18n_load_path_listener.rb +54 -0
  102. data/lib/rails_ai_context/introspectors/listeners/literal_paths.rb +123 -0
  103. data/lib/rails_ai_context/introspectors/listeners/macros_listener.rb +248 -4
  104. data/lib/rails_ai_context/introspectors/listeners/mailbox_routing_listener.rb +2 -1
  105. data/lib/rails_ai_context/introspectors/listeners/method_call_listener.rb +7 -1
  106. data/lib/rails_ai_context/introspectors/listeners/methods_listener.rb +352 -69
  107. data/lib/rails_ai_context/introspectors/listeners/migration_dsl_listener.rb +1 -1
  108. data/lib/rails_ai_context/introspectors/listeners/migration_replay_listener.rb +35 -26
  109. data/lib/rails_ai_context/introspectors/listeners/mixins_listener.rb +62 -29
  110. data/lib/rails_ai_context/introspectors/listeners/mount_listener.rb +8 -0
  111. data/lib/rails_ai_context/introspectors/listeners/namespaced_roots_listener.rb +35 -0
  112. data/lib/rails_ai_context/introspectors/listeners/nested_constants_listener.rb +32 -0
  113. data/lib/rails_ai_context/introspectors/listeners/owner_scope.rb +67 -0
  114. data/lib/rails_ai_context/introspectors/listeners/preview_paths_listener.rb +16 -7
  115. data/lib/rails_ai_context/introspectors/listeners/proc_literal_listener.rb +42 -0
  116. data/lib/rails_ai_context/introspectors/listeners/queue_assignment_listener.rb +54 -0
  117. data/lib/rails_ai_context/introspectors/listeners/rake_task_dsl_listener.rb +21 -8
  118. data/lib/rails_ai_context/introspectors/listeners/route_files_listener.rb +4 -11
  119. data/lib/rails_ai_context/introspectors/listeners/routes_dsl_listener.rb +461 -65
  120. data/lib/rails_ai_context/introspectors/listeners/schema_dsl_listener.rb +76 -14
  121. data/lib/rails_ai_context/introspectors/listeners/scopes_listener.rb +40 -1
  122. data/lib/rails_ai_context/introspectors/listeners/validations_listener.rb +10 -1
  123. data/lib/rails_ai_context/introspectors/listeners/view_paths_listener.rb +37 -0
  124. data/lib/rails_ai_context/introspectors/middleware_introspector.rb +47 -3
  125. data/lib/rails_ai_context/introspectors/migration_introspector.rb +3 -3
  126. data/lib/rails_ai_context/introspectors/migration_replay/helpers.rb +4 -3
  127. data/lib/rails_ai_context/introspectors/migration_replay/statements.rb +72 -12
  128. data/lib/rails_ai_context/introspectors/migration_replay.rb +110 -14
  129. data/lib/rails_ai_context/introspectors/model_introspector.rb +708 -214
  130. data/lib/rails_ai_context/introspectors/module_aliases.rb +11 -3
  131. data/lib/rails_ai_context/introspectors/multi_database_introspector.rb +9 -57
  132. data/lib/rails_ai_context/introspectors/observability_introspector.rb +5 -8
  133. data/lib/rails_ai_context/introspectors/performance_introspector.rb +28 -8
  134. data/lib/rails_ai_context/introspectors/rake_task_introspector.rb +118 -51
  135. data/lib/rails_ai_context/introspectors/recurring_schedules.rb +150 -0
  136. data/lib/rails_ai_context/introspectors/rendered_record.rb +87 -0
  137. data/lib/rails_ai_context/introspectors/retry_policy.rb +8 -5
  138. data/lib/rails_ai_context/introspectors/route_introspector.rb +153 -42
  139. data/lib/rails_ai_context/introspectors/schema_conventions.rb +127 -40
  140. data/lib/rails_ai_context/introspectors/schema_dump_path.rb +80 -0
  141. data/lib/rails_ai_context/introspectors/schema_introspector.rb +346 -112
  142. data/lib/rails_ai_context/introspectors/schema_reader.rb +69 -29
  143. data/lib/rails_ai_context/introspectors/service_classes.rb +21 -15
  144. data/lib/rails_ai_context/introspectors/source_calls.rb +1 -1
  145. data/lib/rails_ai_context/introspectors/source_introspector.rb +17 -2
  146. data/lib/rails_ai_context/introspectors/source_scan.rb +174 -26
  147. data/lib/rails_ai_context/introspectors/sqlite_virtual_tables.rb +28 -0
  148. data/lib/rails_ai_context/introspectors/stimulus_introspector.rb +16 -5
  149. data/lib/rails_ai_context/introspectors/structure_sql_reader.rb +372 -98
  150. data/lib/rails_ai_context/introspectors/superclass_chain.rb +14 -15
  151. data/lib/rails_ai_context/introspectors/table_name.rb +46 -45
  152. data/lib/rails_ai_context/introspectors/test_introspector.rb +289 -78
  153. data/lib/rails_ai_context/introspectors/turbo_introspector.rb +10 -5
  154. data/lib/rails_ai_context/introspectors/view_introspector.rb +5 -40
  155. data/lib/rails_ai_context/introspectors/view_template_introspector.rb +12 -10
  156. data/lib/rails_ai_context/method_name.rb +31 -0
  157. data/lib/rails_ai_context/migration_status.rb +5 -5
  158. data/lib/rails_ai_context/output_guard.rb +6 -4
  159. data/lib/rails_ai_context/package_json.rb +96 -5
  160. data/lib/rails_ai_context/path_resolver.rb +170 -15
  161. data/lib/rails_ai_context/payload.rb +48 -0
  162. data/lib/rails_ai_context/pending_migrations.rb +15 -18
  163. data/lib/rails_ai_context/portable_path.rb +76 -4
  164. data/lib/rails_ai_context/redaction.rb +8 -2
  165. data/lib/rails_ai_context/route_coverage.rb +5 -1
  166. data/lib/rails_ai_context/safe_file.rb +4 -1
  167. data/lib/rails_ai_context/safe_path.rb +61 -10
  168. data/lib/rails_ai_context/schema_hint.rb +8 -2
  169. data/lib/rails_ai_context/schema_version.rb +7 -9
  170. data/lib/rails_ai_context/serializers/claude_rules_serializer.rb +1 -1
  171. data/lib/rails_ai_context/serializers/markdown_serializer.rb +13 -4
  172. data/lib/rails_ai_context/serializers/section_facts.rb +5 -1
  173. data/lib/rails_ai_context/serializers/stack_overview_helper.rb +2 -2
  174. data/lib/rails_ai_context/serializers/tool_guide_helper.rb +7 -4
  175. data/lib/rails_ai_context/server.rb +26 -7
  176. data/lib/rails_ai_context/test_framework.rb +32 -5
  177. data/lib/rails_ai_context/tools/analyze_feature.rb +22 -10
  178. data/lib/rails_ai_context/tools/base_tool.rb +69 -30
  179. data/lib/rails_ai_context/tools/diagnose.rb +1 -1
  180. data/lib/rails_ai_context/tools/generate_test.rb +8 -9
  181. data/lib/rails_ai_context/tools/get_active_support.rb +19 -4
  182. data/lib/rails_ai_context/tools/get_api.rb +17 -11
  183. data/lib/rails_ai_context/tools/get_autoload.rb +10 -5
  184. data/lib/rails_ai_context/tools/get_callbacks.rb +65 -14
  185. data/lib/rails_ai_context/tools/get_component_catalog.rb +7 -4
  186. data/lib/rails_ai_context/tools/get_concern.rb +31 -18
  187. data/lib/rails_ai_context/tools/get_config.rb +52 -18
  188. data/lib/rails_ai_context/tools/get_controllers.rb +57 -13
  189. data/lib/rails_ai_context/tools/get_conventions.rb +44 -22
  190. data/lib/rails_ai_context/tools/get_engines.rb +2 -0
  191. data/lib/rails_ai_context/tools/get_env.rb +209 -52
  192. data/lib/rails_ai_context/tools/get_env_config.rb +38 -7
  193. data/lib/rails_ai_context/tools/get_frontend_stack.rb +56 -7
  194. data/lib/rails_ai_context/tools/get_helper_methods.rb +136 -26
  195. data/lib/rails_ai_context/tools/get_job_pattern.rb +143 -87
  196. data/lib/rails_ai_context/tools/get_mailers.rb +59 -2
  197. data/lib/rails_ai_context/tools/get_model_details.rb +178 -28
  198. data/lib/rails_ai_context/tools/get_partial_interface.rb +101 -62
  199. data/lib/rails_ai_context/tools/get_routes.rb +35 -9
  200. data/lib/rails_ai_context/tools/get_schema.rb +160 -61
  201. data/lib/rails_ai_context/tools/get_service_pattern.rb +121 -25
  202. data/lib/rails_ai_context/tools/get_stimulus.rb +1 -1
  203. data/lib/rails_ai_context/tools/get_test_info.rb +152 -82
  204. data/lib/rails_ai_context/tools/get_view.rb +37 -20
  205. data/lib/rails_ai_context/tools/migration_advisor.rb +64 -24
  206. data/lib/rails_ai_context/tools/onboard.rb +174 -88
  207. data/lib/rails_ai_context/tools/performance_check.rb +5 -13
  208. data/lib/rails_ai_context/tools/query.rb +122 -28
  209. data/lib/rails_ai_context/tools/read_logs.rb +45 -17
  210. data/lib/rails_ai_context/tools/runtime_info.rb +1 -1
  211. data/lib/rails_ai_context/tools/safe_call.rb +20 -15
  212. data/lib/rails_ai_context/tools/search_code.rb +114 -40
  213. data/lib/rails_ai_context/tools/search_docs.rb +22 -11
  214. data/lib/rails_ai_context/tools/security_scan.rb +51 -4
  215. data/lib/rails_ai_context/tools/validate.rb +21 -4
  216. data/lib/rails_ai_context/tools/validate_semantics.rb +18 -6
  217. data/lib/rails_ai_context/version.rb +1 -1
  218. data/lib/rails_ai_context/vfs.rb +2 -2
  219. data/lib/rails_ai_context/view_file.rb +53 -3
  220. data/lib/rails_ai_context.rb +10 -2
  221. data/server.json +1 -1
  222. metadata +34 -3
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "set"
3
4
  require "yaml"
4
5
 
5
6
  module RailsAiContext
@@ -19,12 +20,14 @@ module RailsAiContext
19
20
  # the class, never from the directory.
20
21
  JOB_DIRS = %w[app/jobs app/workers app/sidekiq].freeze
21
22
  ACTIVE_JOB_BASES = %w[ActiveJob::Base ApplicationJob ActionMailer::MailDeliveryJob].freeze
22
- JOB_MACROS = %i[
23
- queue_as retry_on discard_on
24
- sidekiq_options sidekiq_throttle include
25
- before_enqueue after_enqueue before_perform after_perform
26
- around_perform around_enqueue
23
+ QUE_JOB_BASES = %w[Que::Job].freeze
24
+ JOB_CALLBACKS = %i[
25
+ before_enqueue after_enqueue around_enqueue before_perform after_perform around_perform after_discard
27
26
  ].freeze
27
+ JOB_MACROS = (%i[
28
+ queue_as queue_with_priority enqueue_after_transaction_commit= limits_concurrency include
29
+ sidekiq_throttle step
30
+ ] + RetryPolicy::MACROS + JOB_CALLBACKS).freeze
28
31
 
29
32
  # @return [Hash] async workers, mailers, and channels
30
33
  def call
@@ -35,11 +38,15 @@ module RailsAiContext
35
38
  workers: extract_workers,
36
39
  job_bases: job_bases,
37
40
  enqueue_helpers: enqueue_helpers,
41
+ async_methods: async_methods,
38
42
  mailers: booted_mailers[:mailers],
39
43
  mailer_bases: booted_mailers[:bases],
40
44
  channels: extract_channels,
41
- recurring_jobs: extract_solid_queue_recurring,
42
- sidekiq_config: extract_sidekiq_config
45
+ connections: extract_connections,
46
+ recurring_jobs: recurring_jobs,
47
+ sidekiq_config: extract_sidekiq_config,
48
+ solid_queue_config: extract_solid_queue_config,
49
+ mailer_settings: mailer_settings(booted_mailers[:mailers], booted: true)
43
50
  }
44
51
  end
45
52
 
@@ -52,11 +59,15 @@ module RailsAiContext
52
59
  workers: extract_workers,
53
60
  job_bases: job_bases,
54
61
  enqueue_helpers: enqueue_helpers,
62
+ async_methods: async_methods,
55
63
  mailers: source_mailers[:mailers],
56
64
  mailer_bases: source_mailers[:bases],
57
65
  channels: extract_channels_from_source,
58
- recurring_jobs: extract_solid_queue_recurring,
59
- sidekiq_config: extract_sidekiq_config
66
+ connections: extract_connections,
67
+ recurring_jobs: recurring_jobs,
68
+ sidekiq_config: extract_sidekiq_config,
69
+ solid_queue_config: extract_solid_queue_config,
70
+ mailer_settings: mailer_settings(source_mailers[:mailers])
60
71
  }
61
72
  end
62
73
 
@@ -72,24 +83,29 @@ module RailsAiContext
72
83
  job.name.start_with?("ActionMailer", "ActiveStorage::", "ActionMailbox::", "Turbo::", "Sentry::")
73
84
  next unless app_defined?(job)
74
85
  if SuperclassChain.abstract_base?(job.name, inherited: job.descendants.any?)
75
- reflected_bases << { name: job.name, file: source_file_for(job) }
86
+ reflected_bases << { name: job.name, file: source_file_for(job), active_job: true }
76
87
  next
77
88
  end
78
89
 
79
90
  queue = job.queue_name
80
- # ActiveJob defaults queue_name to a lambda; any other Proc is a
81
- # queue_as block, and only the source says what it computes.
91
+ # The default is a lambda; another Proc is a queue_as block, and a
92
+ # Proc argument is stored as its inspect string, with the line that set it.
82
93
  if queue.equal?(ActiveJob::Base.queue_name)
83
94
  queue = job.queue_name_from_part(nil)
84
95
  elsif queue.is_a?(Proc)
85
- queue = (job_candidates.key?(job.name) && inherited_queue(job.name)) || COMPUTED_QUEUE
96
+ queue = labelled(COMPUTED_QUEUE, queue.source_location && block_at(*queue.source_location))
97
+ elsif queue.to_s.include?("#<Proc:")
98
+ location = queue.match(/#<Proc:0x\h+ (.+):(\d+)(?: \(lambda\))?>/)
99
+ queue = labelled(PROC_QUEUE, location && proc_at(location[1], location[2].to_i))
86
100
  end
87
101
 
88
102
  {
89
103
  name: job.name,
90
104
  file: source_file_for(job),
105
+ active_job: true,
91
106
  queue: queue.to_s,
92
- priority: job.priority
107
+ # A block priority is a Proc here; the source names it instead.
108
+ priority: (job.priority unless job.priority.is_a?(Proc))
93
109
  }.compact
94
110
  end.sort_by { |j| j[:name] }
95
111
  rescue => e
@@ -108,20 +124,18 @@ module RailsAiContext
108
124
  # all its workers use.
109
125
  def base_record(name, candidate)
110
126
  macros = candidate.ast[:macros]
111
- declares = class_body(macros, candidate.source).select { |m| JOB_BASE_DECLARATIONS.include?(m[:macro]) }
127
+ declares = candidate_body(name).select { |m| JOB_BASE_DECLARATIONS.include?(m[:macro]) }
112
128
  .sort_by { |m| m[:offset] }.map { |m| written(candidate.source, m) }
113
129
  heirs = job_candidates.keys.select { |other| other != name && chain_of(other).include?(name) }
114
- { name: name, file: candidate.file, queue: inherited_queue(name),
130
+ { name: name, file: candidate.file, active_job: (true if active_job?(name)), queue: inherited_queue(name),
115
131
  options: inherited_options(name).presence, retries: RetryPolicy.entries(macros).presence,
116
132
  declares: declares.presence, inherited_by: heirs.sort.presence }.compact
117
133
  end
118
134
 
119
135
  # What a base declares beyond its queue, options and retries: the mixins, the
120
136
  # throttle and the callbacks every job below it runs with.
121
- JOB_BASE_DECLARATIONS = %i[
122
- include sidekiq_throttle before_enqueue after_enqueue before_perform after_perform
123
- around_perform around_enqueue
124
- ].freeze
137
+ JOB_BASE_DECLARATIONS = (%i[include sidekiq_throttle queue_with_priority enqueue_after_transaction_commit= limits_concurrency] +
138
+ JOB_CALLBACKS).freeze
125
139
 
126
140
  # Sidekiq hands `sidekiq_options` to a subclass and ActiveJob hands it `queue_as`,
127
141
  # so both are read down the app's chain, the nearest class that sets a key winning.
@@ -139,7 +153,39 @@ module RailsAiContext
139
153
  end
140
154
 
141
155
  def inherited_queue(name)
142
- chain_of(name).lazy.filter_map { |link| queue_as(job_candidates[link].ast[:macros]) }.first
156
+ chain_of(name).each_with_index.lazy.filter_map { |link, depth| queue_as(job_candidates[link].ast, own: depth.zero?) }.first
157
+ end
158
+
159
+ def labelled(label, source)
160
+ source ? "#{label}: `#{source}`" : label
161
+ end
162
+
163
+ def block_at(file, line)
164
+ queue_as_walk(file)&.dig(:macros)&.find { |m| m[:macro] == :queue_as && m[:location] == line && m[:block] }&.dig(:block)
165
+ end
166
+
167
+ # Only one Proc literal on the line says which one it was.
168
+ def proc_at(file, line)
169
+ found = queue_as_walk(file)&.dig(:procs)&.select { |literal| literal[:line] == line } || []
170
+ found.first[:source] if found.one?
171
+ end
172
+
173
+ # Only the app's own files are read; a job file reuses the walk that found it.
174
+ def queue_as_walk(file)
175
+ @queue_as_walks ||= {}
176
+ @queue_as_walks.fetch(file) do
177
+ @queue_as_walks[file] = begin
178
+ real = File.realpath(file)
179
+ root = app.root.to_s
180
+ if SourceScan.under_root?(file, real, root, app_root_real)
181
+ relative = SourceScan.relative_file(file, real, root, app_root_real)
182
+ job_candidates.values.find { |candidate| candidate.file == relative }&.ast ||
183
+ (source = SafeFile.read(file)) && SourceIntrospector.walk_source(source, QUEUE_AS_LISTENERS)
184
+ end
185
+ rescue SystemCallError
186
+ nil
187
+ end
188
+ end
143
189
  end
144
190
 
145
191
  # This class and its ancestors among the candidates, nearest first.
@@ -153,17 +199,103 @@ module RailsAiContext
153
199
  end
154
200
 
155
201
  COMPUTED_QUEUE = "computed by a block"
202
+ PROC_QUEUE = "queue_as given a Proc: ActiveJob does not call it, so the queue is named after the Proc's text"
203
+ PROC_LITERAL = /\A(?:->|(?:lambda|proc|Proc\.new)(?![\w.]))/
204
+
205
+ QUEUE_AS_LISTENERS = {
206
+ macros: -> { Listeners::GenericMacroListener.new(*JOB_MACROS, block_source: [ :queue_as, :queue_with_priority, *RetryPolicy::BLOCK_MACROS ]) },
207
+ procs: Listeners::ProcLiteralListener,
208
+ queue_assignments: Listeners::QueueAssignmentListener
209
+ }.freeze
156
210
 
157
211
  # A literal queue by name; one picked at enqueue time by the source that
158
212
  # picks it, the way other computed values read.
159
- def queue_as(macros)
160
- hit = macros.find { |m| m[:macro] == :queue_as } or return nil
161
- return hit[:args].first.to_s if hit[:args].any?
213
+ def queue_as(ast, own: true)
214
+ hit = ast[:macros].find { |m| m[:macro] == :queue_as } or return assigned_queue(ast, own)
215
+ return queue_name_from_part(hit[:args].first.to_s) if hit[:args].any?
216
+ return labelled(COMPUTED_QUEUE, hit[:block]) if hit[:block]
162
217
 
163
218
  source = hit[:values].first.to_s.gsub(/\s+/, " ").strip
164
219
  return COMPUTED_QUEUE if source.empty?
165
220
 
166
- source.start_with?("->", "lambda", "proc") ? "#{COMPUTED_QUEUE}: `#{source}`" : "`#{source}` (computed)"
221
+ assigned = Array(ast[:procs]).find { |literal| literal[:constant] == source }
222
+ return labelled(PROC_QUEUE, assigned[:source]) if assigned
223
+
224
+ source.match?(PROC_LITERAL) ? labelled(PROC_QUEUE, source) : "`#{source}` (computed)"
225
+ end
226
+
227
+ # Resque reads @queue off the class itself, so a subclass does not inherit it, and
228
+ # asks `def self.queue` only when @queue is unset; Que resolves self.queue up the superclass chain.
229
+ def assigned_queue(ast, own)
230
+ hits = Array(ast[:queue_assignments]).reverse
231
+ hit = (own && hits.find { |a| a[:form] == :ivar }) || hits.find { |a| a[:form] != :ivar } or return nil
232
+ hit[:queue] || "`#{hit[:source]}` (computed)"
233
+ end
234
+
235
+ # ActiveJob's queue_name_from_part, with the queue settings the app's config assigns.
236
+ # A setting the static tier cannot evaluate reads as its source, with a note why.
237
+ def queue_name_from_part(part)
238
+ settings = active_job_queue_settings
239
+ name = part || settings.dig(:default_queue_name, :text) || "default"
240
+ used = [ (settings[:default_queue_name] unless part) ]
241
+ prefix = settings[:queue_name_prefix]
242
+ if prefix && !prefix[:text].empty?
243
+ delimiter = settings[:queue_name_delimiter]
244
+ name = [ prefix[:text], name ].join(delimiter ? delimiter[:text] : "_")
245
+ used.push(prefix, delimiter)
246
+ end
247
+
248
+ notes = used.compact.filter_map { |setting| setting[:note] }.uniq
249
+ notes.empty? ? name : "#{name} (#{notes.join("; ")})"
250
+ end
251
+
252
+ QUEUE_SETTINGS = %i[queue_name_prefix queue_name_delimiter default_queue_name].freeze
253
+
254
+ # config/application.rb, then this environment's file over it.
255
+ def active_job_queue_settings
256
+ @active_job_queue_settings ||= [ "config/application.rb", "config/environments/#{RailsAiContext.environment_name}.rb" ]
257
+ .each_with_object({}) do |file, settings|
258
+ config_assignments(file).each do |hit|
259
+ path = hit[:path]
260
+ next unless hit[:assignment] && path.size == 2 && path.first == :active_job && QUEUE_SETTINGS.include?(path.last)
261
+
262
+ settings[path.last] = queue_setting(path.last, hit)
263
+ end
264
+ end
265
+ end
266
+
267
+ # Only a string or symbol written as one is the value Rails sees.
268
+ def queue_setting(key, hit)
269
+ literal = hit[:literal]
270
+ text = literal ? hit[:value].to_s : "`#{hit[:source]}`"
271
+ note = if hit[:condition] then "#{key} set only when `#{hit[:condition]}`"
272
+ elsif !literal then "computed"
273
+ end
274
+ { text: text, note: note }
275
+ end
276
+
277
+ def config_assignments(file)
278
+ Array(config_walk(file)[:config])
279
+ end
280
+
281
+ CONFIG_FILE_LISTENERS = {
282
+ config: Listeners::ConfigAssignmentListener,
283
+ calls: -> { Listeners::MethodCallListener.new(names: REGISTER_CALLS.keys + %w[load_defaults]) },
284
+ previews: -> { Listeners::PreviewPathsListener.new(framework: :action_mailer) }
285
+ }.freeze
286
+
287
+ # One walk of a config file per run, shared by the queue settings, the GoodJob cron and the mailer settings.
288
+ def config_walk(relative)
289
+ @config_file_walks ||= {}
290
+ @config_file_walks.fetch(relative) do
291
+ source = config_source(relative)
292
+ @config_file_walks[relative] = source ? SourceIntrospector.walk_source(source, CONFIG_FILE_LISTENERS) : {}
293
+ end
294
+ end
295
+
296
+ def config_source(relative)
297
+ @config_sources ||= {}
298
+ @config_sources.fetch(relative) { @config_sources[relative] = SafePath.read(relative, under: app.root.to_s).first }
167
299
  end
168
300
 
169
301
  def sidekiq_options(macros)
@@ -175,13 +307,65 @@ module RailsAiContext
175
307
  # that reaches neither ActiveJob nor a Sidekiq mixin is no job and no job base.
176
308
  def base?(name)
177
309
  SuperclassChain.abstract_base?(name, inherited: inherited_names.include?(name)) &&
178
- (worker?(name) || active_job?(name))
310
+ (worker?(name) || active_job?(name) || que_job?(name))
179
311
  end
180
312
 
181
313
  def reflected_bases
182
314
  @reflected_bases ||= []
183
315
  end
184
316
 
317
+ # delayed_job's handle_asynchronously wraps a model method so every call is
318
+ # queued; the app has no job class for it at all. Called from a module, it
319
+ # wraps the method on each model that includes the module.
320
+ def async_methods
321
+ found = []
322
+ modules = {}
323
+ sources = []
324
+ model_sources(skip_concerns: false) do |record|
325
+ sources << [ record.path, record.source ]
326
+ next unless record.source.include?("handle_asynchronously")
327
+
328
+ # app/models/concerns is an autoload root of its own.
329
+ owner = DeclaredConstant.resolve(record.source, record.path_name.delete_prefix("Concerns::"))
330
+ calls = async_calls(record)
331
+ DeclaredConstant.declared_names(record.source, path_name: owner).include?(owner) ? found.concat(calls.map { |call| { owner: owner, **call } }) : modules[owner] = calls
332
+ end
333
+ found + mixin_async_methods(sources, modules)
334
+ end
335
+
336
+ # A concern that includes the concern runs its `included` block on its own includers, so
337
+ # the walk follows each module includer until it reaches classes.
338
+ def mixin_async_methods(sources, modules)
339
+ return [] if modules.empty?
340
+
341
+ mixins = sources.flat_map { |_path, source| DeclaredConstant.declared_module_names(source) }.to_set -
342
+ sources.flat_map { |_path, source| DeclaredConstant.declared_names(source) }
343
+ found = []
344
+ seen = modules.keys.to_set
345
+ until modules.empty?
346
+ reached = Hash.new { |hash, key| hash[key] = [] }
347
+ Includers.of(app.root, sources, modules.keys, macros: %i[include prepend]).each do |name, includers|
348
+ includers.uniq.each do |includer|
349
+ next reached[includer].concat(modules[name]) if mixins.include?(includer)
350
+
351
+ found.concat(modules[name].map { |call| { owner: includer, **call } })
352
+ end
353
+ end
354
+ modules = reached.reject { |name, _| seen.include?(name) }
355
+ seen.merge(modules.keys)
356
+ end
357
+ found.uniq
358
+ end
359
+
360
+ def async_calls(record)
361
+ walked = SourceIntrospector.walk_source(record.source, { calls: -> { Listeners::GenericMacroListener.new(:handle_asynchronously) } })
362
+ walked[:calls].filter_map do |call|
363
+ method = call[:args].first or next
364
+ options = call[:option_values].map { |key, value| "#{key}: #{value}" }
365
+ { method: method.to_s, file: "#{record.file}:#{call[:location]}", options: options.join(", ").presence }.compact
366
+ end
367
+ end
368
+
185
369
  ENQUEUE_HELPERS = %w[enqueue enqueue_in enqueue_at].freeze
186
370
 
187
371
  # An app that enqueues through its own helper (`Jobs.enqueue(:process_post)`) names a
@@ -237,35 +421,90 @@ module RailsAiContext
237
421
  next if name == "ApplicationJob" || worker?(name) || base?(name)
238
422
 
239
423
  ast = candidate.ast
240
- unknown_base = !active_job?(name)
424
+ active = active_job?(name)
425
+ que = !active && que_job?(name)
426
+ unknown_base = !active && !que
241
427
  next if unknown_base && !performs?(ActionResolver.own_methods(ast[:methods], candidate.declared))
242
428
 
243
- queue = inherited_queue(name)
429
+ queue = inherited_queue(name) || (queue_name_from_part(nil) if active)
244
430
 
245
- perform_method = ast[:methods].find { |m| m[:name] == "perform" && m[:scope] == :instance }
431
+ perform_method = ActionResolver.entry_point(ast[:methods], names: que ? ActionResolver::QUE_ENTRY_POINTS : ActionResolver::ENTRY_POINTS)
246
432
  perform_signature = ActionResolver.parameter_list(perform_method) if perform_method && perform_method[:params]&.any?
247
433
 
248
- # Extract job callbacks
249
- callback_names = %i[before_enqueue after_enqueue before_perform after_perform around_perform around_enqueue]
250
- callbacks = ast[:macros]
251
- .select { |m| callback_names.include?(m[:macro]) }
252
- .map { |m| m[:macro].to_s }
253
- .uniq
254
-
255
434
  retries = RetryPolicy.entries(ast[:macros])
256
435
 
257
436
  job = { name: name, file: candidate.file }
437
+ job[:active_job] = true if active
258
438
  job[:unknown_base] = true if unknown_base
439
+ job[:que] = true if que
259
440
  job[:queue] = queue if queue
260
441
  job[:retries] = retries if retries.any?
261
442
  job[:perform_signature] = perform_signature if perform_signature
262
- job[:callbacks] = callbacks if callbacks.any?
443
+ job[:entry_point] = perform_method[:name] if perform_method && perform_method[:name] != "perform"
444
+ job.merge!(run_settings(name))
263
445
  job
264
446
  end.sort_by { |j| j[:name] }
265
447
  rescue => e
266
448
  RailsAiContext.debug_fail(e, [], label: "extract_jobs_from_source")
267
449
  end
268
450
 
451
+ # What decides when a job runs, as its class (or the nearest base setting it) writes it.
452
+ def run_settings(name)
453
+ candidate = job_candidates[name]
454
+ macros = candidate_body(name)
455
+ settings = {}
456
+ priority = nearest_macro(name, :queue_with_priority)
457
+ if priority
458
+ value = priority[:values].first
459
+ settings[:priority] = if priority[:block] then labelled(COMPUTED_PRIORITY, priority[:block])
460
+ elsif value.is_a?(String) && value.match?(PROC_LITERAL) then labelled(COMPUTED_PRIORITY, value)
461
+ else value
462
+ end
463
+ end
464
+ commit = nearest_macro(name, :enqueue_after_transaction_commit=)
465
+ settings[:enqueue_after_transaction_commit] = commit[:values].first.then { |v| v.is_a?(Symbol) ? ":#{v}" : v.to_s } if commit
466
+ concurrency = nearest_macro(name, :limits_concurrency)
467
+ settings[:concurrency] = written(job_candidates[concurrency[:owner]].source, concurrency) if concurrency
468
+ callbacks = macros.select { |m| JOB_CALLBACKS.include?(m[:macro]) }.sort_by { |m| m[:offset] }
469
+ settings[:callbacks] = callbacks.map { |m| written(candidate.source, m) } if callbacks.any?
470
+ settings.merge(continuation(name))
471
+ end
472
+
473
+ CONTINUABLE = "ActiveJob::Continuable"
474
+
475
+ # ActiveJob::Continuable (Rails 8.1) resumes a retried job at its first
476
+ # unfinished step, so the steps perform runs, in order, decide what runs again.
477
+ def continuation(name)
478
+ continuable = chain_of(name).any? do |link|
479
+ candidate_body(link).any? { |m| m[:macro] == :include && m[:values].map { |v| v.to_s.delete_prefix("::") }.include?(CONTINUABLE) }
480
+ end
481
+ return {} unless continuable
482
+
483
+ ast = job_candidates[name].ast
484
+ perform = ActionResolver.entry_point(ast[:methods])
485
+ steps = ast[:macros].select do |m|
486
+ m[:macro] == :step && m[:args].any? && perform && (perform[:offset]...perform[:end_offset]).cover?(m[:offset])
487
+ end
488
+ { continuable: true, steps: steps.sort_by { |m| m[:offset] }.map { |m| step_entry(m) } }
489
+ end
490
+
491
+ def step_entry(macro)
492
+ step = { name: macro[:args].first.to_s, runs: macro[:proc_lines].any? ? "block" : "method" }
493
+ options = macro[:option_values].slice(:isolated, :start)
494
+ step[:options] = options.map { |key, value| "#{key}: #{value}" }.join(", ") if options.any?
495
+ step
496
+ end
497
+
498
+ COMPUTED_PRIORITY = "computed by a block"
499
+
500
+ def nearest_macro(name, macro)
501
+ chain_of(name).each do |link|
502
+ hit = candidate_body(link).reverse.find { |m| m[:macro] == macro }
503
+ return hit.merge(owner: link) if hit
504
+ end
505
+ nil
506
+ end
507
+
269
508
  # Sidekiq workers, read from source in both tiers: the class is not an
270
509
  # ActiveJob descendant, so reflection has no list to walk.
271
510
  WORKER_MIXINS = %w[Sidekiq::Job Sidekiq::Worker Sidekiq::IterableJob].freeze
@@ -290,7 +529,7 @@ module RailsAiContext
290
529
  options: inherited_options(name),
291
530
  inherited_from: option_sources(name).presence,
292
531
  throttle: throttle && throttle_summary(throttle),
293
- entry_point: entry && entry[:name] == "execute" ? "execute" : nil,
532
+ entry_point: entry && entry[:name] != "perform" ? entry[:name] : nil,
294
533
  perform_signature: entry && entry[:params]&.any? ? ActionResolver.parameter_list(entry) : nil,
295
534
  retries: retries.any? ? retries : nil,
296
535
  calls: calls.any? ? calls : nil
@@ -302,14 +541,14 @@ module RailsAiContext
302
541
 
303
542
  # One class file. `declares` carries every constant in it, so a subclass
304
543
  # elsewhere can find its base here.
305
- Candidate = Data.define(:file, :source, :declared, :declares, :superclass, :ast)
544
+ Candidate = Data.define(:file, :source, :declared, :declares, :superclass, :nesting, :ast)
306
545
 
307
546
  # Every class in every job directory, one walk each. The kind of job waits for the
308
547
  # whole map: the mixin or ActiveJob ancestry is often on a base in another file.
309
548
  def job_candidates
310
549
  @job_candidates ||= JOB_DIRS.each_with_object({}) do |kind, found|
311
550
  SourceScan.each(app.root, kind: kind) do |record|
312
- declarations = DeclaredConstant.declarations(record.source)
551
+ declarations = DeclaredConstant.declarations(record.source, path_name: record.path_name)
313
552
  next if declarations.empty?
314
553
 
315
554
  declaration = DeclaredConstant.declaration_for(declarations, record.path_name) || declarations.first
@@ -321,11 +560,11 @@ module RailsAiContext
321
560
  declared: declaration.name,
322
561
  declares: declarations.map(&:name),
323
562
  superclass: declaration.superclass,
324
- ast: SourceIntrospector.walk_source(record.source, {
325
- macros: -> { Listeners::GenericMacroListener.new(*JOB_MACROS) },
563
+ nesting: declaration.nesting,
564
+ ast: SourceIntrospector.walk_source(record.source, QUEUE_AS_LISTENERS.merge(
326
565
  methods: Listeners::MethodsListener,
327
566
  calls: SourceCalls.listener
328
- })
567
+ ))
329
568
  )
330
569
  end
331
570
  end
@@ -352,25 +591,34 @@ module RailsAiContext
352
591
  def superclass_of(name)
353
592
  candidate = job_candidates[name] or return nil
354
593
 
355
- SuperclassChain.resolve_in_scope(candidate.declared, candidate.superclass) do |qualified|
594
+ SuperclassChain.resolve_in_scope(candidate.declared, candidate.superclass, nesting: candidate.nesting) do |qualified|
356
595
  candidate_names[qualified]
357
596
  end
358
597
  end
359
598
 
360
599
  # Whether the chain reaches ActiveJob. The job directories answer first, since a base
361
600
  # there is often namespace-relative; anything else goes through the autoload roots.
362
- def active_job?(name, seen = [])
601
+ def active_job?(name)
602
+ reaches?(name, ACTIVE_JOB_BASES)
603
+ end
604
+
605
+ # Que jobs subclass Que::Job and define run rather than perform.
606
+ def que_job?(name)
607
+ reaches?(name, QUE_JOB_BASES)
608
+ end
609
+
610
+ def reaches?(name, bases, seen = [])
363
611
  return false if seen.include?(name)
364
612
 
365
613
  candidate = job_candidates[name] or return false
366
614
  parent = candidate.superclass or return false
367
- return true if ACTIVE_JOB_BASES.include?(parent)
615
+ return true if bases.include?(parent)
368
616
 
369
617
  resolved = superclass_of(name)
370
- return active_job?(resolved, seen + [ name ]) if resolved
618
+ return reaches?(resolved, bases, seen + [ name ]) if resolved
371
619
 
372
620
  @superclass_lookup ||= SuperclassChain.lookup_for(app.root)
373
- SuperclassChain.to(candidate.source, bases: ACTIVE_JOB_BASES,
621
+ SuperclassChain.to(candidate.source, bases: bases,
374
622
  lookup: @superclass_lookup, only: candidate.declared).any?
375
623
  end
376
624
 
@@ -412,23 +660,8 @@ module RailsAiContext
412
660
  worker?(parent, seen + [ name ])
413
661
  end
414
662
 
415
- def extract_solid_queue_recurring
416
- paths = [
417
- File.join(app.root, "config", "recurring.yml"),
418
- File.join(app.root, "config", "solid_queue.yml")
419
- ]
420
- path = paths.find { |p| File.exist?(p) }
421
- return [] unless path
422
-
423
- content = RailsAiContext::SafeFile.read(path)
424
- return [] unless content
425
- jobs = []
426
- content.scan(/(\w+):\s*\n\s+class:\s*(\w+).*?(?:schedule:\s*["']?([^"'\n]+))?/m) do |name, klass, schedule|
427
- jobs << { name: name, class: klass, schedule: schedule&.strip }.compact
428
- end
429
- jobs
430
- rescue => e
431
- RailsAiContext.debug_fail(e, [], label: "extract_solid_queue_recurring")
663
+ def recurring_jobs
664
+ RecurringSchedules.read(app.root, ->(file) { config_source(file)&.include?("good_job") ? config_assignments(file) : [] })
432
665
  end
433
666
 
434
667
  # Read as YAML so a path in a comment is no queue. Sidekiq's keys may be symbols or
@@ -447,6 +680,36 @@ module RailsAiContext
447
680
  RailsAiContext.debug_fail(e, nil, label: "extract_sidekiq_config")
448
681
  end
449
682
 
683
+ SOLID_QUEUE_FILE = "config/queue.yml"
684
+
685
+ # The queues Solid Queue's workers poll, from this environment's section or the
686
+ # whole file; a worker that names none polls every queue, as QueueSelector reads it.
687
+ def extract_solid_queue_config
688
+ return nil unless solid_queue_adapter?
689
+
690
+ data = ConfigYaml.read(app.root, SOLID_QUEUE_FILE, label: "Solid Queue", marker: ConfigYaml::ERB_OUTPUT)
691
+ return nil unless data.is_a?(Hash)
692
+
693
+ section = data[RailsAiContext.environment_name].is_a?(Hash) ? data[RailsAiContext.environment_name] : data
694
+ workers = Array(section["workers"]).select { |worker| worker.is_a?(Hash) }
695
+ return nil if workers.empty?
696
+
697
+ queues = workers.flat_map { |worker| Array(worker["queues"]).map { |queue| queue.to_s.strip }.presence || [ "*" ] }
698
+ computed, named = queues.uniq.partition { |queue| ConfigYaml.marked?(queue) }
699
+ config = { file: SOLID_QUEUE_FILE, queues: named }
700
+ config[:queues_computed] = true if computed.any?
701
+ config
702
+ end
703
+
704
+ # Read from source in both tiers: production's adapter is the one queue.yml is for,
705
+ # whatever the running environment uses. With no adapter in the config, the Gemfile decides.
706
+ def solid_queue_adapter?
707
+ adapters = app_config_files.flat_map do |relative|
708
+ config_assignments(relative).select { |hit| hit[:assignment] && hit[:path] == %i[active_job queue_adapter] }
709
+ end.map { |hit| hit[:value].to_s }
710
+ adapters.any? ? adapters.include?("solid_queue") : GemfileGems.names(app.root).include?("solid_queue")
711
+ end
712
+
450
713
  def sidekiq_yml
451
714
  path = File.join(app.root, "config", "sidekiq.yml")
452
715
  return nil unless File.exist?(path)
@@ -518,12 +781,13 @@ module RailsAiContext
518
781
  next
519
782
  end
520
783
 
784
+ file = source_file_for(mailer)
521
785
  entry = {
522
786
  name: mailer.name,
523
- file: source_file_for(mailer),
787
+ file: file,
524
788
  actions: actions,
525
789
  delivery_method: mailer.delivery_method.to_s
526
- }.compact
790
+ }.compact.merge(mailer_extras(mailer.name, file))
527
791
  actions.any? ? entry : entry.merge(no_action_detail(mailer))
528
792
  end.sort_by { |m| m[:name] }
529
793
  rescue => e
@@ -584,6 +848,7 @@ module RailsAiContext
584
848
  before_action after_action around_action
585
849
  prepend_before_action prepend_after_action prepend_around_action
586
850
  append_before_action append_after_action append_around_action
851
+ before_deliver after_deliver around_deliver
587
852
  ].freeze
588
853
 
589
854
  def source_mailers
@@ -593,7 +858,7 @@ module RailsAiContext
593
858
  mailers_outside_mailer_dirs(scanned.map(&:file))
594
859
  # A mixin under app/mailers holds a mailer's actions (an Emails::* module); a class
595
860
  # is a mailer only when its chain reaches one.
596
- parent_of = candidates.reject(&:mixin).to_h { |klass| [ klass.name, klass.parent_class ] }
861
+ parent_of = candidates.reject(&:mixin).to_h { |klass| [ klass.name, [ klass.parent_class, klass.nesting ] ] }
597
862
  klasses = candidates.select { |klass| klass.mixin || reaches_mailer?(klass.name, parent_of) }
598
863
  bases = abstract_mailer_bases(klasses.reject(&:mixin))
599
864
  mailers = klasses.reject { |klass| bases.include?(klass.name) }
@@ -614,13 +879,14 @@ module RailsAiContext
614
879
  end
615
880
 
616
881
  # ActionMailer::Base, or a gem parent the app does not define whose name ends in
617
- # Mailer. An app parent is followed through its autoload roots, cached in `parent_of`.
882
+ # Mailer. An app parent is followed through its autoload roots, cached in `parent_of`
883
+ # as [parent, the nesting it is read in].
618
884
  def reaches_mailer?(name, parent_of, seen = [])
619
- parent = parent_of[name]
885
+ parent, nesting = parent_of[name]
620
886
  return false if parent.nil? || seen.include?(name) || seen.size >= SuperclassChain::MAX_DEPTH
621
887
  return true if parent == MAILER_BASE
622
888
 
623
- app_parent = SuperclassChain.resolve_in_scope(name, parent) { |candidate| candidate if app_class?(candidate, parent_of) }
889
+ app_parent = SuperclassChain.resolve_in_scope(name, parent, nesting: nesting) { |candidate| candidate if app_class?(candidate, parent_of) }
624
890
  return reaches_mailer?(app_parent, parent_of, seen + [ name ]) if app_parent
625
891
 
626
892
  parent.split("::").last.end_with?("Mailer")
@@ -632,10 +898,10 @@ module RailsAiContext
632
898
  return true if parent_of.key?(name)
633
899
 
634
900
  source = mailer_lookup.call(name) or return false
635
- declaration = DeclaredConstant.declaration_named(DeclaredConstant.declarations(source), name)
901
+ declaration = DeclaredConstant.declaration_named(DeclaredConstant.declarations(source, path_name: name), name)
636
902
  return false unless declaration
637
903
 
638
- parent_of[name] = declaration.superclass
904
+ parent_of[name] = [ declaration.superclass, declaration.nesting ]
639
905
  true
640
906
  end
641
907
 
@@ -643,12 +909,169 @@ module RailsAiContext
643
909
  @mailer_lookup ||= SuperclassChain.lookup_for(app.root)
644
910
  end
645
911
 
912
+ # A mailer's own declarations as written, the template formats of each action, and
913
+ # its preview class: read from source in both tiers.
914
+ def mailer_extras(name, file, source = nil, macros = nil, methods = nil)
915
+ source ||= file && SafeFile.read(File.join(app.root.to_s, file))
916
+ extras = {}
917
+ if source
918
+ unless macros
919
+ walked = SourceIntrospector.walk_source(source, {
920
+ macros: -> { Listeners::GenericMacroListener.new(ACTION_CALLBACKS + MAILER_DECLARATIONS) },
921
+ methods: Listeners::MethodsListener
922
+ })
923
+ macros, methods = walked.values_at(:macros, :methods)
924
+ end
925
+ declares = SourceIntrospector.outside_defs(macros, methods).select { |m| (MAILER_DECLARATIONS + ACTION_CALLBACKS).include?(m[:macro]) }
926
+ .sort_by { |m| m[:offset] }.map { |m| written(source, m) }
927
+ extras[:declares] = declares if declares.any?
928
+ end
929
+ templates = mailer_templates(name)
930
+ extras[:templates] = templates if templates.any?
931
+ extras[:preview] = mailer_previews[name] if mailer_previews[name]
932
+ extras
933
+ end
934
+
935
+ # welcome.html.erb and welcome.text.erb are one action in two formats; a partial is no action.
936
+ def mailer_templates(name)
937
+ dir = File.join(app.root.to_s, "app/views", name.underscore)
938
+ Dir.glob(File.join(dir, "*")).select { |path| File.file?(path) }.each_with_object({}) do |path, found|
939
+ action, *middle, handler = File.basename(path).split(".")
940
+ next if action.start_with?("_") || handler.nil?
941
+
942
+ (found[action] ||= []) << (template_format(middle) || "any format")
943
+ end.transform_values { |formats| formats.uniq.sort }.sort.to_h
944
+ end
945
+
946
+ # action.locale.format+variant.handler: the format is the last part, and a lone
947
+ # part that reads as a locale and names no format is the locale.
948
+ def template_format(middle)
949
+ format = middle.last&.sub(/\+.*\z/, "")
950
+ return nil if format.blank?
951
+ return nil if middle.one? && format.match?(ViewFile::LOCALE) && !ViewFile::FORMAT_EXTENSIONS.include?(format)
952
+
953
+ format
954
+ end
955
+
956
+ DEFAULT_MAILER_PREVIEW_DIRS = %w[test/mailers/previews spec/mailers/previews].freeze
957
+ MAILER_CONFIG_GLOBS = %w[config/application.rb config/environments/*.rb].freeze
958
+
959
+ # Rails adds test/mailers/previews and rspec-rails spec/mailers/previews; the app adds the rest.
960
+ def mailer_preview_dirs
961
+ @mailer_preview_dirs ||= begin
962
+ configured = mailer_config_walks.flat_map { |_relative, walked| Array(walked[:previews]) }
963
+ (DEFAULT_MAILER_PREVIEW_DIRS + configured).uniq.select { |dir| Dir.exist?(File.join(app.root.to_s, dir)) }
964
+ end
965
+ end
966
+
967
+ def app_config_files
968
+ @app_config_files ||= MAILER_CONFIG_GLOBS.flat_map { |glob| Dir.glob(File.join(app.root.to_s, glob)).sort }.map { |path| path.delete_prefix("#{app.root}/") }
969
+ end
970
+
971
+ def mailer_config_files
972
+ @mailer_config_files ||= app_config_files.map { |relative| File.join(app.root.to_s, relative) } + PathResolver.initializer_paths(app.root)
973
+ end
974
+
975
+ # Only a file that mentions mail settings is walked; one walked already for another reader is reused.
976
+ def mailer_config_walks
977
+ @mailer_config_walks ||= mailer_config_files.filter_map do |path|
978
+ relative = path.delete_prefix("#{app.root}/")
979
+ next unless @config_file_walks&.key?(relative) || config_source(relative)&.match?(/action_mailer|register_(?:interceptor|observer)|load_defaults/)
980
+ [ relative, config_walk(relative) ]
981
+ end
982
+ end
983
+
984
+ # Mailer name => its preview class, file and the emails it previews.
985
+ def mailer_previews
986
+ @mailer_previews ||= mailer_preview_dirs.flat_map { |dir| Dir.glob(File.join(app.root.to_s, dir, "**/*_preview.rb")).sort }
987
+ .each_with_object({}) do |path, found|
988
+ next unless SafePath.contained?(File.realpath(path), app_root_real)
989
+
990
+ source = SafeFile.read(path) or next
991
+ declared = DeclaredConstant.declarations(source, path_name: File.basename(path, ".rb").camelize).find { |d| d.name.end_with?("Preview") } or next
992
+ methods = SourceIntrospector.walk_source(source, { methods: Listeners::MethodsListener })[:methods]
993
+ previews = ActionResolver.own_methods(methods, declared.name)
994
+ .select { |m| m[:scope] == :instance && m[:visibility] == :public }.map { |m| m[:name] }
995
+ found[declared.name.delete_suffix("Preview")] ||= { name: declared.name, file: path.delete_prefix("#{app.root}/"), methods: previews }
996
+ rescue SystemCallError
997
+ next
998
+ end
999
+ end
1000
+
1001
+ REGISTER_CALLS = { "register_interceptor" => :interceptors, "register_interceptors" => :interceptors,
1002
+ "register_observer" => :observers, "register_observers" => :observers }.freeze
1003
+
1004
+ # Statically the deliver_later queue is the config's, else nil under load_defaults 6.1+ (ActiveJob's default).
1005
+ def mailer_settings(mailers, booted: false)
1006
+ settings = { interceptors: [], observers: [] }
1007
+ queue = nil
1008
+ queue_set = false
1009
+ version = nil
1010
+ mailer_config_walks.each do |relative, walked|
1011
+ Array(walked[:config]).each do |hit|
1012
+ next unless hit[:assignment] && hit[:path].first == :action_mailer && hit[:path].size == 2
1013
+
1014
+ key = hit[:path].last
1015
+ if key == :deliver_later_queue_name && in_this_environment?(relative)
1016
+ queue_set = true
1017
+ queue = hit[:value]&.to_s
1018
+ end
1019
+ settings[key].concat(registered_all(hit, relative)) if settings.key?(key)
1020
+ end
1021
+ Array(walked[:calls]).each do |call|
1022
+ if call[:name] == "load_defaults"
1023
+ version = defaults_version(call[:arguments].first) if relative == "config/application.rb"
1024
+ else
1025
+ settings[REGISTER_CALLS[call[:name]]].concat(registered_all(call, relative))
1026
+ end
1027
+ end
1028
+ end
1029
+ if booted && defined?(ActionMailer::Base) && ActionMailer::Base.respond_to?(:deliver_later_queue_name)
1030
+ queue = ActionMailer::Base.deliver_later_queue_name&.to_s
1031
+ elsif !queue_set
1032
+ queue = version && version >= 6.1 ? nil : "mailers"
1033
+ end
1034
+ settings.transform_values! { |list| list.uniq { |entry| entry[:name] } }
1035
+ queue = Array(mailers).any? ? queue_name_from_part(queue.presence) : nil
1036
+ settings.merge(deliver_later_queue: queue, preview_paths: mailer_preview_dirs)
1037
+ end
1038
+
1039
+ # Each argument of a register call or element of a config list, read off the listener's node facts.
1040
+ def registered_all(hit, relative)
1041
+ computed = Array(hit[:computed])
1042
+ constants = hit[:constants] || {}
1043
+ Array(hit[:arguments]).flatten.map { |name| registered(name, relative, computed: computed, constants: constants) }
1044
+ end
1045
+
1046
+ # A symbol, string or constant name is the class ActionMailer camelizes it to.
1047
+ def registered(name, relative, computed: [], constants: {})
1048
+ text = name.to_s
1049
+ return { name: constants[text], file: relative } if constants[text]
1050
+ return { name: text.camelize.delete_prefix("::"), file: relative } if name.is_a?(Symbol) || (literal_name?(text) && !computed.include?(text))
1051
+
1052
+ { name: text, file: relative, unresolved: true }
1053
+ end
1054
+
1055
+ # A string value's characters, not Ruby source.
1056
+ def literal_name?(text)
1057
+ text.match?(%r{\A(?:::)?\w+(?:(?:/|::)\w+)*\z})
1058
+ end
1059
+
1060
+ # A version that is not a literal is the running Rails's, past every cutoff.
1061
+ def defaults_version(arg)
1062
+ arg.to_s.match?(/\A\d+(\.\d+)?\z/) ? arg.to_s.to_f : Float::INFINITY
1063
+ end
1064
+
1065
+ def in_this_environment?(relative)
1066
+ !relative.start_with?("config/environments/") || relative == "config/environments/#{RailsAiContext.environment_name}.rb"
1067
+ end
1068
+
646
1069
  # What a base hands every mailer inheriting it, each written the way the
647
1070
  # app wrote it, the methods it defines, and who inherits it.
648
1071
  MAILER_DECLARATIONS = %i[layout helper helper_method include default default_url_options=].freeze
649
1072
 
650
1073
  def mailer_base_record(name, file, source, macros, methods, heirs)
651
- declares = class_body(macros, source).select { |m| (MAILER_DECLARATIONS + ACTION_CALLBACKS).include?(m[:macro]) }
1074
+ declares = SourceIntrospector.outside_defs(macros, methods).select { |m| (MAILER_DECLARATIONS + ACTION_CALLBACKS).include?(m[:macro]) }
652
1075
  .sort_by { |m| m[:offset] }.map { |m| written(source, m) }
653
1076
  defined = ActionResolver.own_methods(methods, name).select { |m| m[:scope] == :instance }.map { |m| m[:name] }.uniq
654
1077
  { name: name, file: file, declares: declares.presence, methods: defined.presence,
@@ -671,17 +1094,16 @@ module RailsAiContext
671
1094
 
672
1095
  # A declaration is a call in the class body; the same name inside a
673
1096
  # method - `default[:from]` in a `class << self` reader - is a use.
674
- def class_body(macros, source)
675
- root = AstCache.parse_string(source)&.value or return macros
676
- outside = SourceIntrospector.calls_outside_methods(root, self_receiver: true).values.flatten
677
- .map { |call| call.location.start_offset }
678
- macros.select { |m| outside.include?(m[:offset]) }
1097
+ def candidate_body(name)
1098
+ @candidate_bodies ||= {}
1099
+ @candidate_bodies[name] ||= SourceIntrospector.outside_defs(job_candidates[name].ast[:macros], job_candidates[name].ast[:methods])
679
1100
  end
680
1101
 
681
1102
  def inherits_from?(name, base, parent_of, seen = [])
682
1103
  return false if seen.include?(name) || seen.size >= SuperclassChain::MAX_DEPTH
683
1104
 
684
- parent = SuperclassChain.resolve_in_scope(name, parent_of[name]) { |candidate| candidate if parent_of.key?(candidate) }
1105
+ superclass, nesting = parent_of[name]
1106
+ parent = SuperclassChain.resolve_in_scope(name, superclass, nesting: nesting) { |candidate| candidate if parent_of.key?(candidate) }
685
1107
  return false unless parent
686
1108
  return true if parent == base
687
1109
 
@@ -695,7 +1117,7 @@ module RailsAiContext
695
1117
  return [] if named.empty?
696
1118
 
697
1119
  inherited = klasses.filter_map do |klass|
698
- SuperclassChain.resolve_in_scope(klass.name, klass.parent_class) do |candidate|
1120
+ SuperclassChain.resolve_in_scope(klass.name, klass.parent_class, nesting: klass.nesting) do |candidate|
699
1121
  candidate if named.include?(candidate)
700
1122
  end
701
1123
  end
@@ -710,10 +1132,10 @@ module RailsAiContext
710
1132
  declarations = DeclaredConstant.declarations(record.source).select(&:superclass)
711
1133
  next [] if declarations.empty?
712
1134
 
713
- klass = walk_class(record, ACTION_CALLBACKS)
1135
+ klass = walk_class(record, ACTION_CALLBACKS + MAILER_DECLARATIONS)
714
1136
  next [] if klass.nil? || framework_hook?(klass)
715
1137
 
716
- declarations.map { |d| klass.with(name: d.name, parent_class: d.superclass, mixin: false) }
1138
+ declarations.map { |d| klass.with(name: d.name, parent_class: d.superclass, nesting: d.nesting, mixin: false) }
717
1139
  end
718
1140
  end
719
1141
 
@@ -744,7 +1166,7 @@ module RailsAiContext
744
1166
  )
745
1167
 
746
1168
  entry = { name: klass.name, file: klass.file, actions: actions,
747
- confidence: RailsAiContext::Confidence::STATIC }
1169
+ confidence: RailsAiContext::Confidence::STATIC }.merge(mailer_extras(klass.name, klass.file, klass.source, klass.macros, klass.methods))
748
1170
  return entry if actions.any?
749
1171
 
750
1172
  class_actions = ActionResolver.own_methods(klass.methods, klass.name)
@@ -758,18 +1180,38 @@ module RailsAiContext
758
1180
  def extract_channels_from_source
759
1181
  # ApplicationCable holds the base Channel and Connection, neither of
760
1182
  # which is a channel of the app's own.
761
- source_classes("app/channels").filter_map do |klass|
1183
+ channel_sources.filter_map do |klass|
762
1184
  next if klass.name.start_with?("ApplicationCable::")
763
1185
 
764
- stream_methods = ActionResolver.own_methods(klass.methods, klass.name)
765
- .select { |m| m[:scope] == :instance }
766
- .map { |m| m[:name] }
767
- .select { |m| m.start_with?("stream_") || m == "subscribed" }
768
- { name: klass.name, file: klass.file, stream_methods: stream_methods,
769
- confidence: RailsAiContext::Confidence::STATIC }
1186
+ own = ActionResolver.own_methods(klass.methods, klass.name).select { |m| m[:scope] == :instance }
1187
+ names = own.map { |m| m[:name] }
1188
+ actions = own.select { |m| m[:visibility] == :public }.map { |m| m[:name] }
1189
+ .reject { |m| CHANNEL_LIFECYCLE.include?(m) || m.start_with?("stream_") }
1190
+ { name: klass.name, file: klass.file,
1191
+ stream_methods: names.select { |m| m.start_with?("stream_") || m == "subscribed" },
1192
+ streams: extract_channel_streams(klass.macros),
1193
+ periodic: extract_channel_periodic(klass.macros),
1194
+ actions: actions.empty? ? nil : actions.uniq.sort,
1195
+ confidence: RailsAiContext::Confidence::STATIC }.compact
770
1196
  end.sort_by { |c| c[:name] }
771
1197
  end
772
1198
 
1199
+ CHANNEL_LIFECYCLE = %w[subscribed unsubscribed].freeze
1200
+
1201
+ # `identified_by` is a Connection macro: what every channel reads as its own
1202
+ # `current_user`. Read from source in both tiers, so both give one answer.
1203
+ def extract_connections
1204
+ channel_sources.filter_map do |klass|
1205
+ ids = extract_identified_by(klass.macros) or next
1206
+ { name: klass.name, file: klass.file, identified_by: ids }
1207
+ end.sort_by { |c| c[:name] }
1208
+ end
1209
+
1210
+ # One walk of app/channels, shared by channels, connections and the booted tier.
1211
+ def channel_sources
1212
+ @channel_sources ||= source_classes("app/channels", macros: CHANNEL_MACROS)
1213
+ end
1214
+
773
1215
  # Class name, method list and file for every .rb of a kind, read from
774
1216
  # the AST. Concerns stay out: Rails adds app/*/concerns as its own
775
1217
  # autoload root, so what lives there is a mixin, not a mailer or a
@@ -789,7 +1231,7 @@ module RailsAiContext
789
1231
  end
790
1232
  end
791
1233
 
792
- SourceClass = Data.define(:name, :methods, :file, :macros, :parent_class, :mixin, :source)
1234
+ SourceClass = Data.define(:name, :methods, :file, :macros, :parent_class, :nesting, :mixin, :source)
793
1235
 
794
1236
  # The methods are the file's, since a mailer's actions are often on a mixin; a file
795
1237
  # declaring a mailer and an interceptor is read as one, and the hook drops both.
@@ -798,12 +1240,13 @@ module RailsAiContext
798
1240
  listeners[:macros] = -> { Listeners::GenericMacroListener.new(macros) } if macros.any?
799
1241
  walked = SourceIntrospector.walk_source(record.source, listeners)
800
1242
 
801
- declarations = DeclaredConstant.declarations(record.source)
1243
+ declarations = DeclaredConstant.declarations(record.source, path_name: record.path_name)
802
1244
  name = declarations.map(&:name).find { |n| n.casecmp?(record.path_name) } ||
803
1245
  DeclaredConstant.resolve(record.source, record.path_name)
1246
+ own = declarations.find { |d| d.name == name }
804
1247
  SourceClass.new(name: name, methods: walked[:methods] || [], file: record.file,
805
1248
  macros: walked[:macros] || [],
806
- parent_class: declarations.find { |d| d.name == name }&.superclass,
1249
+ parent_class: own&.superclass, nesting: own&.nesting,
807
1250
  mixin: declarations.empty?, source: record.source)
808
1251
  rescue StandardError, ScriptError => e
809
1252
  RailsAiContext.debug_fail(e, nil, label: "source_classes for #{record.path}")
@@ -836,11 +1279,13 @@ module RailsAiContext
836
1279
  ActionCable::Channel::Base.descendants.filter_map do |channel|
837
1280
  next if channel.name.nil? || channel.name == "ApplicationCable::Channel"
838
1281
 
839
- macros = channel_macros(channel_source(channel))
1282
+ file = source_file_for(channel)
1283
+ macros = channel_sources.find { |klass| klass.file == file }&.macros ||
1284
+ channel_macros(channel_source(channel))
840
1285
 
841
1286
  {
842
1287
  name: channel.name,
843
- file: source_file_for(channel),
1288
+ file: file,
844
1289
  stream_methods: channel.instance_methods(false)
845
1290
  .select { |m| m.to_s.start_with?("stream_") || m == :subscribed }
846
1291
  .map(&:to_s),
@@ -896,22 +1341,22 @@ module RailsAiContext
896
1341
  result.empty? ? nil : result
897
1342
  end
898
1343
 
899
- # `periodically :method_name, every: 3.seconds`. The interval keeps its
900
- # source form so lambdas like `-> { current_user.interval }` survive whole.
1344
+ # `periodically :method_name, every: 3.seconds` or `periodically every: 3.seconds do`.
1345
+ # The interval keeps its source form so lambdas like `-> { current_user.interval }`
1346
+ # survive whole.
901
1347
  def extract_channel_periodic(macros)
902
- timers = macros.select { |hit| hit[:macro] == :periodically }.filter_map do |hit|
1348
+ timers = macros.select { |hit| hit[:macro] == :periodically }.map do |hit|
903
1349
  method_name = hit[:args].first
904
- next unless method_name
905
- { method: method_name.to_s, every: hit[:option_values][:every].to_s }
1350
+ timer = method_name ? { method: method_name.to_s } : { block: true }
1351
+ timer.merge(every: hit[:option_values][:every].to_s)
906
1352
  end
907
1353
  timers.any? ? timers : nil
908
1354
  end
909
1355
 
910
1356
  # RPC actions = public instance methods that aren't lifecycle hooks or stream helpers.
911
1357
  def extract_channel_actions(channel)
912
- ignored = %i[subscribed unsubscribed]
913
1358
  actions = channel.instance_methods(false).reject do |m|
914
- ignored.include?(m) || m.to_s.start_with?("stream_")
1359
+ CHANNEL_LIFECYCLE.include?(m.to_s) || m.to_s.start_with?("stream_")
915
1360
  end
916
1361
  actions.empty? ? nil : actions.map(&:to_s).sort
917
1362
  end