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
@@ -2,8 +2,8 @@
2
2
 
3
3
  module RailsAiContext
4
4
  module Introspectors
5
- # Parses config/environments/*.rb - the one config surface no other
6
- # introspector covers. Captures, per environment file, which config keys
5
+ # Parses config/environments/*.rb and config/application.rb, which every
6
+ # environment runs. Captures, per environment file, which config keys
7
7
  # are assigned and the values of the toggles AI most often needs to
8
8
  # compare across environments (force_ssl, eager_load, caching, logging,
9
9
  # queue adapter, mailer delivery).
@@ -34,24 +34,60 @@ module RailsAiContext
34
34
  {
35
35
  current: current_environment,
36
36
  count: files.size,
37
- environments: files
38
- }
37
+ environments: files,
38
+ application: summarize_application
39
+ }.compact
39
40
  end
40
41
 
41
42
  private
42
43
 
44
+ APPLICATION = "config/application.rb"
45
+ REFUSALS = { traversal: :outside, outside: :outside, sensitive: :withheld, missing: :missing, too_large: :too_large }.freeze
46
+
47
+ def summarize_application
48
+ path = File.join(root, APPLICATION)
49
+ return nil unless File.file?(path)
50
+
51
+ assignments, config_keys = read_config(path)
52
+ { file: APPLICATION, config_keys: config_keys, config_for: config_for_files(assignments).presence }.compact
53
+ end
54
+
55
+ # The keys config_for gives the environment it reads (`env:`, else this one):
56
+ # `shared` deep-merged under that environment's section. Names only: a value is often a secret.
57
+ def config_for_files(assignments)
58
+ assignments.filter_map do |key, entries|
59
+ call = entries.last[:config_for] or next
60
+ entry = { key: key, call: call[:arguments], read: call[:read], file: call[:file] }.compact
61
+ next entry.merge(path_unread: true) unless call[:file]
62
+
63
+ environment = current_environment
64
+ case call[:env]
65
+ when :expression then next entry.merge(environment_unread: true)
66
+ when String then environment = entry[:environment] = call[:env]
67
+ end
68
+ refusal = SafePath.locate(call[:file], under: root).refusal
69
+ next entry.merge(REFUSALS.fetch(refusal) => true) if refusal
70
+
71
+ data = ConfigYaml.read(root, call[:file], label: "config_for", marker: ConfigYaml::ERB_OUTPUT)
72
+ next entry.merge(unreadable: true) unless data.is_a?(Hash)
73
+
74
+ sections = [ data["shared"], data[environment] ].select { |section| section.is_a?(Hash) }
75
+ computed, keys = sections.flat_map(&:keys).uniq.partition { |name| ConfigYaml.marked?(name) }
76
+ entry.merge(keys: keys.sort, erb_keys: computed.size.nonzero?).compact
77
+ end
78
+ end
79
+
43
80
  def summarize(path)
44
- relative = path.sub("#{root}/", "")
45
- assignments = config_assignments(path)
81
+ assignments, config_keys = read_config(path)
46
82
  name = File.basename(path, ".rb")
47
- {
48
- name: name,
49
- file: relative,
50
- config_keys: assignments.keys.sort,
51
- notable: extract_notable(assignments, environment: name)
52
- }
53
- rescue => e
54
- RailsAiContext.debug_fail(e, nil, label: "summarize environment #{path}")
83
+ { name: name, file: path.sub("#{root}/", ""), config_keys: config_keys, notable: extract_notable(assignments, environment: name) }
84
+ end
85
+
86
+ # The file's config assignments by path, and every key it sets, written or assigned.
87
+ def read_config(path)
88
+ entries = SourceIntrospector.walk(path, { config: Listeners::ConfigAssignmentListener })[:config]
89
+ assignments = config_assignments(entries)
90
+ [ assignments, (assignments.keys + entries.filter_map { |entry| written_key(entry) }).uniq.sort ]
55
91
  end
56
92
 
57
93
  # Assigned `config.*` paths at any depth, mapped to their value source:
@@ -62,15 +98,24 @@ module RailsAiContext
62
98
  # Every assignment of a path, not the first: Rails' own development
63
99
  # template assigns `perform_caching` in both halves of one `if`, and
64
100
  # the first one is the branch that is not running.
65
- def config_assignments(path)
66
- walked = SourceIntrospector.walk(path, { config: Listeners::ConfigAssignmentListener })
67
- walked[:config].each_with_object({}) do |entry, acc|
101
+ def config_assignments(entries)
102
+ entries.each_with_object({}) do |entry, acc|
68
103
  next unless entry[:assignment]
69
104
 
70
105
  (acc[entry[:path].join(".")] ||= []) << entry
71
106
  end
72
107
  end
73
108
 
109
+ # `config.hosts << x` and `config.middleware.use X` change the receiver's
110
+ # setting; `config.session_store :cookie_store` names its own.
111
+ def written_key(entry)
112
+ path = entry[:path]
113
+ case entry[:write]
114
+ when :call then (path.size > 1 ? path[0..-2] : path).join(".")
115
+ when :operator, :block then path.join(".")
116
+ end
117
+ end
118
+
74
119
  def extract_notable(assignments, environment:)
75
120
  NOTABLE_KEYS.each_with_object({}) do |key, notable|
76
121
  entries = assignments[key]
@@ -29,6 +29,8 @@ module RailsAiContext
29
29
  { name: "RAILS_ENV", safe: true, category: :core, doc: "Rails environment (development, test, production)." },
30
30
  { name: "RACK_ENV", safe: true, category: :core, doc: "Rack environment (falls back to RAILS_ENV)." },
31
31
  { name: "RAILS_RELATIVE_URL_ROOT", safe: true, category: :core, doc: "Mount Rails at a sub-path." },
32
+ { name: "RAILS_GROUPS", safe: true, category: :core, doc: "Extra Bundler groups Rails.groups requires, comma-separated." },
33
+ { name: "RAILS_DEVELOPMENT_HOSTS", safe: true, category: :core, doc: "Extra hosts allowed in development, comma-separated." },
32
34
  { name: "RAILS_LOG_LEVEL", safe: true, category: :logging, doc: "Logger level (debug/info/warn/error/fatal)." },
33
35
  { name: "RAILS_LOG_TO_STDOUT", safe: true, category: :logging, doc: "When set, logs go to stdout instead of log/*.log." },
34
36
  { name: "RAILS_MAX_THREADS", safe: true, category: :server, doc: "Puma thread count & ActiveRecord pool default." },
@@ -37,13 +39,13 @@ module RailsAiContext
37
39
  { name: "PORT", safe: true, category: :server, doc: "HTTP port (Puma, Thruster)." },
38
40
  { name: "HOST", safe: true, category: :server, doc: "Bind address (Puma, Thruster)." },
39
41
 
40
- # Assets / eager load
42
+ # Assets
41
43
  { name: "RAILS_SERVE_STATIC_FILES", safe: true, category: :assets, doc: "Serve static assets from Rails (vs an edge CDN/nginx)." },
42
- { name: "RAILS_EAGER_LOAD", safe: true, category: :boot, doc: "Force eager_load regardless of environment." },
43
44
 
44
45
  # Master key / credentials - presence only
45
46
  { name: "RAILS_MASTER_KEY", safe: false, category: :secrets, doc: "Master key for config/credentials.yml.enc." },
46
47
  { name: "SECRET_KEY_BASE", safe: false, category: :secrets, doc: "Session / signed cookie / MessageEncryptor key." },
48
+ { name: "SECRET_KEY_BASE_DUMMY", safe: true, category: :secrets, doc: "Use a throwaway secret_key_base, for assets:precompile in a build (7.1+)." },
47
49
 
48
50
  # Database - presence only
49
51
  { name: "DATABASE_URL", safe: false, category: :database, doc: "Primary database connection URL." },
@@ -56,10 +58,13 @@ module RailsAiContext
56
58
  { name: "REDIS_URL", safe: false, category: :cache, doc: "Redis URL (cache / Sidekiq / Action Cable)." },
57
59
  { name: "REDIS_CACHE_URL", safe: false, category: :cache, doc: "Dedicated Redis URL for caching." },
58
60
  { name: "MEMCACHED_URL", safe: false, category: :cache, doc: "Memcached URL." },
61
+ { name: "RAILS_CACHE_ID", safe: true, category: :cache, doc: "Prefix ActiveSupport::Cache.expand_cache_key adds (fragment and view caching), not Rails.cache keys." },
62
+ { name: "RAILS_APP_VERSION", safe: true, category: :cache, doc: "The expand_cache_key prefix when RAILS_CACHE_ID is unset." },
63
+ { name: "SOLID_QUEUE_IN_PUMA", safe: true, category: :jobs, doc: "Run the Solid Queue supervisor inside Puma (generated config/puma.rb)." },
59
64
 
60
65
  # Kamal / deployment
61
66
  { name: "KAMAL_REGISTRY_PASSWORD", safe: false, category: :deploy, doc: "Kamal container registry password." },
62
- { name: "KAMAL_HOST", safe: true, category: :deploy, doc: "Kamal target host override." },
67
+ { name: "KAMAL_HOST", safe: true, category: :deploy, doc: "The host the container runs on; Kamal sets it, never reads it." },
63
68
 
64
69
  # Heroku
65
70
  { name: "DYNO", safe: true, category: :platform, doc: "Heroku dyno name." },
@@ -72,9 +77,6 @@ module RailsAiContext
72
77
  { name: "BUNDLE_PATH", safe: false, category: :bundler, doc: "Path where gems are installed." },
73
78
  { name: "DISABLE_SPRING", safe: true, category: :bundler, doc: "Disable Spring preloader." },
74
79
 
75
- # Rails 8.1
76
- { name: "RAILS_EVENT_REPORTER", safe: true, category: :observability, doc: "Enable structured event reporter (8.1+)." },
77
-
78
80
  # Testing / parallelism
79
81
  { name: "PARALLEL_WORKERS", safe: true, category: :testing, doc: "Parallel test worker count." },
80
82
  { name: "TEST_ENV_NUMBER", safe: true, category: :testing, doc: "Worker-specific identifier for parallel tests." },
@@ -11,9 +11,14 @@ module RailsAiContext
11
11
  SCAN_PATTERNS = {
12
12
  "app" => %w[**/*.rb **/*.erb],
13
13
  "config" => %w[**/*.rb **/*.yml **/*.yaml],
14
- "lib" => %w[**/*.rb **/*.rake]
14
+ "lib" => %w[**/*.rb **/*.rake],
15
+ "db" => %w[seeds.rb seeds/**/*.rb],
16
+ "bin" => %w[*],
17
+ "." => %w[config.ru]
15
18
  }.freeze
16
19
 
20
+ RUBY_EXTENSIONS = %w[.rb .rake .ru].freeze
21
+
17
22
  COMPUTED_DEFAULT = :computed
18
23
 
19
24
  module_function
@@ -23,9 +28,12 @@ module RailsAiContext
23
28
  real_root = File.realpath(root.to_s)
24
29
  files(root.to_s, real_root).each_with_object({}) do |(path, names_only), found|
25
30
  source = RailsAiContext::SafeFile.read(path)
26
- next unless source&.include?("ENV")
31
+ next unless source
32
+ # Prefilter: ENV and the Rails.app.creds / .envs readers; the AST decides.
33
+ next unless source.include?("ENV") || source.include?(".creds") || source.include?(".envs")
34
+ next if script?(path) && !source.match?(/\A#!.*\bruby\b/)
27
35
  # A YAML or ERB file reads ENV only inside a tag.
28
- next if !path.end_with?(".rb", ".rake") && !source.include?("<%")
36
+ next if !ruby?(path) && !source.include?("<%")
29
37
 
30
38
  refs = references(ruby_source(path, source))
31
39
  refs = refs.map { |ref| ref.except(:default).merge(default_unread: true) } if names_only
@@ -60,9 +68,18 @@ module RailsAiContext
60
68
  end.uniq(&:first)
61
69
  end
62
70
 
71
+ # A bin/ script has no extension, and only its shebang says it is Ruby.
72
+ def script?(path)
73
+ File.extname(path).empty?
74
+ end
75
+
76
+ def ruby?(path)
77
+ script?(path) || path.end_with?(*RUBY_EXTENSIONS)
78
+ end
79
+
63
80
  # ERB tags carry the Ruby of a `.yml` or `.erb` file.
64
81
  def ruby_source(path, source)
65
- return source if path.end_with?(".rb", ".rake")
82
+ return source if ruby?(path)
66
83
 
67
84
  RailsAiContext::ErbSource.ruby_in_place(source)
68
85
  end
@@ -70,7 +70,7 @@ module RailsAiContext
70
70
  mounting = detect_mounting_strategy(all_deps)
71
71
  state = labels_for(STATE_MARKERS, all_deps)
72
72
  testing = labels_for(TEST_MARKERS, all_deps)
73
- pkg_mgr = detect_package_manager
73
+ pkg_mgr, pkg_mgr_dir = RailsAiContext::PackageJson.package_manager(root)
74
74
  mono = detect_monorepo
75
75
  build = detect_build_tool
76
76
  vite_fw = detect_vite_config_frameworks
@@ -97,11 +97,13 @@ module RailsAiContext
97
97
 
98
98
  {
99
99
  frontend_roots: enriched_roots,
100
+ outside_frontend_roots: RailsAiContext::PackageJson.configured_outside(root).map { |dir| dir[:label] },
100
101
  frameworks: frameworks,
101
102
  mounting_strategy: mounting,
102
103
  state_management: state,
103
104
  testing: testing,
104
105
  package_manager: pkg_mgr,
106
+ package_manager_dir: pkg_mgr_dir,
105
107
  typescript: ts,
106
108
  monorepo: mono,
107
109
  build_tool: build,
@@ -111,6 +113,28 @@ module RailsAiContext
111
113
  }
112
114
  end
113
115
 
116
+ # A config file the bundler reads comes first: jsbundling-rails writes
117
+ # webpack.config.js, rollup.config.js or bun.config.js, and bun has no package.
118
+ def self.build_tool(root)
119
+ root = root.to_s
120
+ return "vite" if Dir.glob(File.join(root, "vite.config.*")).any?
121
+ return "webpack" if File.exist?(File.join(root, "config/webpacker.yml")) ||
122
+ File.exist?(File.join(root, "config/shakapacker.yml"))
123
+ %w[webpack rollup bun].each do |tool|
124
+ return tool if Dir.glob(File.join(root, "#{tool}.config.*")).any?
125
+ end
126
+ dirs = RailsAiContext::PackageJson.frontend_roots(root).map { |dir| dir[:dir] }
127
+ %w[vite webpack rollup bun].each do |tool|
128
+ return tool if dirs.any? { |dir| frontend_config?(dir, "#{tool}.config.*") }
129
+ end
130
+ %w[esbuild webpack rollup].find { |pkg| RailsAiContext::PackageJson.present?(root, pkg) }
131
+ end
132
+
133
+ def self.frontend_config?(dir, pattern)
134
+ Dir.glob(pattern, base: dir).any? { |name| RailsAiContext::PackageJson.outside_file(dir, name) }
135
+ end
136
+ private_class_method :frontend_config?
137
+
114
138
  private
115
139
 
116
140
  # ---- Package.json reading ----
@@ -191,27 +215,15 @@ module RailsAiContext
191
215
  markers.filter_map { |pkg, label| label if all_deps.key?(pkg) }.uniq
192
216
  end
193
217
 
194
- # ---- Package manager ----
195
-
196
- def detect_package_manager
197
- return "bun" if File.exist?(File.join(root, "bun.lock")) || File.exist?(File.join(root, "bun.lockb"))
198
- return "pnpm" if File.exist?(File.join(root, "pnpm-lock.yaml"))
199
- return "yarn" if File.exist?(File.join(root, "yarn.lock"))
200
- return "npm" if File.exist?(File.join(root, "package-lock.json"))
201
-
202
- nil
203
- end
204
-
205
218
  # ---- TypeScript ----
206
219
 
207
220
  # An app whose TypeScript lives under frontend/ has no tsconfig.json at
208
- # its root, so the frontend roots are searched too. The root one wins.
221
+ # its root, so the frontend roots are searched too, then the declared
222
+ # ones outside it. The root one wins.
209
223
  def detect_typescript(roots)
210
224
  dirs = [ root ] + roots.map { |fr| File.join(root, fr[:path]) }
211
225
  path = dirs.map { |dir| File.join(dir, "tsconfig.json") }.find { |p| File.exist?(p) }
212
- return { enabled: false } unless path
213
-
214
- compiler, = ModuleAliases.compiler_options(path)
226
+ compiler, = path ? ModuleAliases.compiler_options(path) : outside_compiler_options
215
227
  return { enabled: false } unless compiler
216
228
 
217
229
  {
@@ -221,6 +233,14 @@ module RailsAiContext
221
233
  }
222
234
  end
223
235
 
236
+ def outside_compiler_options
237
+ RailsAiContext::PackageJson.configured_outside(root).each do |dir|
238
+ path = RailsAiContext::PackageJson.outside_file(dir[:dir], "tsconfig.json") or next
239
+ return ModuleAliases.compiler_options(path.to_s, within: dir[:dir])
240
+ end
241
+ nil
242
+ end
243
+
224
244
  # ---- Monorepo ----
225
245
 
226
246
  MONOREPO_MARKERS = { "turbo.json" => "turborepo", "nx.json" => "nx", "lerna.json" => "lerna" }.freeze
@@ -262,12 +282,7 @@ module RailsAiContext
262
282
  # ---- Build tool ----
263
283
 
264
284
  def detect_build_tool
265
- return "vite" if Dir.glob(File.join(root, "vite.config.*")).any?
266
- return "webpack" if File.exist?(File.join(root, "config/webpacker.yml")) ||
267
- File.exist?(File.join(root, "config/shakapacker.yml"))
268
- return "esbuild" if RailsAiContext::PackageJson.present?(root, "esbuild")
269
-
270
- nil
285
+ self.class.build_tool(root)
271
286
  end
272
287
 
273
288
  # ---- Vite config framework detection ----
@@ -16,6 +16,8 @@ module RailsAiContext
16
16
  "omniauth" => { category: :auth, note: "OAuth integration via OmniAuth." },
17
17
  "pundit" => { category: :auth, note: "Authorization via Pundit policies in app/policies/." },
18
18
  "cancancan" => { category: :auth, note: "Authorization via CanCanCan abilities." },
19
+ "action_policy" => { category: :auth, note: "Authorization via Action Policy policies in app/policies/." },
20
+ "rolify" => { category: :auth, note: "Role management via Rolify." },
19
21
  "rodauth-rails" => { category: :auth, note: "Authentication via Rodauth." },
20
22
  "authentication-zero" => { category: :auth, note: "Zero-dependency authentication generator for Rails." },
21
23
  "doorkeeper" => { category: :auth, note: "OAuth 2 provider via Doorkeeper." },
@@ -57,7 +59,7 @@ module RailsAiContext
57
59
  "lookbook" => { category: :frontend, note: "UI component preview and documentation via Lookbook." },
58
60
 
59
61
  # API
60
- "rswag-api" => { category: :api, note: "Serves OpenAPI specs from openapi/ directory." },
62
+ "rswag-api" => { category: :api, note: "Serves OpenAPI specs from its configured openapi_root. Set in config/initializers/rswag_api.rb." },
61
63
  "rswag-ui" => { category: :api, note: "Swagger UI for API documentation." },
62
64
  "grape-swagger" => { category: :api, note: "Swagger docs for Grape APIs." },
63
65
  "apipie-rails" => { category: :api, note: "API documentation DSL for Rails." },
@@ -5,6 +5,10 @@ module RailsAiContext
5
5
  # The gems a Gemfile declares, read through the AST: a commented-out
6
6
  # `# gem "stripe"` line is not a gem the app uses.
7
7
  module GemfileGems
8
+ MUTEX = Mutex.new
9
+ CACHE = {}
10
+ private_constant :MUTEX, :CACHE
11
+
8
12
  module_function
9
13
 
10
14
  # @return [Array<String>] gem names, in declaration order
@@ -15,13 +19,57 @@ module RailsAiContext
15
19
  # Every `gem` and `group` entry, with its options and groups.
16
20
  # @return [Array<Hash>] empty when there is no Gemfile or it cannot be read
17
21
  def entries(root)
18
- path = File.join(root.to_s, "Gemfile")
19
- return [] unless File.file?(path)
22
+ read_bundle(GemLock.bundle(root)) || []
23
+ end
24
+
25
+ # The bundle's Gemfile with the files it evaluates, walked once until any
26
+ # of them changes or appears; nil when there is no Gemfile.
27
+ def read_bundle(bundle)
28
+ gemfile = bundle.gemfile
29
+ return nil unless gemfile && File.file?(gemfile)
30
+
31
+ key = [ bundle.dir, gemfile ]
32
+ cached = MUTEX.synchronize { CACHE[key] }
33
+ return cached[:entries] if cached && cached[:stamps].all? { |path, stamp| GemLock.mtime(path) == stamp }
34
+
35
+ stamps = {}
36
+ # A failed walk is kept too: the same files fail the same way until they change.
37
+ entries = begin
38
+ read(bundle.dir, File.basename(gemfile), [], [], stamps)
39
+ rescue StandardError => e
40
+ RailsAiContext.debug_fail(e, nil, label: "GemfileGems.read_bundle")
41
+ end
42
+ MUTEX.synchronize { CACHE[key] = { entries: entries, stamps: stamps } }
43
+ entries
44
+ end
45
+
46
+ # The mtimes of the Gemfile and every file it evaluates, as read_bundle last read them.
47
+ def stamps(bundle)
48
+ read_bundle(bundle)
49
+ MUTEX.synchronize { CACHE[[ bundle.dir, bundle.gemfile ]]&.fetch(:stamps) }
50
+ end
51
+
52
+ # Bundler evaluates it in place, under the surrounding groups; never read outside the bundle's directory.
53
+ def read(root, relative, groups, seen, stamps)
54
+ stamps[File.join(root, relative)] = GemLock.mtime(File.join(root, relative))
55
+ resolution = SafePath.locate(relative, under: root)
56
+ return [ { type: :unknown_gems, call: :eval_gemfile } ] unless resolution.ok?
57
+ return [] if seen.include?(resolution.realpath)
58
+
59
+ seen << resolution.realpath
60
+ found = Array(SourceIntrospector.walk(resolution.realpath, { gems: -> { Listeners::GemfileDslListener.new } })[:gems])
61
+ base = File.expand_path(root)
62
+ found.flat_map do |entry|
63
+ entry = entry.merge(groups: (groups + entry[:groups]).uniq) if groups.any? && %i[gem eval_gemfile].include?(entry[:type])
64
+ next [ entry ] unless entry[:type] == :eval_gemfile
65
+
66
+ nested = File.expand_path(entry[:path], File.dirname(File.join(root, relative)))
67
+ next [ { type: :unknown_gems, call: :eval_gemfile } ] unless SafePath.contained?(nested, base)
20
68
 
21
- Array(SourceIntrospector.walk(path, { gems: -> { Listeners::GemfileDslListener.new } })[:gems])
22
- rescue StandardError => e
23
- RailsAiContext.debug_fail(e, [], label: "GemfileGems.entries")
69
+ read(root, nested.delete_prefix(SafePath.dir_prefix(base)), entry[:groups], seen, stamps)
70
+ end
24
71
  end
72
+ private_class_method :read
25
73
  end
26
74
  end
27
75
  end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsAiContext
4
+ module Introspectors
5
+ # The endpoints of each Grape API the routes mount, read from the API
6
+ # classes under app/api and lib/api. A path is built the way Grape builds
7
+ # it: mount paths, prefix, a :path version, namespaces, then the
8
+ # endpoint's own path. Read from source, so both tiers answer the same.
9
+ module GrapeEndpoints
10
+ DIRS = %w[app/api lib/api].freeze
11
+ ROOTS = %w[Grape::API Grape::API::Instance].freeze
12
+
13
+ module_function
14
+
15
+ # @param mounts [Array<Hash>] the routes' mounted apps, engine and path
16
+ # @return [Hash{String => Array<Hash>}] verb, path, params and file per endpoint, by mounted API
17
+ def call(root, mounts)
18
+ mounts = Array(mounts).select { |m| m.is_a?(Hash) && m[:engine] }
19
+ return {} if mounts.empty?
20
+
21
+ classes = read(root.to_s)
22
+ mounts.each_with_object({}) do |mount, found|
23
+ name = resolve(classes, mount[:engine].delete_prefix("::"), nil)
24
+ next unless name && grape?(classes, name)
25
+
26
+ list = expand(classes, name, base: [ mount[:path] ], namespace: [], prefix: nil, version: nil, seen: [])
27
+ found[mount[:engine]] = list if list.any?
28
+ end
29
+ end
30
+
31
+ def line(endpoint)
32
+ params = endpoint[:params].map do |param|
33
+ details = [ param[:type], ("required" if param[:required]) ].compact
34
+ details.any? ? "#{param[:name]} (#{details.join(', ')})" : param[:name]
35
+ end
36
+ "`#{endpoint[:verb]}` `#{endpoint[:path]}`#{" - params: #{params.join(', ')}" if params.any?}"
37
+ end
38
+
39
+ def read(root)
40
+ classes = {}
41
+ DIRS.each do |dir|
42
+ FileWalk.each_file(File.join(root, dir)).select { |path| path.end_with?(".rb") }.sort.each do |path|
43
+ relative = path.delete_prefix("#{root}/")
44
+ source, = SafePath.read(relative, under: root)
45
+ next if source.nil? || source.empty?
46
+
47
+ records = SourceIntrospector.walk_source(source, { grape: Listeners::GrapeApiListener })[:grape]
48
+ Array(records).each do |record|
49
+ entry = classes[record[:owner]] ||= { endpoints: [], mounts: [], file: relative }
50
+ case record[:kind]
51
+ when :class then entry[:superclass] = record[:superclass]
52
+ when :endpoint then entry[:endpoints] << record
53
+ when :mount then entry[:mounts] << record
54
+ else entry[record[:kind]] ||= record
55
+ end
56
+ end
57
+ end
58
+ end
59
+ classes
60
+ end
61
+
62
+ # A name written inside V1 can mean V1::Users or a top-level Users.
63
+ def resolve(classes, name, scope)
64
+ candidates = []
65
+ parts = scope.to_s.split("::")
66
+ until parts.empty?
67
+ candidates << "#{parts.join('::')}::#{name}"
68
+ parts.pop
69
+ end
70
+ candidates << name
71
+ candidates.find { |candidate| classes.key?(candidate) } || (name if ROOTS.include?(name))
72
+ end
73
+
74
+ def grape?(classes, name, seen = [])
75
+ return true if ROOTS.include?(name)
76
+ return false if seen.include?(name) || !classes[name]
77
+
78
+ parent = classes[name][:superclass] or return false
79
+ grape?(classes, resolve(classes, parent, name.rpartition("::").first) || parent, seen + [ name ])
80
+ end
81
+
82
+ def expand(classes, name, base:, namespace:, prefix:, version:, seen:)
83
+ return [] if seen.include?(name)
84
+
85
+ entry = classes[name] or return []
86
+ prefix = entry[:prefix]&.dig(:value) || prefix
87
+ version = entry[:version] || version
88
+ version_part = version[:value] if version && version[:using] == "path"
89
+
90
+ own = entry[:endpoints].map do |endpoint|
91
+ path = join(*base, prefix, version_part, *namespace, *endpoint[:namespace], endpoint[:path])
92
+ { verb: endpoint[:verb], path: path, params: endpoint[:params], file: entry[:file] }
93
+ end
94
+ own + entry[:mounts].flat_map do |mount|
95
+ target = resolve(classes, mount[:target], name)
96
+ next [] unless target && grape?(classes, target)
97
+
98
+ expand(classes, target, base: base + [ mount[:path] ], namespace: namespace + mount[:namespace],
99
+ prefix: prefix, version: version, seen: seen + [ name ])
100
+ end
101
+ end
102
+
103
+ def join(*parts)
104
+ "/" + parts.compact.flat_map { |part| part.to_s.split("/") }.reject(&:empty?).join("/")
105
+ end
106
+ private_class_method :read, :resolve, :grape?, :expand, :join
107
+ end
108
+ end
109
+ end
@@ -16,7 +16,10 @@ module RailsAiContext
16
16
  included = []
17
17
  PathResolver.dirs_for(root, "app/helpers").each do |dir|
18
18
  FileWalk.each_file(dir).select { |path| path.end_with?(".rb") }.each do |path|
19
- walked = SourceIntrospector.walk(path, { methods: Listeners::MethodsListener, mixins: Listeners::MixinsListener })
19
+ source = RailsAiContext::SafeFile.read(path)
20
+ next if source && !view_helper?(source, path, dir)
21
+
22
+ walked = SourceIntrospector.walk(path, { methods: Listeners::MethodsListener, mixins: Listeners::MixinsListener }, source: source)
20
23
  names.merge(Array(walked[:methods]).map { |m| m[:name].to_s })
21
24
  included.concat(Array(walked[:mixins]).select { |m| m[:macro] == :include && m[:ancestor] }.map { |m| m[:name] })
22
25
  end
@@ -31,6 +34,14 @@ module RailsAiContext
31
34
  RailsAiContext.debug_fail(e, Set.new, label: "HelperNames")
32
35
  end
33
36
 
37
+ # `helper :all` mixes in the modules of `**/*_helper.rb` files only; a class there reaches no view.
38
+ def view_helper?(source, path, dir)
39
+ return false unless path.end_with?("_helper.rb")
40
+
41
+ path_name = path.delete_prefix("#{dir}/").delete_prefix("concerns/").delete_suffix(".rb").camelize
42
+ !DeclaredConstant.declared_names(source, path_name: path_name).include?(DeclaredConstant.named(source, path_name))
43
+ end
44
+
34
45
  # The instance methods `owner` defines in a file: what including it gives a view.
35
46
  def defined_methods(path, owner)
36
47
  methods = SourceIntrospector.walk(path, { methods: Listeners::MethodsListener })[:methods]