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
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../spec_resolver"
4
+ require_relative "project_files"
5
+ require_relative "constant_references"
6
+ require_relative "spec_pattern"
7
+
8
+ # Ranks the test files that sit next to a nested source's missing 1:1 spec --
9
+ # the direct children of the source's mirror directories -- by evidence that
10
+ # they exercise the source's constant. Files with no such evidence are dropped.
11
+ # Only the given directories are scanned (not recursively) and constants are
12
+ # read from Prism's token stream (comments and strings never count), so the cost
13
+ # stays at one directory listing plus a lex of each sibling and, when the
14
+ # sibling does not name the subject, of its mirrored source.
15
+ class Evilution::SpecResolver::SiblingSpecRanker
16
+ # Ranks, best first.
17
+ NAMES_SUBJECT = 0 # file name or top-level describe constant is the subject
18
+ CALLS_SUBJECT = 1 # the sibling's own subject (its mirrored source) references the subject
19
+ MENTIONS_SUBJECT = 2 # the sibling mentions the subject constant
20
+
21
+ MINITEST_SUFFIX = "_test.rb"
22
+ TEST_PREFIX = "test_"
23
+
24
+ def initialize(test_suffix:, files: Evilution::SpecResolver::ProjectFiles)
25
+ @test_suffix = test_suffix
26
+ @files = files
27
+ end
28
+
29
+ # source_path: project-relative source file (e.g. app/models/a/b/c.rb).
30
+ # dirs: test directories to scan. spec_pattern: drops excluded test files
31
+ # before any of them is read. Returns ranked test file paths.
32
+ def call(source_path, dirs, spec_pattern: nil)
33
+ subject = Subject.new(source_path)
34
+ test_files = Evilution::SpecResolver::SpecPattern.filter(test_files_in(dirs), spec_pattern)
35
+ ranked = test_files.filter_map do |test_file|
36
+ rank = rank_of(test_file, subject)
37
+ [rank, test_file] if rank
38
+ end
39
+ ranked.sort.map(&:last)
40
+ end
41
+
42
+ private
43
+
44
+ Subject = Struct.new(:source_path) do
45
+ def name
46
+ @name ||= File.basename(source_path, ".rb")
47
+ end
48
+
49
+ # Constant spelling with case and underscores folded away, so an acronym
50
+ # (HTMLRenderer) matches its snake_case file name (html_renderer).
51
+ def key
52
+ @key ||= name.delete("_").downcase
53
+ end
54
+
55
+ def source_dir
56
+ File.dirname(source_path)
57
+ end
58
+
59
+ def constant?(constant)
60
+ constant.downcase == key
61
+ end
62
+ end
63
+ private_constant :Subject
64
+
65
+ def test_files_in(dirs)
66
+ dirs.flat_map { |dir| globs_for(dir).flat_map { |glob| @files.glob(glob) } }.uniq.sort
67
+ end
68
+
69
+ def globs_for(dir)
70
+ globs = ["#{dir}/*#{@test_suffix}"]
71
+ globs << "#{dir}/#{TEST_PREFIX}*.rb" if @test_suffix == MINITEST_SUFFIX
72
+ globs
73
+ end
74
+
75
+ def rank_of(test_file, subject)
76
+ return NAMES_SUBJECT if stem(test_file) == subject.name
77
+
78
+ references = references_in(test_file)
79
+ return NAMES_SUBJECT if names_subject?(references, subject)
80
+ return CALLS_SUBJECT if mentions?(references_in(mirrored_source(test_file, subject)), subject)
81
+ return MENTIONS_SUBJECT if mentions?(references, subject)
82
+
83
+ nil
84
+ end
85
+
86
+ def references_in(path)
87
+ Evilution::SpecResolver::ConstantReferences.new(@files.read(path))
88
+ end
89
+
90
+ def names_subject?(references, subject)
91
+ constant = references.describe_constant
92
+ constant ? subject.constant?(constant) : false
93
+ end
94
+
95
+ def mentions?(references, subject)
96
+ references.constants.any? { |constant| subject.constant?(constant) }
97
+ end
98
+
99
+ # The source a sibling test mirrors: same directory as the subject, named
100
+ # after the test file (warnings_spec.rb -> <subject dir>/warnings.rb).
101
+ def mirrored_source(test_file, subject)
102
+ "#{subject.source_dir}/#{stem(test_file)}.rb"
103
+ end
104
+
105
+ def stem(test_file)
106
+ base = File.basename(test_file)
107
+ return base.delete_suffix(@test_suffix) if base.end_with?(@test_suffix)
108
+
109
+ base.delete_suffix(".rb").delete_prefix(TEST_PREFIX)
110
+ end
111
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../spec_resolver"
4
+
5
+ # The `spec_pattern` glob a run may narrow test files with. Shared by the
6
+ # resolver and the sibling ranker so both apply the same matching rules, and so
7
+ # the ranker can drop excluded files before reading them.
8
+ module Evilution::SpecResolver::SpecPattern
9
+ FLAGS = File::FNM_PATHNAME | File::FNM_EXTGLOB
10
+
11
+ module_function
12
+
13
+ # Paths unchanged when pattern is nil.
14
+ def filter(paths, pattern)
15
+ return paths unless pattern
16
+
17
+ paths.select { |path| File.fnmatch?(pattern, path, FLAGS) }
18
+ end
19
+ end
@@ -8,40 +8,57 @@ class Evilution::SpecResolver
8
8
  # rather than mirroring the lib/ tree 1:1 (EV-z7f5 / GH #1325).
9
9
  CONVENTIONAL_SUBDIRS = %w[unit lib].freeze
10
10
  MINITEST_SUFFIX = "_test.rb"
11
+ # Directory separators a stripped source path needs before its mirror
12
+ # directory is scanned for related sibling tests (see #sibling_specs).
13
+ MIN_SIBLING_DEPTH = 2
11
14
 
12
15
  def initialize(test_dir: "spec", test_suffix: "_spec.rb", request_dir: "requests")
13
16
  @test_dir = test_dir
14
17
  @test_suffix = test_suffix
15
18
  @request_dir = request_dir
19
+ @sibling_ranker = SiblingSpecRanker.new(test_suffix: test_suffix)
16
20
  end
17
21
 
22
+ # Resolution order, first hit wins: a test named after the source (the 1:1
23
+ # mirror file, then the non-mirrored naming conventions), then the best
24
+ # related test among the siblings in the source's own mirror directory, then
25
+ # the parent directories' tests.
18
26
  def call(source_path, spec_pattern: nil)
19
27
  return nil if source_path.nil? || source_path.empty?
20
28
 
21
29
  normalized = normalize_path(source_path)
22
- candidates = candidate_test_paths(normalized)
23
- candidates = filter_by_pattern(candidates, spec_pattern) if spec_pattern
24
- candidates.find { |path| project_relative_exists?(path) }
30
+ dedicated_spec(normalized, spec_pattern) ||
31
+ sibling_specs(normalized, spec_pattern).first ||
32
+ parent_spec(normalized, spec_pattern)
25
33
  end
26
34
 
27
35
  def resolve_all(source_paths)
28
36
  Array(source_paths).filter_map { |path| call(path) }.uniq
29
37
  end
30
38
 
31
- # Like #call, but returns an ARRAY of test files and additionally covers the
32
- # dir-grouped layout (a source file's tests live in a directory named after
33
- # the source basename, e.g. lib/x/branch.rb -> test/unit/branch/*_test.rb,
34
- # rather than a single mirror file). The deterministic file mirror from #call
35
- # always wins; only when no mirror file exists is the first matching grouped
36
- # directory expanded into its test files (EV-bi41). Returns nil when nothing
37
- # resolves.
39
+ # Like #call, but returns an ARRAY of test files: every related sibling test
40
+ # (ranked) instead of only the best one, and additionally the dir-grouped
41
+ # layout (a source file's tests live in a directory named after the source
42
+ # basename, e.g. lib/x/branch.rb -> test/unit/branch/*_test.rb, rather than a
43
+ # single mirror file). A grouped directory is dedicated to the source, so it
44
+ # ranks right after a test named after the source and above the related
45
+ # siblings and the parent directories' tests (EV-bi41). Returns nil when
46
+ # nothing resolves.
38
47
  def resolve_specs(source_path, spec_pattern: nil)
39
48
  return nil if source_path.nil? || source_path.empty?
40
49
 
41
- file = call(source_path, spec_pattern: spec_pattern)
42
- return [file] if file
50
+ normalized = normalize_path(source_path)
51
+ dedicated = dedicated_spec(normalized, spec_pattern)
52
+ return [dedicated] if dedicated
53
+
54
+ grouped = resolve_grouped_dir(normalized, spec_pattern: spec_pattern)
55
+ return grouped if grouped
56
+
57
+ siblings = sibling_specs(normalized, spec_pattern)
58
+ return siblings unless siblings.empty?
43
59
 
44
- resolve_grouped_dir(source_path, spec_pattern: spec_pattern)
60
+ parent = parent_spec(normalized, spec_pattern)
61
+ parent ? [parent] : nil
45
62
  end
46
63
 
47
64
  # Best-guess candidate for an unresolved source, found by basename glob
@@ -55,51 +72,54 @@ class Evilution::SpecResolver
55
72
  stem = File.basename(normalize_path(source_path), ".rb")
56
73
  return nil if stem.empty?
57
74
 
58
- suggestion_globs(stem).flat_map { |glob| glob_relative(glob) }.uniq.min_by(&:length)
75
+ suggestion_globs(stem).flat_map { |glob| ProjectFiles.glob(glob) }.uniq.min_by(&:length)
59
76
  end
60
77
 
61
78
  private
62
79
 
63
- # Glob for project-relative paths. Mirrors #call's project_relative_exists?
64
- # contract: when run inside an isolated worker chdir'd into a sandbox, glob
65
- # against PROJECT_ROOT so suggestions still find real project files. base:
66
- # already yields paths relative to the root, matching the CWD-glob shape.
67
- def glob_relative(glob)
68
- return Dir.glob(glob) unless Evilution.in_isolated_worker?
69
-
70
- Dir.glob(glob, base: Evilution::PROJECT_ROOT)
80
+ def first_existing(candidates, spec_pattern)
81
+ SpecPattern.filter(candidates, spec_pattern).find { |path| ProjectFiles.exist?(path) }
71
82
  end
72
83
 
73
- def suggestion_globs(stem)
74
- globs = ["#{@test_dir}/**/*#{stem}*#{@test_suffix}"]
75
- globs << "#{@test_dir}/**/test_#{stem}.rb" if @test_suffix == MINITEST_SUFFIX
76
- globs
84
+ # A test named after the source: the 1:1 mirror (plus the controller ->
85
+ # request mapping), then the non-mirrored naming conventions.
86
+ def dedicated_spec(normalized, spec_pattern)
87
+ first_existing(primary_candidates(normalized) + convention_candidates(strip_source_prefix(normalized)), spec_pattern)
77
88
  end
78
89
 
79
- # Existence check that succeeds against the current CWD. When the caller
80
- # is an isolated worker that chdir'd into a per-mutation sandbox (Evilution
81
- # signals this via in_isolated_worker?), also try PROJECT_ROOT so the
82
- # sandbox CWD does not break spec resolution (EV-wqxu / GH #1278).
83
- def project_relative_exists?(path)
84
- return true if File.exist?(path)
85
- return false unless Evilution.in_isolated_worker?
90
+ def parent_spec(normalized, spec_pattern)
91
+ first_existing(primary_candidates(normalized).flat_map { |c| parent_fallback_candidates(c) }.uniq, spec_pattern)
92
+ end
86
93
 
87
- File.exist?(File.expand_path(path, Evilution::PROJECT_ROOT))
94
+ # Related tests among the direct children of the source's mirror
95
+ # directories, ranked by SiblingSpecRanker. Only a source at least two
96
+ # directories below its strip prefix has a mirror directory of its own: one
97
+ # level down (app/models/user.rb, lib/<gem>/x.rb) the mirror directory is a
98
+ # whole test bucket (spec/models, spec/<gem>), so nothing is scanned. The
99
+ # bare-basename variant is skipped too, so the test root is never scanned.
100
+ def sibling_specs(normalized, spec_pattern)
101
+ stripped = strip_source_prefix(normalized)
102
+ return [] if stripped.count("/") < MIN_SIBLING_DEPTH
103
+
104
+ variants = mirror_variants(stripped).select { |variant| variant.include?("/") }
105
+ dirs = variants.flat_map { |variant| roots.map { |root| "#{root}/#{File.dirname(variant)}" } }
106
+ @sibling_ranker.call(normalized, dirs.uniq.select { |dir| ProjectFiles.directory?(dir) }, spec_pattern: spec_pattern)
88
107
  end
89
108
 
90
- def filter_by_pattern(candidates, pattern)
91
- candidates.select { |path| File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB) }
109
+ def suggestion_globs(stem)
110
+ globs = ["#{@test_dir}/**/*#{stem}*#{@test_suffix}"]
111
+ globs << "#{@test_dir}/**/test_#{stem}.rb" if @test_suffix == MINITEST_SUFFIX
112
+ globs
92
113
  end
93
114
 
94
115
  # Dir-grouped resolution: find the first candidate directory that exists and
95
- # expand it into its test files. Ranked below #call's file mirrors (callers
96
- # try #call first), so a 1:1 spec always wins when present.
97
- def resolve_grouped_dir(source_path, spec_pattern: nil)
98
- dir = directory_candidates(normalize_path(source_path)).find { |c| project_relative_dir?(c) }
116
+ # expand it into its test files. Ranked below a test named after the source,
117
+ # so a 1:1 spec always wins when present.
118
+ def resolve_grouped_dir(normalized, spec_pattern: nil)
119
+ dir = directory_candidates(normalized).find { |c| ProjectFiles.directory?(c) }
99
120
  return nil unless dir
100
121
 
101
- files = test_files_in(dir)
102
- files = filter_by_pattern(files, spec_pattern) if spec_pattern
122
+ files = SpecPattern.filter(test_files_in(dir), spec_pattern)
103
123
  files.empty? ? nil : files
104
124
  end
105
125
 
@@ -130,15 +150,7 @@ class Evilution::SpecResolver
130
150
  # minitest/test-unit, the `test_` prefix convention. Sorted for determinism.
131
151
  def test_files_in(dir)
132
152
  globs = ["#{dir}/**/*#{@test_suffix}", ("#{dir}/**/test_*.rb" if @test_suffix == MINITEST_SUFFIX)]
133
- globs.compact.flat_map { |glob| glob_relative(glob) }.uniq.sort
134
- end
135
-
136
- # Directory analogue of #project_relative_exists?.
137
- def project_relative_dir?(path)
138
- return true if File.directory?(path)
139
- return false unless Evilution.in_isolated_worker?
140
-
141
- File.directory?(File.expand_path(path, Evilution::PROJECT_ROOT))
153
+ globs.compact.flat_map { |glob| ProjectFiles.glob(glob) }.uniq.sort
142
154
  end
143
155
 
144
156
  def normalize_path(path)
@@ -150,23 +162,17 @@ class Evilution::SpecResolver
150
162
  path
151
163
  end
152
164
 
153
- def candidate_test_paths(source_path)
154
- base = source_path.sub(/\.rb\z/, @test_suffix)
155
- prefix = STRIPPABLE_PREFIXES.find { |p| source_path.start_with?(p) }
156
- stripped = prefix ? base.delete_prefix(prefix) : base
157
-
165
+ # The 1:1 mirrors of the source (plus the controller -> request mapping).
166
+ def primary_candidates(source_path)
167
+ stripped = strip_source_prefix(source_path)
158
168
  primary = mirror_candidates(stripped)
159
- primary.unshift(controller_to_request_test(stripped)) if prefix
160
- primary.compact!
161
-
162
- fallbacks = primary.flat_map { |c| parent_fallback_candidates(c) }
163
-
164
- (primary + fallbacks + convention_candidates(stripped)).uniq
169
+ primary.unshift(controller_to_request_test(stripped)) if STRIPPABLE_PREFIXES.any? { |p| source_path.start_with?(p) }
170
+ primary.compact
165
171
  end
166
172
 
167
173
  # Non-mirrored Test::Unit/minitest filename conventions: the `test_<name>.rb`
168
- # prefix and the flat namespace-prefixed form. Both rank below the mirrored
169
- # layouts assembled in #candidate_test_paths.
174
+ # prefix and the flat namespace-prefixed form. Both rank right below the 1:1
175
+ # mirrors (see #dedicated_spec) and above the related sibling tests.
170
176
  def convention_candidates(stripped)
171
177
  prefix_convention_candidates(stripped) + flat_prefixed_candidates(stripped)
172
178
  end
@@ -219,7 +225,8 @@ class Evilution::SpecResolver
219
225
  # basename forms (test_timed_stack.rb) that collide across namespaces and are
220
226
  # already covered by #prefix_convention_candidates. Top-level sources have no
221
227
  # namespace to flatten, so they produce nothing here. Ranked below the mirrored
222
- # layouts (appended last) so a 1:1 file always wins. Minitest suffix only.
228
+ # layouts (see #convention_candidates) so a 1:1 file always wins. Minitest
229
+ # suffix only.
223
230
  def flat_prefixed_candidates(stripped)
224
231
  return [] unless @test_suffix == MINITEST_SUFFIX
225
232
  return [] unless stripped.include?("/")
@@ -265,3 +272,7 @@ class Evilution::SpecResolver
265
272
  candidates
266
273
  end
267
274
  end
275
+
276
+ require_relative "spec_resolver/project_files"
277
+ require_relative "spec_resolver/spec_pattern"
278
+ require_relative "spec_resolver/sibling_spec_ranker"
@@ -8,23 +8,35 @@ class Evilution::SpecSelector
8
8
  @spec_mappings = spec_mappings || {}
9
9
  @spec_pattern = spec_pattern
10
10
  @spec_resolver = spec_resolver
11
+ @resolved = {}
11
12
  end
12
13
 
14
+ # Answers are remembered per source for the life of this selector, because
15
+ # the integrations ask once per mutation and the resolver may read every
16
+ # sibling test to rank them. A selector belongs to one run (it is built with
17
+ # the run's Config), so a long-running process such as the MCP server never
18
+ # reuses an answer across runs. The baseline resolves in the parent process
19
+ # before any mutation fork, so forked workers inherit the warmed answers.
13
20
  def call(source_path)
14
21
  return @spec_files unless @spec_files.empty?
22
+ return @resolved[source_path] if @resolved.key?(source_path)
15
23
 
24
+ @resolved[source_path] = resolve(source_path)
25
+ end
26
+
27
+ private
28
+
29
+ def resolve(source_path)
16
30
  mapped = mapping_for(source_path)
17
31
  if mapped
18
- existing = mapped.select { |path| project_relative_exists?(path) }
19
- return existing unless existing.empty?
32
+ existing = mapped.select { |path| Evilution::SpecResolver::ProjectFiles.exist?(path) }
33
+ return existing.freeze unless existing.empty?
20
34
  end
21
35
 
22
36
  resolved = resolve_via_resolver(source_path)
23
- resolved && !resolved.empty? ? resolved : nil
37
+ resolved && !resolved.empty? ? resolved.dup.freeze : nil
24
38
  end
25
39
 
26
- private
27
-
28
40
  # Prefer the array-returning #resolve_specs, but fall back to the older single-file #call contract so a custom
29
41
  # resolver that only implements #call keeps working.
30
42
  def resolve_via_resolver(source_path)
@@ -50,12 +62,4 @@ class Evilution::SpecSelector
50
62
  end
51
63
  normalized.delete_prefix("./")
52
64
  end
53
-
54
- # Same semantics as Evilution::SpecResolver#project_relative_exists?
55
- def project_relative_exists?(path)
56
- return true if File.exist?(path)
57
- return false unless Evilution.in_isolated_worker?
58
-
59
- File.exist?(File.expand_path(path, Evilution::PROJECT_ROOT))
60
- end
61
65
  end
@@ -17,10 +17,14 @@
17
17
  # A subject written in a concern's `included` or `prepended` block is marked
18
18
  # `concern_hook`: the block runs in each class that takes the concern, so a
19
19
  # mutation of it has to be run again on the classes that took it already.
20
+ #
21
+ # byte_range: where `source` sits in the file, in bytes; nil when the subject
22
+ # was not taken from a parsed file.
20
23
  class Evilution::Subject
21
- attr_reader :name, :file_path, :line_number, :source, :node, :kind
24
+ attr_reader :name, :file_path, :line_number, :source, :node, :kind, :byte_range
22
25
 
23
- def initialize(name:, file_path:, line_number:, source:, node:, kind: :method, concern_hook: false)
26
+ def initialize(name:, file_path:, line_number:, source:, node:, kind: :method, concern_hook: false,
27
+ byte_range: nil)
24
28
  @name = name
25
29
  @file_path = file_path
26
30
  @line_number = line_number
@@ -28,6 +32,7 @@ class Evilution::Subject
28
32
  @node = node
29
33
  @kind = kind
30
34
  @concern_hook = concern_hook
35
+ @byte_range = byte_range
31
36
  end
32
37
 
33
38
  def concern_hook?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Evilution
4
- VERSION = "1.5.0"
4
+ VERSION = "1.6.0"
5
5
  end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "securerandom"
5
+ require "tmpdir"
6
+ require_relative "../worker_database"
7
+
8
+ # Where the workers of one parallel pass keep their own copies of the shared
9
+ # SQLite test database: a directory under the system temp dir, chosen by the
10
+ # parent before the workers fork, with a subdirectory per worker process, so a
11
+ # worker that replaces a dead one never opens what the dead one left. Nothing
12
+ # is created until a worker copies.
13
+ #
14
+ # The parent deletes a worker's copies once that worker has been reaped,
15
+ # before its replacement starts, so a pass that replaces workers often never
16
+ # holds more copies than it has worker slots. It removes the directory when
17
+ # the pass ends, also when it ends on an exception or an interrupt. Only a
18
+ # parent killed outright (SIGKILL) leaves it behind, in the temp dir. It is
19
+ # deliberately not registered with TempDirTracker: a forked child that dies
20
+ # runs the inherited at_exit cleanup, which would delete the databases the
21
+ # other workers are still using.
22
+ class Evilution::WorkerDatabase::Copies
23
+ # Copies are off: every worker keeps the database the preload connected to.
24
+ class None
25
+ def enabled?
26
+ false
27
+ end
28
+
29
+ def discard(_pid)
30
+ nil
31
+ end
32
+
33
+ def remove
34
+ nil
35
+ end
36
+ end
37
+
38
+ NONE = None.new.freeze
39
+
40
+ attr_reader :root
41
+
42
+ def initialize(root: File.join(Dir.tmpdir, "evilution-worker-db-#{Process.pid}-#{SecureRandom.hex(4)}"))
43
+ @root = root
44
+ end
45
+
46
+ def enabled?
47
+ true
48
+ end
49
+
50
+ # Runs in the worker: copies the database into this process's directory and
51
+ # returns the copy's path.
52
+ def provision(source)
53
+ destination = File.join(worker_dir(Process.pid), File.basename(source))
54
+ Evilution::WorkerDatabase::SqliteCopy.call(source, destination)
55
+ destination
56
+ end
57
+
58
+ # Runs in the parent, once the worker process `pid` has exited: nothing
59
+ # holds its copies open any more.
60
+ def discard(pid)
61
+ FileUtils.rm_rf(worker_dir(pid))
62
+ end
63
+
64
+ def remove
65
+ FileUtils.rm_rf(root)
66
+ end
67
+
68
+ private
69
+
70
+ def worker_dir(pid)
71
+ File.join(root, "worker-#{pid}")
72
+ end
73
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../worker_database"
4
+
5
+ # The part of a database's schema that tells whether a worker can use it in
6
+ # place of the preloaded one: its tables and, when it records them, its applied
7
+ # migrations. Read through the connection API common to ActiveRecord 6.1-8.x,
8
+ # so it works without migration files and with schema.rb or structure.sql.
9
+ class Evilution::WorkerDatabase::Schema
10
+ attr_reader :tables, :versions
11
+
12
+ def self.of(base)
13
+ base.connection_pool.with_connection do |conn|
14
+ new(tables: conn.tables, versions: versions(conn, base.schema_migrations_table_name))
15
+ end
16
+ end
17
+
18
+ def self.versions(conn, table)
19
+ return [] unless conn.table_exists?(table)
20
+
21
+ conn.select_values("SELECT version FROM #{conn.quote_table_name(table)}")
22
+ end
23
+ private_class_method :versions
24
+
25
+ def initialize(tables:, versions:)
26
+ @tables = tables.sort
27
+ @versions = versions.map(&:to_s).sort
28
+ end
29
+
30
+ # What this database lacks compared to the reference, or nil when it has
31
+ # everything. Extra tables and migrations are fine.
32
+ def missing_from(reference)
33
+ tables = reference.tables - self.tables
34
+ return "missing tables: #{tables.join(", ")}" unless tables.empty?
35
+
36
+ versions = reference.versions - self.versions
37
+ "missing migrations: #{versions.join(", ")}" unless versions.empty?
38
+ end
39
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require_relative "../worker_database"
5
+
6
+ # Copies a SQLite database for one parallel worker.
7
+ #
8
+ # The copy goes through SQLite's online backup API rather than a file copy: it
9
+ # is a consistent snapshot that includes what the write-ahead log holds but has
10
+ # not yet been checkpointed into the main file, and it is compact, so a large
11
+ # leftover WAL file is not duplicated for every worker. The sqlite3 gem is the
12
+ # one the app's SQLite adapter already depends on.
13
+ module Evilution::WorkerDatabase::SqliteCopy
14
+ class Incomplete < StandardError; end
15
+
16
+ # Files SQLite keeps beside a database. A copy left at the destination by an
17
+ # earlier worker must not lend its log or journal to the new one.
18
+ COMPANIONS = ["", "-wal", "-shm", "-journal"].freeze
19
+
20
+ module_function
21
+
22
+ # A SQLite database in a plain file. An in-memory database is never shared
23
+ # between processes, and a file: URI carries parameters a copy could not keep.
24
+ def copyable?(configuration_hash)
25
+ database = configuration_hash[:database]
26
+ configuration_hash[:adapter].to_s.include?("sqlite") && database.is_a?(String) && !database.empty? &&
27
+ database != ":memory:" && !database.start_with?("file:")
28
+ end
29
+
30
+ def call(source, destination)
31
+ raise Errno::ENOENT, source unless File.file?(source)
32
+
33
+ require "sqlite3" unless defined?(::SQLite3::Backup)
34
+ FileUtils.mkdir_p(File.dirname(destination))
35
+ COMPANIONS.each { |suffix| FileUtils.rm_f("#{destination}#{suffix}") }
36
+ backup(source, destination)
37
+ end
38
+
39
+ def backup(source, destination)
40
+ from = ::SQLite3::Database.new(source, readonly: true)
41
+ begin
42
+ to = ::SQLite3::Database.new(destination)
43
+ begin
44
+ copy_pages(from, to, source)
45
+ ensure
46
+ to.close
47
+ end
48
+ ensure
49
+ from.close
50
+ end
51
+ end
52
+
53
+ def copy_pages(from, to, source)
54
+ backup = ::SQLite3::Backup.new(to, "main", from, "main")
55
+ begin
56
+ status = backup.step(-1)
57
+ ensure
58
+ backup.finish
59
+ end
60
+ return if status == ::SQLite3::Constants::ErrorCode::DONE
61
+
62
+ raise Incomplete, "copying #{source} stopped with SQLite status #{status}"
63
+ end
64
+ private_class_method :backup, :copy_pages
65
+ end