evilution 1.5.0 → 1.6.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 (228) hide show
  1. checksums.yaml +4 -4
  2. data/.beads/interactions.jsonl +1 -0
  3. data/.rubocop_todo.yml +30 -0
  4. data/CHANGELOG.md +59 -0
  5. data/README.md +285 -45
  6. data/docs/architecture.md +110 -12
  7. data/docs/ast_pattern_syntax.md +35 -7
  8. data/docs/combined_runs_spike.md +407 -0
  9. data/docs/integrations.md +1 -1
  10. data/docs/isolation.md +63 -2
  11. data/docs/migration-from-mutant.md +16 -11
  12. data/docs/mutant_schemata_spike.md +387 -0
  13. data/docs/operator_yield_study.md +576 -0
  14. data/docs/probe_first_spike.md +264 -0
  15. data/lib/evilution/ast/ancestor_lookup.rb +36 -0
  16. data/lib/evilution/ast/callback_declaration.rb +36 -3
  17. data/lib/evilution/ast/class_body.rb +62 -16
  18. data/lib/evilution/ast/def_region.rb +130 -0
  19. data/lib/evilution/ast/local_declaration.rb +37 -0
  20. data/lib/evilution/ast/parent_index.rb +50 -0
  21. data/lib/evilution/ast/parse_cache.rb +27 -0
  22. data/lib/evilution/ast/parser.rb +8 -3
  23. data/lib/evilution/ast/pattern/filter.rb +53 -12
  24. data/lib/evilution/ast/skip_filter.rb +83 -0
  25. data/lib/evilution/ast/skip_sites/debug_output.rb +67 -0
  26. data/lib/evilution/ast/skip_sites/error_messages.rb +74 -0
  27. data/lib/evilution/ast/skip_sites/logging.rb +93 -0
  28. data/lib/evilution/ast/skip_sites/message.rb +44 -0
  29. data/lib/evilution/ast/skip_sites/nodes.rb +105 -0
  30. data/lib/evilution/ast/skip_sites/presets.rb +31 -0
  31. data/lib/evilution/ast/skip_sites/purity.rb +69 -0
  32. data/lib/evilution/ast/skip_sites/requires.rb +40 -0
  33. data/lib/evilution/ast/skip_sites.rb +9 -0
  34. data/lib/evilution/ast/source_surgeon.rb +20 -2
  35. data/lib/evilution/ast/syntax_check.rb +10 -8
  36. data/lib/evilution/baseline/report.rb +11 -2
  37. data/lib/evilution/baseline.rb +31 -6
  38. data/lib/evilution/cache/keys.rb +140 -0
  39. data/lib/evilution/cache/run_fingerprint.rb +51 -0
  40. data/lib/evilution/cache/skeleton.rb +57 -0
  41. data/lib/evilution/cache/spec_digest.rb +39 -0
  42. data/lib/evilution/cache.rb +145 -42
  43. data/lib/evilution/class_tree.rb +38 -0
  44. data/lib/evilution/cli/command.rb +12 -4
  45. data/lib/evilution/cli/commands/run.rb +12 -3
  46. data/lib/evilution/cli/commands/subjects.rb +5 -2
  47. data/lib/evilution/cli/parser/options_builder.rb +67 -3
  48. data/lib/evilution/cli/printers/environment.rb +34 -4
  49. data/lib/evilution/cli/printers/session_detail.rb +20 -3
  50. data/lib/evilution/compare/categorizer.rb +2 -1
  51. data/lib/evilution/compare/normalizer.rb +3 -2
  52. data/lib/evilution/config/sources.rb +3 -0
  53. data/lib/evilution/config/validators/base.rb +9 -0
  54. data/lib/evilution/config/validators/since.rb +22 -0
  55. data/lib/evilution/config/validators/skip_sites.rb +24 -0
  56. data/lib/evilution/config/validators/timeout_ceiling.rb +10 -0
  57. data/lib/evilution/config/validators/timeout_factor.rb +10 -0
  58. data/lib/evilution/config/validators/timeout_floor.rb +10 -0
  59. data/lib/evilution/config.rb +122 -6
  60. data/lib/evilution/coverage/load_tracker.rb +50 -0
  61. data/lib/evilution/coverage/map.rb +42 -5
  62. data/lib/evilution/coverage/map_builder.rb +123 -22
  63. data/lib/evilution/coverage/map_store.rb +15 -5
  64. data/lib/evilution/coverage/recorder.rb +42 -3
  65. data/lib/evilution/coverage_example_filter.rb +35 -0
  66. data/lib/evilution/equivalent/heuristic/comment_marking.rb +14 -1
  67. data/lib/evilution/example_filter.rb +20 -0
  68. data/lib/evilution/git/changed_lines.rb +103 -0
  69. data/lib/evilution/git/since_scope.rb +77 -0
  70. data/lib/evilution/git/unified_diff.rb +68 -0
  71. data/lib/evilution/infra_crash.rb +26 -0
  72. data/lib/evilution/integration/base.rb +97 -6
  73. data/lib/evilution/integration/crash_detector.rb +46 -6
  74. data/lib/evilution/integration/known_failures.rb +4 -0
  75. data/lib/evilution/integration/loading/body_call_neutralizer.rb +49 -11
  76. data/lib/evilution/integration/loading/callback_equivalence.rb +62 -0
  77. data/lib/evilution/integration/loading/callback_redeclaration.rb +151 -36
  78. data/lib/evilution/integration/loading/concern_redeclaration.rb +16 -12
  79. data/lib/evilution/integration/loading/concern_registry.rb +87 -0
  80. data/lib/evilution/integration/loading/concern_state_cleaner.rb +19 -8
  81. data/lib/evilution/integration/loading/created_modules.rb +56 -0
  82. data/lib/evilution/integration/loading/frozen_namespaces.rb +80 -0
  83. data/lib/evilution/integration/loading/mutation_applier.rb +42 -11
  84. data/lib/evilution/integration/loading/neutralization.rb +41 -0
  85. data/lib/evilution/integration/loading/redefinition_recovery.rb +5 -3
  86. data/lib/evilution/integration/loading/superseded_modules.rb +22 -19
  87. data/lib/evilution/integration/minitest/stop_hook.rb +57 -0
  88. data/lib/evilution/integration/minitest.rb +22 -2
  89. data/lib/evilution/integration/minitest_crash_detector.rb +19 -0
  90. data/lib/evilution/integration/rspec/baseline_runner.rb +3 -2
  91. data/lib/evilution/integration/rspec/crash_detector_lifecycle.rb +6 -1
  92. data/lib/evilution/integration/rspec/example_filter_applier.rb +16 -0
  93. data/lib/evilution/integration/rspec/example_ids.rb +23 -0
  94. data/lib/evilution/integration/rspec/example_order.rb +86 -0
  95. data/lib/evilution/integration/rspec/killer_first.rb +72 -0
  96. data/lib/evilution/integration/rspec/result_builder.rb +7 -0
  97. data/lib/evilution/integration/rspec/state_guard/example_group_descendants.rb +44 -0
  98. data/lib/evilution/integration/rspec/state_guard.rb +2 -2
  99. data/lib/evilution/integration/rspec/target_resolver.rb +42 -0
  100. data/lib/evilution/integration/rspec/test_file_resolver.rb +4 -2
  101. data/lib/evilution/integration/rspec.rb +98 -47
  102. data/lib/evilution/integration/stop_policy.rb +49 -0
  103. data/lib/evilution/integration/test_unit/dispatcher.rb +20 -1
  104. data/lib/evilution/integration/test_unit/subject_class_registry.rb +6 -1
  105. data/lib/evilution/integration/test_unit.rb +28 -2
  106. data/lib/evilution/isolation/fork.rb +58 -13
  107. data/lib/evilution/isolation/in_process.rb +12 -1
  108. data/lib/evilution/line_set.rb +72 -0
  109. data/lib/evilution/mcp/info_tool/actions/environment.rb +12 -16
  110. data/lib/evilution/mcp/info_tool/actions/subjects.rb +3 -5
  111. data/lib/evilution/mcp/info_tool/status_glossary.rb +15 -2
  112. data/lib/evilution/mcp/info_tool.rb +24 -17
  113. data/lib/evilution/mcp/mutate_tool/config_builder.rb +13 -1
  114. data/lib/evilution/mcp/mutate_tool/option_parser.rb +2 -2
  115. data/lib/evilution/mcp/mutate_tool/progress_streamer.rb +1 -1
  116. data/lib/evilution/mcp/mutate_tool/report_trimmer.rb +28 -5
  117. data/lib/evilution/mcp/mutate_tool/survived_enricher.rb +12 -7
  118. data/lib/evilution/mcp/mutate_tool.rb +32 -7
  119. data/lib/evilution/mcp/session_tool.rb +1 -2
  120. data/lib/evilution/mcp.rb +4 -0
  121. data/lib/evilution/mutation/line_diff.rb +111 -0
  122. data/lib/evilution/mutation.rb +17 -13
  123. data/lib/evilution/mutator/base.rb +121 -98
  124. data/lib/evilution/mutator/constant_value_subjects.rb +29 -1
  125. data/lib/evilution/mutator/line_range_filter.rb +27 -0
  126. data/lib/evilution/mutator/load_sources.rb +88 -0
  127. data/lib/evilution/mutator/operator/alias_removal.rb +2 -5
  128. data/lib/evilution/mutator/operator/argument_method_call_replacement.rb +3 -0
  129. data/lib/evilution/mutator/operator/attr_accessor_swap.rb +2 -5
  130. data/lib/evilution/mutator/operator/constant_namespace_strip.rb +3 -3
  131. data/lib/evilution/mutator/operator/freeze_removal.rb +29 -11
  132. data/lib/evilution/mutator/operator/method_call_removal.rb +3 -1
  133. data/lib/evilution/mutator/operator/mixin_removal.rb +2 -5
  134. data/lib/evilution/mutator/operator/superclass_removal.rb +2 -5
  135. data/lib/evilution/mutator/operator/symbol_literal.rb +1 -1
  136. data/lib/evilution/mutator/operator/visibility_removal.rb +2 -5
  137. data/lib/evilution/mutator/registry.rb +11 -5
  138. data/lib/evilution/mutator/restore_source_cache.rb +41 -0
  139. data/lib/evilution/parallel/pool.rb +11 -4
  140. data/lib/evilution/parallel/work_queue/dispatcher/result_stream.rb +40 -0
  141. data/lib/evilution/parallel/work_queue/dispatcher.rb +20 -7
  142. data/lib/evilution/parallel/work_queue/worker/loop.rb +7 -2
  143. data/lib/evilution/parallel/work_queue/worker.rb +10 -3
  144. data/lib/evilution/parallel/work_queue/worker_stat.rb +7 -0
  145. data/lib/evilution/parallel/work_queue.rb +69 -12
  146. data/lib/evilution/parallel_db_warning.rb +39 -12
  147. data/lib/evilution/process_supervisor.rb +22 -2
  148. data/lib/evilution/reporter/cli/item_formatters/coverage_gap.rb +13 -2
  149. data/lib/evilution/reporter/cli/item_formatters/subject_score.rb +5 -1
  150. data/lib/evilution/reporter/cli/line_formatters/mutations.rb +9 -2
  151. data/lib/evilution/reporter/cli/line_formatters/result_line.rb +9 -1
  152. data/lib/evilution/reporter/cli/line_formatters/skipped_sites.rb +14 -0
  153. data/lib/evilution/reporter/cli/line_formatters/uncovered_code.rb +17 -0
  154. data/lib/evilution/reporter/cli/metrics_block.rb +4 -0
  155. data/lib/evilution/reporter/cli/trailer.rb +4 -4
  156. data/lib/evilution/reporter/cli.rb +9 -2
  157. data/lib/evilution/reporter/html/assets/style.css +8 -0
  158. data/lib/evilution/reporter/html/sections/file_section.rb +9 -0
  159. data/lib/evilution/reporter/html/sections/uncovered_details.rb +27 -0
  160. data/lib/evilution/reporter/html/templates/file_section.html.erb +2 -1
  161. data/lib/evilution/reporter/html/templates/summary_cards.html.erb +3 -0
  162. data/lib/evilution/reporter/html/templates/uncovered_details.html.erb +11 -0
  163. data/lib/evilution/reporter/json/mutation_detail.rb +77 -0
  164. data/lib/evilution/reporter/json/subjects.rb +1 -0
  165. data/lib/evilution/reporter/json/timings.rb +42 -0
  166. data/lib/evilution/reporter/json.rb +13 -50
  167. data/lib/evilution/result/coverage_gap.rb +5 -0
  168. data/lib/evilution/result/mutation_result.rb +55 -3
  169. data/lib/evilution/result/mutation_timings.rb +54 -0
  170. data/lib/evilution/result/run_timings.rb +20 -0
  171. data/lib/evilution/result/subject_score.rb +8 -1
  172. data/lib/evilution/result/subject_scorer.rb +5 -3
  173. data/lib/evilution/result/summary.rb +49 -12
  174. data/lib/evilution/runner/baseline_runner.rb +47 -5
  175. data/lib/evilution/runner/diagnostics.rb +0 -13
  176. data/lib/evilution/runner/isolation_resolver.rb +16 -4
  177. data/lib/evilution/runner/mutation_executor/isolated_run.rb +34 -0
  178. data/lib/evilution/runner/mutation_executor/kill_history.rb +66 -0
  179. data/lib/evilution/runner/mutation_executor/mutation_runner.rb +31 -12
  180. data/lib/evilution/runner/mutation_executor/neutralizer/baseline_failed.rb +1 -0
  181. data/lib/evilution/runner/mutation_executor/neutralizer/infra_error.rb +6 -20
  182. data/lib/evilution/runner/mutation_executor/probe_first.rb +99 -0
  183. data/lib/evilution/runner/mutation_executor/probe_first_run.rb +59 -0
  184. data/lib/evilution/runner/mutation_executor/result_cache.rb +45 -14
  185. data/lib/evilution/runner/mutation_executor/result_notifier.rb +1 -1
  186. data/lib/evilution/runner/mutation_executor/result_packer.rb +54 -24
  187. data/lib/evilution/runner/mutation_executor/runless_verdict.rb +39 -0
  188. data/lib/evilution/runner/mutation_executor/strategy/parallel/hinted_items.rb +27 -0
  189. data/lib/evilution/runner/mutation_executor/strategy/parallel/ordered_delivery.rb +97 -0
  190. data/lib/evilution/runner/mutation_executor/strategy/parallel.rb +102 -83
  191. data/lib/evilution/runner/mutation_executor/timed_run.rb +33 -0
  192. data/lib/evilution/runner/mutation_executor/timeout_policy/adaptive.rb +67 -0
  193. data/lib/evilution/runner/mutation_executor/timeout_policy/base.rb +35 -0
  194. data/lib/evilution/runner/mutation_executor/timeout_policy/fixed.rb +17 -0
  195. data/lib/evilution/runner/mutation_executor/timeout_policy.rb +56 -0
  196. data/lib/evilution/runner/mutation_executor.rb +75 -10
  197. data/lib/evilution/runner/mutation_planner.rb +29 -21
  198. data/lib/evilution/runner/phase_clock.rb +22 -0
  199. data/lib/evilution/runner/report_publisher.rb +1 -1
  200. data/lib/evilution/runner/subject_pipeline.rb +18 -8
  201. data/lib/evilution/runner.rb +87 -26
  202. data/lib/evilution/session/diff.rb +9 -2
  203. data/lib/evilution/session/store.rb +17 -2
  204. data/lib/evilution/source_ast_cache.rb +10 -0
  205. data/lib/evilution/spec_ast_cache.rb +15 -0
  206. data/lib/evilution/spec_resolver/constant_references.rb +66 -0
  207. data/lib/evilution/spec_resolver/project_files.rb +50 -0
  208. data/lib/evilution/spec_resolver/sibling_spec_ranker.rb +111 -0
  209. data/lib/evilution/spec_resolver/spec_pattern.rb +19 -0
  210. data/lib/evilution/spec_resolver.rb +76 -65
  211. data/lib/evilution/spec_selector.rb +17 -13
  212. data/lib/evilution/subject.rb +7 -2
  213. data/lib/evilution/version.rb +1 -1
  214. data/lib/evilution/worker_database/copies.rb +73 -0
  215. data/lib/evilution/worker_database/schema.rb +39 -0
  216. data/lib/evilution/worker_database/sqlite_copy.rb +65 -0
  217. data/lib/evilution/worker_database.rb +214 -0
  218. data/schema/evilution.config.schema.json +74 -2
  219. metadata +84 -13
  220. data/lib/evilution/integration/rspec/state_guard/object_space_example_groups.rb +0 -42
  221. data/scripts/benchmark_density +0 -262
  222. data/scripts/benchmark_density.yml +0 -19
  223. data/scripts/canary_manifest.yml +0 -47
  224. data/scripts/compare_mutations +0 -421
  225. data/scripts/compare_mutations.yml +0 -24
  226. data/scripts/compare_targeting +0 -279
  227. data/scripts/compare_targeting.example.yml +0 -24
  228. data/scripts/mutant_json_adapter +0 -227
@@ -1,29 +1,50 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../integration"
4
+ require_relative "stop_policy"
5
+ require_relative "rspec/example_ids"
4
6
 
5
7
  class Evilution::Integration::CrashDetector
6
8
  def self.register_with_rspec
7
9
  ::RSpec::Core::Formatters.register self, :example_failed
8
10
  end
9
11
 
12
+ # The run's stop policy. Left alone, the detector never stops a run: only a
13
+ # mutation's own run is handed a policy that can.
14
+ attr_writer :policy
15
+
10
16
  def initialize(_output)
17
+ @policy = Evilution::Integration::StopPolicy::DISABLED
11
18
  reset
12
19
  end
13
20
 
21
+ # The id of the run's first decisive failure (see StopPolicy#decisive?),
22
+ # the example that killed the mutation; nil while there is none. Named
23
+ # whether or not the policy stops the run there.
24
+ attr_reader :killing_id
25
+
14
26
  def reset
15
27
  @assertion_failures = 0
16
28
  @crashes = []
29
+ @raised_quit = false
30
+ @killing_id = nil
17
31
  end
18
32
 
33
+ # RSpec checks RSpec.world.wants_to_quit before each example and skips the
34
+ # rest once it is up, which is how a decisive failure ends the run.
19
35
  def example_failed(notification)
20
- exception = notification.example.exception
36
+ example = notification.example
37
+ crash = tally(example.exception)
38
+ id = Evilution::Integration::RSpec::ExampleIds.of(example)
39
+ @killing_id ||= id if @policy.decisive?(id:, exception: crash)
40
+ raise_quit if @policy.record(id:, exception: crash)
41
+ end
21
42
 
22
- if assertion_exception?(exception)
23
- @assertion_failures += 1
24
- else
25
- @crashes << exception
26
- end
43
+ # Whether this run's flag went up because of this detector. A flag that was
44
+ # already up, such as from RSpec's handler on the user's first Ctrl-C, is
45
+ # not the detector's to take back down.
46
+ def raised_quit?
47
+ @raised_quit
27
48
  end
28
49
 
29
50
  def assertion_failure?
@@ -50,6 +71,25 @@ class Evilution::Integration::CrashDetector
50
71
 
51
72
  private
52
73
 
74
+ def raise_quit
75
+ return if ::RSpec.world.wants_to_quit
76
+
77
+ ::RSpec.world.wants_to_quit = true
78
+ @raised_quit = true
79
+ end
80
+
81
+ # Counts the failure; returns its exception when it is a crash, nil when it
82
+ # is an assertion failure.
83
+ def tally(exception)
84
+ if assertion_exception?(exception)
85
+ @assertion_failures += 1
86
+ nil
87
+ else
88
+ @crashes << exception
89
+ exception
90
+ end
91
+ end
92
+
53
93
  def assertion_exception?(exception)
54
94
  exception.is_a?(::RSpec::Expectations::ExpectationNotMetError) ||
55
95
  (defined?(::RSpec::Mocks::MockExpectationError) &&
@@ -18,6 +18,10 @@ class Evilution::Integration::KnownFailures
18
18
  @ids.empty?
19
19
  end
20
20
 
21
+ def include?(id)
22
+ @ids.include?(id)
23
+ end
24
+
21
25
  # Whether failed_ids names at least one test, and none the baseline did not
22
26
  # already see failing.
23
27
  def only?(failed_ids)
@@ -7,6 +7,7 @@ require_relative "../../ast/included_block"
7
7
  require_relative "../../ast/callback_declaration"
8
8
  require_relative "callback_redeclaration"
9
9
  require_relative "concern_redeclaration"
10
+ require_relative "neutralization"
10
11
 
11
12
  # Strip non-idempotent class/module-body side-effect calls from a mutated
12
13
  # source before re-eval. Such calls (e.g. dry-monads `register_mixin`, plugin
@@ -93,28 +94,63 @@ class Evilution::Integration::Loading::BodyCallNeutralizer
93
94
  # wrapped in a CallbackRedeclaration call, which puts what the re-run
94
95
  # registers in the place of what the declaration registered before.
95
96
  # `keep_lines` is where that was: the declaration's lines in the file as it
96
- # was loaded.
97
- def call(source, file_path: nil, keep_offset: nil, keep_lines: nil)
97
+ # was loaded, and `keep_source` the declaration as written there, which
98
+ # CallbackRedeclaration runs first, in the class body's own binding, to
99
+ # find what the load registered.
100
+ # A mutation that drops the end of a declaration -- its last option -- is
101
+ # made at the very end of what is left, and still keeps it.
102
+ def call(source, file_path: nil, keep_offset: nil, keep_lines: nil, keep_source: nil)
98
103
  return source if file_path && !preloaded?(file_path)
99
104
 
100
105
  result = Prism.parse(source)
101
106
  return source if result.failure?
102
107
 
103
- edits = collect_edits(source, result.value, keep_offset, keep_lines)
108
+ edits = collect_edits(source, result.value, Kept.new(keep_offset, keep_lines, keep_source))
104
109
  return source if edits.empty?
105
110
 
106
111
  apply_edits(source, edits)
107
112
  end
108
113
 
114
+ # The source neutralized as #call neutralizes it, keeping nothing, with
115
+ # the edits that make it: a mutation made away from them is then carried
116
+ # into it (see Neutralization#carry) rather than its file neutralized anew.
117
+ def neutralization(source, file_path: nil)
118
+ return Evilution::Integration::Loading::Neutralization.new(source: source, edits: []) if file_path && !preloaded?(file_path)
119
+
120
+ result = Prism.parse(source)
121
+ return Evilution::Integration::Loading::Neutralization.new(source: source, edits: nil) if result.failure?
122
+
123
+ edits = collect_edits(source, result.value, Kept.new(nil, nil, nil)).sort_by!(&:first)
124
+ neutralized = edits.empty? ? source : apply_edits(source, edits)
125
+ Evilution::Integration::Loading::Neutralization.new(source: neutralized, edits: edits.freeze)
126
+ end
127
+
128
+ # Whether the node holds nothing this neutralizes: only the bodies of
129
+ # classes, modules and singleton classes are looked into. Each opens with
130
+ # its keyword, so text without either keyword needs no walk.
131
+ def blanks_nothing_in?(node)
132
+ return true unless node.slice.match?(SCOPE_KEYWORD)
133
+
134
+ node.breadth_first_search { |child| SCOPES.any? { |scope| child.is_a?(scope) } }.nil?
135
+ end
136
+
109
137
  private
110
138
 
139
+ SCOPES = [Prism::ClassNode, Prism::ModuleNode, Prism::SingletonClassNode].freeze
140
+ SCOPE_KEYWORD = /class|module/
141
+ private_constant :SCOPES, :SCOPE_KEYWORD
142
+
111
143
  def preloaded?(file_path)
112
144
  self.class.preloaded_features.include?(File.expand_path(file_path))
113
145
  end
114
146
 
115
- def collect_edits(source, tree, keep_offset, keep_lines)
147
+ # What the call keeps, and how a kept callback declaration is wrapped.
148
+ Kept = Data.define(:offset, :lines, :source)
149
+ private_constant :Kept
150
+
151
+ def collect_edits(source, tree, kept)
116
152
  edits = []
117
- walker = Walker.new(source, IDEMPOTENT_CALLS, edits, keep_offset, keep_lines)
153
+ walker = Walker.new(source, IDEMPOTENT_CALLS, edits, kept)
118
154
  walker.visit(tree)
119
155
  edits
120
156
  end
@@ -135,13 +171,14 @@ class Evilution::Integration::Loading::BodyCallNeutralizer
135
171
  Prism::ConstantAndWriteNode, Prism::ConstantPathAndWriteNode
136
172
  ].freeze
137
173
 
138
- def initialize(source, allowlist, edits, keep_offset, keep_lines)
174
+ def initialize(source, allowlist, edits, kept)
139
175
  super()
140
176
  @source = source
141
177
  @allowlist = allowlist
142
178
  @edits = edits
143
- @keep_offset = keep_offset
144
- @keep_lines = keep_lines
179
+ @keep_offset = kept.offset
180
+ @keep_lines = kept.lines
181
+ @keep_source = kept.source
145
182
  end
146
183
 
147
184
  def visit_class_node(node)
@@ -206,8 +243,9 @@ class Evilution::Integration::Loading::BodyCallNeutralizer
206
243
 
207
244
  start_offset = stmt.location.start_offset
208
245
  end_offset = stmt.location.end_offset
209
- lines = "#{@keep_lines.first}..#{@keep_lines.last}"
210
- @edits << [start_offset, start_offset, "#{CALLBACKS}.call(self, __FILE__, #{lines}) { "]
246
+ arguments = ["self", "__FILE__", "#{@keep_lines.first}..#{@keep_lines.last}"]
247
+ arguments.push(@keep_source.dump, "binding") if @keep_source
248
+ @edits << [start_offset, start_offset, "#{CALLBACKS}.call(#{arguments.join(", ")}) { "]
211
249
  @edits << [end_offset, end_offset, " }"]
212
250
  end
213
251
 
@@ -295,7 +333,7 @@ class Evilution::Integration::Loading::BodyCallNeutralizer
295
333
  end
296
334
 
297
335
  def kept?((start_offset, end_offset, _replacement))
298
- @keep_offset && @keep_offset >= start_offset && @keep_offset < end_offset
336
+ @keep_offset && @keep_offset.between?(start_offset, end_offset)
299
337
  end
300
338
 
301
339
  # Prism CallNode location ends at the close of the call syntax (e.g. the
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../loading"
4
+
5
+ # Whether two callbacks are what running one declaration twice registers: the
6
+ # same kind of callback, with the same filter, under the same conditions. A
7
+ # proc is the same when it is written in the same place, an object when it is
8
+ # of the same class and holds the same values -- a validator, with the same
9
+ # attributes and options -- or, when it holds nothing in instance variables,
10
+ # when it says it equals the other, as a string or a number does. A class is only ever itself, and so are
11
+ # `true`, `false` and `nil`.
12
+ #
13
+ # Values are compared down to a fixed depth and by identity below it, which
14
+ # also ends the comparison of a value that holds itself.
15
+ module Evilution::Integration::Loading::CallbackEquivalence
16
+ COMPARED = %i[@filter @if @unless].freeze
17
+ MAX_DEPTH = 8
18
+ private_constant :COMPARED, :MAX_DEPTH
19
+
20
+ def self.call(callback, other)
21
+ return false if callback.respond_to?(:kind) && callback.kind != other.kind
22
+
23
+ COMPARED.all? { |ivar| same?(callback.instance_variable_get(ivar), other.instance_variable_get(ivar), 0) }
24
+ end
25
+
26
+ def self.same?(value, other, depth)
27
+ return true if value.equal?(other)
28
+ return false if depth > MAX_DEPTH || value.is_a?(Module) || !value.instance_of?(other.class)
29
+ return value == other if self_compared?(value)
30
+
31
+ same_parts?(parts(value), parts(other), depth + 1)
32
+ end
33
+
34
+ # A value holding nothing in instance variables but saying itself what it
35
+ # equals -- a symbol, a string, a number, a range, a Struct, a Data, a
36
+ # Time, a Set -- keeps what it is made of where only its own equality reads
37
+ # it.
38
+ def self.self_compared?(value)
39
+ case value
40
+ when Proc, Array, Hash then false
41
+ else value.instance_variables.empty? && value.class.instance_method(:==).owner != BasicObject
42
+ end
43
+ end
44
+
45
+ def self.same_parts?(values, others, depth)
46
+ values.length == others.length && values.zip(others).all? { |value, other| same?(value, other, depth) }
47
+ end
48
+
49
+ # What a value is made of: where a proc was written, the elements of an
50
+ # array, the keys and values of a hash, the instance variables of anything
51
+ # else. A proc that cannot say where it was written is only ever itself.
52
+ def self.parts(value)
53
+ case value
54
+ when Proc then [value.source_location || value.object_id]
55
+ when Array then value
56
+ when Hash then [value.keys, value.values]
57
+ else value.instance_variables.flat_map { |name| [name, value.instance_variable_get(name)] }
58
+ end
59
+ end
60
+
61
+ private_class_method :same?, :self_compared?, :same_parts?, :parts
62
+ end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../loading"
4
+ require_relative "callback_equivalence"
5
+ require_relative "../../diagnostic"
4
6
 
5
7
  # Runs a callback declaration again and puts what it registers where the
6
8
  # callbacks it registered before were.
@@ -17,49 +19,94 @@ require_relative "../loading"
17
19
  # new ones, and everything else stays where it was, including what subclasses
18
20
  # added themselves. See BodyCallNeutralizer for the source that calls this.
19
21
  #
20
- # "Last time" is the file's own load the first time round, and those
21
- # callbacks are recognised by where their procs were written (or by having
22
- # been dropped as duplicates). After that the module goes by what it
23
- # registered itself: a mutation can add or remove lines, so a proc created by
24
- # the mutated source may report a line that belongs to another declaration of
25
- # the real file.
22
+ # "Last time" is the file's own load the first time round. Those callbacks
23
+ # are recognised by where their procs were written, or by having been dropped
24
+ # as duplicates. A validator under a symbol condition, or none -- `validates
25
+ # :for_lift, inclusion: { in: [true] }` -- has no proc to go by, and the
26
+ # mutated declaration registers it with other values. So when the module is
27
+ # given the declaration as it is written in the file, it runs that again
28
+ # first, in the class body it is written in, so that it reads the constants
29
+ # and local variables the body sees: what it registers then is the same,
30
+ # value for value, as what it registered on load, and takes its place -- one
31
+ # callback for each it registers, and none the class inherited. After that
32
+ # the module goes by what
33
+ # it registered itself: a mutation can add or remove lines, so a proc created
34
+ # by the mutated source may report a line that belongs to another declaration
35
+ # of the real file.
36
+ #
37
+ # A declaration that now registers nothing -- `inclusion: nil`, `presence:
38
+ # false` -- leaves nothing in the place of what it registered before, and
39
+ # what it registers on a later run goes back to that place.
26
40
  module Evilution::Integration::Loading::CallbackRedeclaration
27
41
  CONDITIONS = %i[@if @unless].freeze
28
42
  private_constant :CONDITIONS
29
43
 
30
44
  # klass: the class whose body the declaration is written in. file, lines:
31
- # where the declaration is in the file as it was loaded.
32
- def self.call(klass, file, lines)
45
+ # where the declaration is in the file as it was loaded. original: the
46
+ # declaration's source as written there, or nil. body: the binding of the
47
+ # class body it is written in, or nil to run it in the class alone.
48
+ def self.call(klass, file, lines, original = nil, body = nil, &)
33
49
  return yield unless klass.respond_to?(:__callbacks)
34
50
 
51
+ origin = Origin.new(file, lines)
52
+ rerun_original(klass, origin, Original.new(original, body)) if original && !ledger.registered.key?(key(klass, origin))
53
+ redeclare(klass, origin, &)
54
+ end
55
+
56
+ # rerun: whether the declaration runs as written in the file, registering
57
+ # what it did on load once more.
58
+ def self.redeclare(klass, origin, rerun: false)
35
59
  before = klass.__callbacks.transform_values(&:to_a)
36
60
  result = yield
37
- swaps = Settlement.new(klass, Origin.new(file, lines), registered).call(before)
61
+ swaps = Settlement.new(klass, origin, ledger, key(klass, origin), rerun: rerun).call(before)
38
62
  swap_validators(klass, swaps)
39
63
  result
40
64
  end
41
65
 
42
- # What earlier calls registered, by declaration:
43
- # { [class, file, first line] => callbacks }.
44
- def self.registered
45
- @registered ||= {}
66
+ # A declaration that cannot be run again -- one that raises the second
67
+ # time -- leaves the mutated one to be settled without it, against what
68
+ # the file's load left behind, which may keep the original beside it.
69
+ def self.rerun_original(klass, origin, original)
70
+ redeclare(klass, origin, rerun: true) do
71
+ original.run(klass, origin)
72
+ rescue StandardError => e
73
+ Evilution::Diagnostic.warn(
74
+ "[evilution] could not run #{origin.file}:#{origin.lines.first} again (#{e.class}: #{e.message}); " \
75
+ "its mutations are applied beside what it registered when the file loaded"
76
+ )
77
+ end
78
+ end
79
+
80
+ def self.key(klass, origin)
81
+ [klass, origin.file, origin.lines.first]
82
+ end
83
+
84
+ def self.ledger
85
+ @ledger ||= Ledger.new({}, {})
46
86
  end
47
87
 
48
88
  # `validates` also lists its validator for reflection (`Model.validators`),
49
- # once per class; the old one is exchanged for the new there too.
89
+ # once per class; the old one is exchanged for the new there too, or
90
+ # dropped when nothing took its place. A new one that takes no old one's
91
+ # place is listed by the class alone, so it is added to the lists of its
92
+ # descendants, which took theirs from the class when they were defined.
50
93
  def self.swap_validators(klass, swaps)
51
94
  return unless klass.respond_to?(:_validators)
52
95
 
53
- pairs = swaps.select { |old, new| old && new }.map { |old, new| [old.filter, new.filter] }
96
+ pairs = swaps.map { |old, new| [old && old.filter, new && new.filter] }
54
97
  [klass, *descendants(klass)].each do |target|
55
- target._validators.each_value do |validators|
56
- pairs.each { |old, new| exchange(validators, old, new) }
98
+ target._validators.each do |attribute, validators|
99
+ pairs.each { |old, new| exchange(validators, old, new, klass._validators.fetch(attribute, [])) }
57
100
  end
58
101
  end
59
102
  end
60
103
 
61
- def self.exchange(validators, old, new)
62
- return unless validators.any? { |validator| validator.equal?(old) }
104
+ def self.exchange(validators, old, new, listed)
105
+ return validators << new if old.nil? && Identity.member?(listed, new) && !Identity.member?(validators, new)
106
+
107
+ index = validators.index { |validator| validator.equal?(old) }
108
+ return unless index
109
+ return validators.delete_at(index) unless new
63
110
 
64
111
  validators.reject! { |validator| validator.equal?(new) }
65
112
  validators[validators.index { |validator| validator.equal?(old) }] = new
@@ -69,7 +116,27 @@ module Evilution::Integration::Loading::CallbackRedeclaration
69
116
  klass.subclasses.flat_map { |subclass| [subclass, *descendants(subclass)] }
70
117
  end
71
118
 
72
- private_class_method :registered, :swap_validators, :exchange, :descendants
119
+ private_class_method :redeclare, :rerun_original, :key, :ledger, :swap_validators, :exchange, :descendants
120
+
121
+ # What earlier calls did, by declaration -- [class, file, first line]:
122
+ # the callbacks each registered, and, for one that registered none, the
123
+ # callback of each chain that followed what it registered before, if any.
124
+ Ledger = Data.define(:registered, :vacancies)
125
+ private_constant :Ledger
126
+
127
+ # The declaration as written in the file, and the binding of the class body
128
+ # it is written in, if known.
129
+ Original = Data.define(:source, :body) do
130
+ def run(klass, origin)
131
+ return klass.class_eval(source, origin.file, origin.lines.first) unless body
132
+
133
+ body.eval(source, origin.file, origin.lines.first)
134
+ end
135
+ end
136
+ private_constant :Original
137
+
138
+ Equivalence = Evilution::Integration::Loading::CallbackEquivalence
139
+ private_constant :Equivalence
73
140
 
74
141
  # Callbacks are told apart by identity throughout: two that compare equal
75
142
  # are still two registrations.
@@ -87,54 +154,89 @@ module Evilution::Integration::Loading::CallbackRedeclaration
87
154
  # Rebuilds the chains of a class and its descendants after one declaration
88
155
  # has run again.
89
156
  class Settlement
90
- def initialize(klass, origin, registered)
157
+ def initialize(klass, origin, ledger, key, rerun:)
91
158
  @klass = klass
92
159
  @origin = origin
93
- @registered = registered
94
- @key = [klass, origin.file, origin.lines.first]
160
+ @registered = ledger.registered
161
+ @vacancies = ledger.vacancies[key] ||= {}
162
+ @key = key
163
+ @rerun = rerun
95
164
  end
96
165
 
97
166
  # before: { chain name => callbacks } as they were. Returns [old, new]
98
- # callback pairs for what changed hands.
167
+ # callback pairs for what changed hands, nil standing for no callback.
99
168
  def call(before)
100
169
  previous = @registered[@key]
101
170
  mine = []
171
+ changed = false
102
172
  swaps = before.flat_map do |name, callbacks|
103
173
  stale, fresh = settle(name, callbacks, previous)
104
174
  mine.concat(fresh)
105
- stale.zip(fresh)
175
+ changed ||= !stale.empty?
176
+ pairs(stale, fresh)
106
177
  end
107
- @registered[@key] = mine unless mine.empty?
178
+ @registered[@key] = mine if changed || !mine.empty?
108
179
  swaps
109
180
  end
110
181
 
111
182
  private
112
183
 
184
+ def pairs(stale, fresh)
185
+ return stale.zip(fresh) if stale.length >= fresh.length
186
+
187
+ fresh.zip(stale).map(&:reverse)
188
+ end
189
+
113
190
  def settle(name, before, previous)
114
191
  after = @klass.__callbacks[name].to_a
115
192
  fresh = Identity.without(after, before)
116
- return [[], []] if fresh.empty?
193
+ claimed = @rerun ? claim(name, before, fresh) : []
194
+ stale = before.select { |callback| stale?(callback, previous, claimed) || !Identity.member?(after, callback) }
195
+ return [[], []] if fresh.empty? && stale.empty?
117
196
 
118
- stale = before.select { |callback| stale?(callback, previous) || !Identity.member?(after, callback) }
119
- order = reorder(before, stale, fresh)
197
+ order = reorder(name, before, stale, fresh)
120
198
  @klass.send(:__update_callbacks, name) { |target, chain| rebuild(target, name, chain, order, after) }
121
199
  [stale, fresh]
122
200
  end
123
201
 
124
202
  # What this declaration registered before: what an earlier call recorded
125
203
  # or, the first time, what was written on its lines -- unless an earlier
126
- # call for another declaration put it there.
127
- def stale?(callback, previous)
204
+ # call for another declaration put it there. Run as written in the file,
205
+ # the declaration registers the same callbacks as on load, so those are
206
+ # what it registered too; a mutated one registers others, whose procs may
207
+ # say they were written anywhere.
208
+ def stale?(callback, previous, claimed)
128
209
  return Identity.member?(previous, callback) if previous
210
+ return false if Identity.member?(@registered.values.flatten, callback)
211
+
212
+ @origin.declared?(callback) || Identity.member?(claimed, callback)
213
+ end
214
+
215
+ # The callbacks the declaration, run as written, registered on load: for
216
+ # each it registers now, the last equal one before it that no other is
217
+ # taken for. One the class inherited was registered by its parent, and
218
+ # one recorded for another declaration by that declaration.
219
+ def claim(name, before, fresh)
220
+ others = inherited(name) + @registered.values.flatten
221
+ candidates = Identity.without(before, others).reverse
222
+ fresh.each_with_object([]) do |callback, claimed|
223
+ match = candidates.find { |other| !Identity.member?(claimed, other) && Equivalence.call(other, callback) }
224
+ claimed << match if match
225
+ end
226
+ end
129
227
 
130
- @origin.declared?(callback) && !Identity.member?(@registered.values.flatten, callback)
228
+ def inherited(name)
229
+ parent = @klass.superclass if @klass.is_a?(Class)
230
+ parent.respond_to?(:__callbacks) ? parent.__callbacks[name].to_a : []
131
231
  end
132
232
 
133
- # The new callbacks take the place of the first old one; with no old one
134
- # to replace they stay where the declaration put them, at the end.
135
- def reorder(before, stale, fresh)
136
- return before + fresh if stale.empty?
233
+ # The new callbacks take the place of the first old one. With no old one
234
+ # to replace they go where an earlier run that registered nothing left a
235
+ # place, or stay where the declaration put them, at the end.
236
+ def reorder(name, before, stale, fresh)
237
+ return refill(name, before, fresh) if stale.empty?
137
238
 
239
+ vacate(name, before, stale) if fresh.empty?
138
240
  before.flat_map do |callback|
139
241
  next fresh if callback.equal?(stale.first)
140
242
 
@@ -142,6 +244,19 @@ module Evilution::Integration::Loading::CallbackRedeclaration
142
244
  end
143
245
  end
144
246
 
247
+ def vacate(name, before, stale)
248
+ start = before.index { |callback| callback.equal?(stale.first) }
249
+ @vacancies[name] = before.drop(start).find { |callback| !Identity.member?(stale, callback) }
250
+ end
251
+
252
+ def refill(name, before, fresh)
253
+ following = @vacancies.delete(name)
254
+ index = following && before.index { |callback| callback.equal?(following) }
255
+ return before + fresh unless index
256
+
257
+ before.take(index) + fresh + before.drop(index)
258
+ end
259
+
145
260
  def rebuild(target, name, chain, order, after)
146
261
  own = Identity.without(chain.to_a, after)
147
262
  chain.clear
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "../loading"
4
4
  require_relative "own_methods"
5
+ require_relative "../../class_tree"
5
6
 
6
7
  # Re-declares one statement of a concern's `included` block on the classes
7
8
  # that already include the concern, or of its `prepended` block on those that
@@ -25,14 +26,12 @@ module Evilution::Integration::Loading::ConcernRedeclaration
25
26
  KEY = :evilution_concern_redeclaration
26
27
  BLOCKS = { included: :@_included_block, prepended: :@_prepended_block }.freeze
27
28
 
28
- # Called through UnboundMethods: ObjectSpace hands over every class in the
29
- # VM, and one that defines `include?` or `superclass` of its own for its
30
- # own purposes must not be asked that question.
29
+ # Called through UnboundMethods: the class tree holds every class in the
30
+ # VM, and one that defines `include?` of its own for its own purposes must
31
+ # not be asked that question.
31
32
  INCLUDES = Module.instance_method(:include?)
32
- SUPERCLASS = Class.instance_method(:superclass)
33
- SINGLETON = Module.instance_method(:singleton_class?)
34
33
  ANCESTORS = Module.instance_method(:ancestors)
35
- private_constant :KEY, :BLOCKS, :INCLUDES, :SUPERCLASS, :SINGLETON, :ANCESTORS
34
+ private_constant :KEY, :BLOCKS, :INCLUDES, :ANCESTORS
36
35
 
37
36
  def self.skipping?
38
37
  Thread.current[KEY] == true
@@ -48,15 +47,20 @@ module Evilution::Integration::Loading::ConcernRedeclaration
48
47
  end
49
48
 
50
49
  # The classes that took the concern themselves, by including it or by
51
- # prepending it; a subclass inherits what its parent declares.
50
+ # prepending it; a subclass inherits what its parent declares. They are
51
+ # found going down the class tree from the top: the first class on a branch
52
+ # that has the concern took it, and the classes below it inherit it, so
53
+ # the branch ends there. The tree, unlike a walk of the heap, holds nothing
54
+ # but classes.
52
55
  def self.takers(concern, prepended:)
53
- ObjectSpace.each_object(Class).select do |klass|
54
- next false if SINGLETON.bind_call(klass) || !INCLUDES.bind_call(klass, concern)
56
+ found = []
57
+ Evilution::ClassTree.visit(BasicObject) do |klass|
58
+ next true unless INCLUDES.bind_call(klass, concern)
55
59
 
56
- parent = SUPERCLASS.bind_call(klass)
57
- inherited = !parent.nil? && INCLUDES.bind_call(parent, concern)
58
- !inherited && prepended?(klass, concern) == prepended
60
+ found << klass if prepended?(klass, concern) == prepended
61
+ false
59
62
  end
63
+ found
60
64
  end
61
65
 
62
66
  # A prepended module comes before the class in its ancestors, an included