henitai 0.5.3 → 0.7.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 (141) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +332 -1
  3. data/README.md +123 -19
  4. data/assets/schema/henitai.schema.json +26 -0
  5. data/lib/henitai/arid_node_filter.rb +4 -19
  6. data/lib/henitai/bootstrap_error_message.rb +38 -0
  7. data/lib/henitai/canonical_report_merger.rb +23 -2
  8. data/lib/henitai/checkpoint_reporter.rb +5 -0
  9. data/lib/henitai/cli/operator_command.rb +10 -3
  10. data/lib/henitai/cli/run_command.rb +25 -7
  11. data/lib/henitai/cli/run_options.rb +1 -1
  12. data/lib/henitai/cli.rb +6 -2
  13. data/lib/henitai/composite_progress_reporter.rb +15 -2
  14. data/lib/henitai/configuration.rb +9 -1
  15. data/lib/henitai/configuration_validator/rules.rb +9 -0
  16. data/lib/henitai/configuration_validator/scalars.rb +22 -0
  17. data/lib/henitai/configuration_validator.rb +4 -2
  18. data/lib/henitai/constant_path.rb +48 -0
  19. data/lib/henitai/control_gate.rb +78 -0
  20. data/lib/henitai/control_run.rb +67 -0
  21. data/lib/henitai/coverage_bootstrapper.rb +46 -11
  22. data/lib/henitai/coverage_formatter.rb +26 -3
  23. data/lib/henitai/coverage_report_reader.rb +21 -0
  24. data/lib/henitai/equivalence_detector/operand_predicates.rb +16 -0
  25. data/lib/henitai/equivalence_detector.rb +46 -38
  26. data/lib/henitai/example_summary.rb +24 -0
  27. data/lib/henitai/execution_engine/env_scope.rb +3 -3
  28. data/lib/henitai/execution_engine/fork_server_scope.rb +57 -0
  29. data/lib/henitai/execution_engine.rb +109 -119
  30. data/lib/henitai/execution_provenance.rb +67 -0
  31. data/lib/henitai/fidelity_check.rb +173 -0
  32. data/lib/henitai/flaky_detection_baseline.rb +63 -0
  33. data/lib/henitai/flaky_retry_decision.rb +23 -0
  34. data/lib/henitai/flaky_test_registry.rb +63 -0
  35. data/lib/henitai/fork_server/client.rb +222 -0
  36. data/lib/henitai/fork_server/group_killer.rb +48 -0
  37. data/lib/henitai/fork_server/launcher.rb +38 -0
  38. data/lib/henitai/fork_server/mutant_runner.rb +110 -0
  39. data/lib/henitai/fork_server/protocol.rb +73 -0
  40. data/lib/henitai/fork_server/reaper.rb +24 -0
  41. data/lib/henitai/fork_server/runtime.rb +109 -0
  42. data/lib/henitai/fork_server/server.rb +108 -0
  43. data/lib/henitai/fork_server/server_process.rb +73 -0
  44. data/lib/henitai/fork_server/status.rb +52 -0
  45. data/lib/henitai/fork_server.rb +42 -0
  46. data/lib/henitai/generated_artifacts.rb +3 -0
  47. data/lib/henitai/git_diff_analyzer.rb +28 -11
  48. data/lib/henitai/history_checkpointer.rb +72 -0
  49. data/lib/henitai/incremental_filter.rb +20 -10
  50. data/lib/henitai/integration/base.rb +78 -19
  51. data/lib/henitai/integration/capped_log_sink.rb +34 -0
  52. data/lib/henitai/integration/child_bootstrap.rb +7 -0
  53. data/lib/henitai/integration/child_report_store.rb +65 -0
  54. data/lib/henitai/integration/minitest.rb +8 -13
  55. data/lib/henitai/integration/minitest_autorun.rb +20 -0
  56. data/lib/henitai/integration/minitest_fail_fast.rb +43 -0
  57. data/lib/henitai/integration/minitest_outcome_reporter.rb +44 -0
  58. data/lib/henitai/integration/minitest_preload.rb +30 -0
  59. data/lib/henitai/integration/minitest_runnable_filter.rb +61 -0
  60. data/lib/henitai/integration/minitest_test_runner.rb +47 -18
  61. data/lib/henitai/integration/mutant_run_support.rb +80 -17
  62. data/lib/henitai/integration/rspec_child_runner.rb +49 -9
  63. data/lib/henitai/integration/rspec_file_order.rb +37 -0
  64. data/lib/henitai/integration/rspec_preload.rb +35 -0
  65. data/lib/henitai/integration/rspec_process_runner.rb +4 -20
  66. data/lib/henitai/integration/scenario_log_support.rb +31 -18
  67. data/lib/henitai/integration.rb +18 -6
  68. data/lib/henitai/kill_history/store.rb +67 -0
  69. data/lib/henitai/kill_history.rb +85 -0
  70. data/lib/henitai/minitest_coverage_reporter.rb +22 -6
  71. data/lib/henitai/minitest_result_file.rb +62 -0
  72. data/lib/henitai/mutant/activation.rb +32 -0
  73. data/lib/henitai/mutant/activator.rb +75 -55
  74. data/lib/henitai/mutant/def_injection.rb +136 -0
  75. data/lib/henitai/mutant/lexical_scope_check.rb +120 -0
  76. data/lib/henitai/mutant/lexical_target.rb +105 -0
  77. data/lib/henitai/mutant/replacement.rb +52 -0
  78. data/lib/henitai/mutant.rb +32 -8
  79. data/lib/henitai/mutant_generator.rb +62 -18
  80. data/lib/henitai/mutant_history_store/database.rb +61 -0
  81. data/lib/henitai/mutant_history_store/sql.rb +9 -3
  82. data/lib/henitai/mutant_history_store/subject_scans.rb +101 -0
  83. data/lib/henitai/mutant_history_store/verdict_cache.rb +48 -18
  84. data/lib/henitai/mutant_history_store.rb +77 -51
  85. data/lib/henitai/mutant_identity.rb +5 -1
  86. data/lib/henitai/mutant_verdict.rb +71 -0
  87. data/lib/henitai/operator.rb +6 -2
  88. data/lib/henitai/operators/array_declaration.rb +4 -0
  89. data/lib/henitai/operators/assignment_expression.rb +2 -0
  90. data/lib/henitai/operators/conditional_expression.rb +21 -1
  91. data/lib/henitai/operators/equality_identity_operator.rb +2 -2
  92. data/lib/henitai/operators/hash_key_type.rb +11 -1
  93. data/lib/henitai/operators/hash_literal.rb +37 -9
  94. data/lib/henitai/operators/method_expression.rb +14 -3
  95. data/lib/henitai/operators/regex_metacharacters.rb +89 -0
  96. data/lib/henitai/operators/regex_mutator.rb +23 -28
  97. data/lib/henitai/operators/return_value.rb +1 -1
  98. data/lib/henitai/operators/string_literal.rb +17 -4
  99. data/lib/henitai/operators.rb +1 -0
  100. data/lib/henitai/per_test_coverage.rb +11 -7
  101. data/lib/henitai/per_test_coverage_collector.rb +30 -31
  102. data/lib/henitai/per_test_coverage_delta.rb +60 -0
  103. data/lib/henitai/per_test_coverage_selector.rb +26 -13
  104. data/lib/henitai/process_wakeup.rb +8 -3
  105. data/lib/henitai/process_worker_runner.rb +41 -7
  106. data/lib/henitai/reporter.rb +15 -8
  107. data/lib/henitai/result.rb +32 -16
  108. data/lib/henitai/reuse_context.rb +52 -0
  109. data/lib/henitai/rspec_coverage_formatter.rb +4 -0
  110. data/lib/henitai/rspec_outcomes.rb +74 -0
  111. data/lib/henitai/runner.rb +112 -21
  112. data/lib/henitai/runner_dependencies.rb +44 -3
  113. data/lib/henitai/sampling_strategy.rb +36 -6
  114. data/lib/henitai/scenario_execution_result.rb +51 -5
  115. data/lib/henitai/signal_deferral.rb +25 -0
  116. data/lib/henitai/signature_line.rb +21 -0
  117. data/lib/henitai/slot_scheduler/control_slots.rb +58 -0
  118. data/lib/henitai/slot_scheduler/draining.rb +35 -6
  119. data/lib/henitai/slot_scheduler/process_control.rb +33 -0
  120. data/lib/henitai/slot_scheduler/retry_policy.rb +20 -6
  121. data/lib/henitai/slot_scheduler.rb +64 -21
  122. data/lib/henitai/source_file_selection.rb +1 -1
  123. data/lib/henitai/source_parser.rb +29 -9
  124. data/lib/henitai/static_filter.rb +45 -14
  125. data/lib/henitai/stillborn_filter.rb +70 -2
  126. data/lib/henitai/subject.rb +8 -3
  127. data/lib/henitai/subject_resolver.rb +65 -31
  128. data/lib/henitai/suite_boot_clock.rb +48 -0
  129. data/lib/henitai/survivor_activation_cache.rb +18 -11
  130. data/lib/henitai/survivor_rerun_strategy.rb +47 -43
  131. data/lib/henitai/test_file_outcomes.rb +93 -0
  132. data/lib/henitai/test_prioritizer.rb +10 -2
  133. data/lib/henitai/timeout_calibrator.rb +31 -9
  134. data/lib/henitai/timeout_ceiling.rb +37 -0
  135. data/lib/henitai/verdict_fingerprint.rb +29 -30
  136. data/lib/henitai/version.rb +1 -1
  137. data/lib/henitai.rb +21 -1
  138. data/sig/configuration_validator.rbs +1 -0
  139. data/sig/henitai.rbs +254 -56
  140. metadata +63 -5
  141. data/lib/henitai/survivor_test_filter.rb +0 -72
@@ -50,7 +50,7 @@ module Henitai
50
50
  return if draining.empty?
51
51
 
52
52
  broadcast_term(draining)
53
- wait_for_drain_window
53
+ wait_for_drain_window(draining)
54
54
  signal_draining_slots(draining)
55
55
  reap_and_remove_draining(draining)
56
56
  end
@@ -80,9 +80,27 @@ module Henitai
80
80
  end
81
81
  end
82
82
 
83
- def wait_for_drain_window
84
- wakeup&.wait(PROCESS_DRAIN_WINDOW)
85
- wakeup&.drain
83
+ # The SIGTERM grace window ends at its deadline or as soon as every
84
+ # draining child has exited, whichever comes first. Unrelated events
85
+ # (CHLD of another slot, server traffic) wake the select early without
86
+ # ending it. Waiting out the full window for a child that already exited
87
+ # cost every timed-out mutant PROCESS_DRAIN_WINDOW of wall time.
88
+ def wait_for_drain_window(draining)
89
+ return unless wakeup
90
+
91
+ deadline = monotonic_time + PROCESS_DRAIN_WINDOW
92
+ while (remaining = deadline - monotonic_time).positive?
93
+ woken = wakeup.wait(remaining)
94
+ wakeup.drain
95
+ break unless woken
96
+ break if all_exited?(draining)
97
+ end
98
+ end
99
+
100
+ # Reaps what has exited; the status waits on the slot for the final reap.
101
+ def all_exited?(draining)
102
+ draining.each_value { |slot| slot.reaped_status ||= wnohang_reap(slot.pid)&.last }
103
+ draining.each_value.all?(&:reaped_status)
86
104
  end
87
105
 
88
106
  def signal_draining_slots(draining)
@@ -113,8 +131,8 @@ module Henitai
113
131
  # One last WNOHANG before blocking: catches processes that exited
114
132
  # between SIGKILL and here.
115
133
  def reap_and_finalize_slot(slot)
116
- _, final_status = wnohang_reap(slot.pid)
117
- reap_pid(slot.pid) unless final_status
134
+ final_status = final_reap(slot)
135
+ tear_down_process_group(slot.pid)
118
136
 
119
137
  slot_table.release_pid(slot.pid)
120
138
  slot_table.delete(slot.slot_id)
@@ -125,9 +143,20 @@ module Henitai
125
143
  record_drain_result(slot, final_status)
126
144
  end
127
145
 
146
+ # The status the drain window already reaped, else a last WNOHANG, else
147
+ # a blocking reap (which yields no status worth classifying).
148
+ def final_reap(slot)
149
+ status = slot.reaped_status || wnohang_reap(slot.pid)&.last
150
+ reap_pid(slot.pid) unless status
151
+ status
152
+ end
153
+
128
154
  def record_drain_result(slot, final_status)
129
155
  result = drain_verdict.build(slot, final_status)
156
+ return report_control(slot.control_key, result) if slot.control_key
157
+
130
158
  slot.mutant.status = result.status
159
+ ScenarioExecutionResult.copy_reason(result, slot.mutant)
131
160
  results << result
132
161
  progress_reporter&.progress(slot.mutant, scenario_result: result)
133
162
  end
@@ -8,6 +8,9 @@ module Henitai
8
8
  # scheduler remains the single caller of the process table. Mixed into
9
9
  # {SlotScheduler}; relies on its +runtime+ reader.
10
10
  module ProcessControl
11
+ GROUP_POLL_INTERVAL = 0.02
12
+ GROUP_POLL_COUNT = 10
13
+
11
14
  private
12
15
 
13
16
  def monotonic_time
@@ -33,6 +36,36 @@ module Henitai
33
36
  end
34
37
  end
35
38
 
39
+ # Terminates whatever a finished child left behind in its process group
40
+ # (daemons, grandchildren). Waits at most PROCESS_DRAIN_WINDOW for them to
41
+ # exit on SIGTERM before escalating to SIGKILL.
42
+ def tear_down_process_group(pid)
43
+ signal_process_group(pid, :SIGTERM)
44
+ return unless group_members_remain?(pid)
45
+
46
+ signal_process_group(pid, :SIGKILL)
47
+ end
48
+
49
+ def group_members_remain?(pid)
50
+ GROUP_POLL_COUNT.times do
51
+ return false unless process_group_alive?(pid)
52
+
53
+ runtime.pause(GROUP_POLL_INTERVAL)
54
+ end
55
+ process_group_alive?(pid)
56
+ end
57
+
58
+ # Signal 0 probes existence only. ESRCH means the group is empty; EPERM
59
+ # means members exist but belong to someone else.
60
+ def process_group_alive?(pid)
61
+ runtime.kill(0, -pid)
62
+ true
63
+ rescue Errno::ESRCH
64
+ false
65
+ rescue Errno::EPERM
66
+ true
67
+ end
68
+
36
69
  def reap_pid(pid)
37
70
  runtime.wait(pid)
38
71
  rescue Errno::ECHILD, Errno::ESRCH
@@ -4,17 +4,31 @@ module Henitai
4
4
  class SlotScheduler
5
5
  # Decides whether a finished slot earns another attempt.
6
6
  #
7
- # A survived verdict is the only retryable one: it is the verdict a flaky
8
- # test can fake, whereas a kill cannot be faked by flakiness. A requested
9
- # shutdown vetoes retries outright — respawning children while tearing the
10
- # run down would leak processes past the drain window.
7
+ # The rule itself lives in FlakyRetryDecision; this class adds the shutdown
8
+ # veto. A requested shutdown vetoes retries outright — respawning children
9
+ # while tearing the run down would leak processes past the drain window.
11
10
  class RetryPolicy
12
- def initialize(max_retries:)
11
+ def initialize(max_retries:, flaky_tests: FlakyTestRegistry.new([]))
13
12
  @max_retries = max_retries.to_i
13
+ @decision = FlakyRetryDecision.new(flaky_tests: flaky_tests, max_retries: @max_retries)
14
14
  end
15
15
 
16
16
  def retry?(slot:, result:, shutdown:)
17
- !shutdown && result.survived? && slot.retry_count < @max_retries
17
+ !shutdown && retry_wanted?(slot, result)
18
+ end
19
+
20
+ # True when only the shutdown stopped a retry: the kill may be faked by
21
+ # a flaky test, so its verdict must not be cached (ADR-19).
22
+ def vetoed?(slot:, result:, shutdown:)
23
+ shutdown && retry_wanted?(slot, result)
24
+ end
25
+
26
+ private
27
+
28
+ def retry_wanted?(slot, result)
29
+ return false unless result.status == :killed
30
+
31
+ @decision.retry?(status: :killed, killing_tests: result.killing_tests, attempts: slot.retry_count)
18
32
  end
19
33
  end
20
34
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "slot_scheduler/process_control"
4
+ require_relative "slot_scheduler/control_slots"
4
5
  require_relative "slot_scheduler/draining"
5
6
  require_relative "slot_scheduler/drain_verdict"
6
7
  require_relative "slot_scheduler/retry_policy"
@@ -25,6 +26,7 @@ module Henitai
25
26
  class SlotScheduler
26
27
  include ProcessControl
27
28
  include Draining
29
+ include ControlSlots
28
30
 
29
31
  PROCESS_DRAIN_WINDOW = 0.2
30
32
 
@@ -38,7 +40,7 @@ module Henitai
38
40
  Slot = Struct.new(
39
41
  :slot_id, :mutant, :pid, :started_at_monotonic, :timeout,
40
42
  :log_paths, :retry_count, :draining, :term_sent_at_monotonic,
41
- :forced_outcome, :worker_index
43
+ :forced_outcome, :worker_index, :control_key, :reaped_status
42
44
  )
43
45
 
44
46
  # @return [Integer] mutants that required at least one retry during the run.
@@ -51,7 +53,8 @@ module Henitai
51
53
  # rubocop:disable Metrics/ParameterLists -- every one of these is a distinct
52
54
  # collaborator supplied by ProcessWorkerRunner; bundling them into a context
53
55
  # object would only move the list.
54
- def initialize(integration:, config:, progress_reporter:, options:, host:, slot_table: SlotTable.new)
56
+ def initialize(integration:, config:, progress_reporter:, options:, host:, slot_table: SlotTable.new,
57
+ flaky_tests: nil, control_listener: nil)
55
58
  # rubocop:enable Metrics/ParameterLists
56
59
  @integration = integration
57
60
  @config = config
@@ -59,6 +62,8 @@ module Henitai
59
62
  @options = options
60
63
  @host = host
61
64
  @slot_table = slot_table
65
+ @injected_flaky_tests = flaky_tests
66
+ @control_listener = control_listener
62
67
  @pending = []
63
68
  @results = []
64
69
  @flaky_retry_count = 0
@@ -70,13 +75,13 @@ module Henitai
70
75
  end
71
76
 
72
77
  def done?
73
- pending.empty? && slot_table.empty?
78
+ pending.empty? && control_jobs.empty? && slot_table.empty?
74
79
  end
75
80
 
81
+ # Queued control jobs go first: detections wait on them (ADR-19).
76
82
  def fill_idle_slots
77
- while slot_table.size < worker_count && !pending.empty?
78
- mutant = pending.shift
79
- spawn_into_slot(mutant)
83
+ while slot_table.size < worker_count && (control_jobs.any? || pending.any?)
84
+ control_jobs.any? ? spawn_control_into_slot(control_jobs.shift) : spawn_into_slot(pending.shift)
80
85
  end
81
86
  end
82
87
 
@@ -119,7 +124,7 @@ module Henitai
119
124
  worker_index = slot_table.next_free_worker_index(worker_count)
120
125
  with_worker_slot(worker_index) do
121
126
  handle = integration.spawn_mutant(mutant: mutant, test_files: test_files)
122
- register_slot(handle, mutant, worker_index, slot_timeout(mutant, test_files))
127
+ register_slot(handle, mutant, worker_index, handle.timeout || slot_timeout(mutant, test_files))
123
128
  end
124
129
  rescue StandardError => e
125
130
  record_spawn_failure(mutant, e)
@@ -133,8 +138,9 @@ module Henitai
133
138
  mutant.tests_completed = test_files.size if mutant.respond_to?(:tests_completed=)
134
139
  end
135
140
 
136
- def register_slot(handle, mutant, worker_index, timeout)
141
+ def register_slot(handle, mutant, worker_index, timeout, control_key: nil)
137
142
  slot = build_slot(slot_table.next_slot_id!, mutant, handle, worker_index, timeout)
143
+ slot.control_key = control_key
138
144
  slot_table.add(slot)
139
145
  slot_table.register_pid(handle.pid, slot.slot_id)
140
146
  Integration::SchedulerDiagnostics.child_started(handle.pid)
@@ -154,14 +160,22 @@ module Henitai
154
160
  resolver ? resolver.call(mutant, test_files) : config.timeout
155
161
  end
156
162
 
163
+ # Every path that reaps a mutant child ends here, so this is the single
164
+ # place that tears down the child's process group. A reaped pid that owns
165
+ # no slot (wait2(-1) can reap any helper child) is never signalled.
157
166
  def complete_slot(pid, wait_result)
158
167
  slot_id = slot_table.release_pid(pid)
159
168
  return unless slot_id
160
169
 
170
+ tear_down_process_group(pid)
171
+
161
172
  slot = slot_table.fetch(slot_id)
162
173
  return unless slot
163
174
 
164
175
  Integration::SchedulerDiagnostics.child_ended(pid)
176
+ return complete_control(slot, wait_result) if slot.control_key
177
+ return rerun_lost_slot(slot) if lost?(wait_result)
178
+
165
179
  result = integration.build_result(wait_result, slot.log_paths)
166
180
  dispatch_slot_result(slot, result)
167
181
  end
@@ -169,12 +183,18 @@ module Henitai
169
183
  def dispatch_slot_result(slot, result)
170
184
  return retry_slot(slot) if retry_policy.retry?(slot: slot, result: result, shutdown: shutdown?)
171
185
 
186
+ mark_retry_cut_short(slot) if retry_policy.vetoed?(slot: slot, result: result, shutdown: shutdown?)
172
187
  finalize_slot(slot, result)
173
188
  end
174
189
 
190
+ def mark_retry_cut_short(slot)
191
+ slot.mutant.retry_cut_short = true if slot.mutant.respond_to?(:retry_cut_short=)
192
+ end
193
+
175
194
  def finalize_slot(slot, result)
176
195
  slot_table.delete(slot.slot_id)
177
196
  slot.mutant.status = result.status
197
+ ScenarioExecutionResult.copy_reason(result, slot.mutant)
178
198
  results << result
179
199
  progress_reporter&.progress(slot.mutant, scenario_result: result)
180
200
  result.release_output! if result.respond_to?(:release_output!)
@@ -184,7 +204,13 @@ module Henitai
184
204
  # the runner builds when nothing is pending) pass `config: nil`, and this
185
205
  # is only reached once a slot has actually finished.
186
206
  def retry_policy
187
- @retry_policy ||= RetryPolicy.new(max_retries: config.max_flaky_retries)
207
+ @retry_policy ||= RetryPolicy.new(max_retries: config.max_flaky_retries, flaky_tests: flaky_tests)
208
+ end
209
+
210
+ def flaky_tests
211
+ return @injected_flaky_tests if @injected_flaky_tests
212
+
213
+ FlakyTestRegistry.for_config(config)
188
214
  end
189
215
 
190
216
  def drain_verdict
@@ -199,20 +225,43 @@ module Henitai
199
225
  @test_file_selection ||= TestFileSelection.new(options: options, integration: integration)
200
226
  end
201
227
 
228
+ # The fork server died while the mutant ran, so nothing is known about
229
+ # it. It is run again on the local path and never becomes a verdict.
230
+ def lost?(wait_result)
231
+ wait_result.respond_to?(:lost?) && wait_result.lost?
232
+ end
233
+
234
+ def rerun_lost_slot(slot)
235
+ return slot_table.delete(slot.slot_id) if shutdown?
236
+
237
+ respawn_slot(slot) { |handle| restart_slot(slot, handle) }
238
+ end
239
+
202
240
  def retry_slot(slot)
203
- test_files = test_file_selection.for(slot.mutant)
204
- with_worker_slot(slot.worker_index) do
205
- handle = integration.spawn_mutant(mutant: slot.mutant, test_files: test_files)
206
- finish_retry(slot, handle)
207
- end
241
+ respawn_slot(slot) { |handle| finish_retry(slot, handle) }
242
+ end
243
+
244
+ def respawn_slot(slot)
245
+ with_worker_slot(slot.worker_index) { yield respawn_handle(slot) }
208
246
  rescue StandardError => e
209
247
  slot_table.delete(slot.slot_id)
210
248
  record_spawn_failure(slot.mutant, e)
211
249
  end
212
250
 
251
+ def respawn_handle(slot)
252
+ test_files = test_file_selection.for(slot.mutant)
253
+ handle = integration.spawn_mutant(mutant: slot.mutant, test_files: test_files)
254
+ slot.timeout = handle.timeout || slot_timeout(slot.mutant, test_files)
255
+ handle
256
+ end
257
+
213
258
  def finish_retry(slot, handle)
214
259
  @flaky_retry_count += 1 if slot.retry_count.zero?
215
260
  slot.retry_count += 1
261
+ restart_slot(slot, handle)
262
+ end
263
+
264
+ def restart_slot(slot, handle)
216
265
  reset_slot_for_retry(slot, handle)
217
266
  slot_table.register_pid(handle.pid, slot.slot_id)
218
267
  Integration::SchedulerDiagnostics.child_started(handle.pid)
@@ -228,13 +277,7 @@ module Henitai
228
277
  end
229
278
 
230
279
  def record_spawn_failure(mutant, error)
231
- result = ScenarioExecutionResult.new(
232
- status: :compile_error,
233
- stdout: "",
234
- stderr: "spawn failed: #{error.message}",
235
- log_path: "/dev/null",
236
- exit_status: nil
237
- )
280
+ result = ScenarioExecutionResult.spawn_failure(error)
238
281
  mutant.status = result.status
239
282
  results << result
240
283
  progress_reporter&.progress(mutant, scenario_result: result)
@@ -58,7 +58,7 @@ module Henitai
58
58
  # dirty and untracked files): the working tree is what gets tested, so it is
59
59
  # always part of "changed since REF".
60
60
  def changed_paths_since
61
- @git_diff_analyzer.changed_files(from: @since, to: "HEAD") +
61
+ @git_diff_analyzer.changed_files_since(@since) +
62
62
  @git_diff_analyzer.working_tree_changed_files
63
63
  end
64
64
 
@@ -12,10 +12,18 @@ module Henitai
12
12
  class SourceParser
13
13
  DEFAULT_PATH = "(string)"
14
14
 
15
+ # Emits the modern parser-gem AST format (procarg0, kwargs, index, lambda,
16
+ # forward_arg, ...). The legacy format cannot tell `|x|` from `|x,|` or
17
+ # keyword arguments from a braced hash, so re-rendering a mutated subtree
18
+ # through the unparser would silently change its meaning.
19
+ class Builder < Prism::Translation::Parser::Builder
20
+ modernize
21
+ end
22
+
15
23
  @cache = {}
16
24
 
17
- def self.parse(source, path: DEFAULT_PATH)
18
- new.parse(source, path:)
25
+ def self.parse(source, path: DEFAULT_PATH, line: 1)
26
+ new.parse(source, path:, line:)
19
27
  end
20
28
 
21
29
  # Returns the parsed AST for +path+, re-using a cached result when the
@@ -31,20 +39,32 @@ module Henitai
31
39
  @cache.clear
32
40
  end
33
41
 
34
- def parse(source, path: DEFAULT_PATH)
35
- Prism::Translation::ParserCurrent.new.parse(source_buffer(source, path))
42
+ # Reads +path+ as UTF-8 source, honouring a magic encoding comment the
43
+ # same way parsing does. Raises EncodingError when the bytes are invalid
44
+ # in the declared (or default UTF-8) encoding.
45
+ def self.read_file(path)
46
+ buffer = Parser::Source::Buffer.new(path)
47
+ buffer.source = File.read(path, encoding: Encoding::UTF_8)
48
+ buffer.source
49
+ end
50
+
51
+ # +line+ is the number of the source's first line, as for Kernel#eval.
52
+ def parse(source, path: DEFAULT_PATH, line: 1)
53
+ Prism::Translation::ParserCurrent.new(Builder.new).parse(source_buffer(source, path, line))
36
54
  end
37
55
 
56
+ # Read as UTF-8, Ruby's own default source encoding, not the process
57
+ # locale: with LANG=C the default external encoding is US-ASCII and every
58
+ # non-ASCII source file would fail to parse. A magic encoding comment is
59
+ # still honoured by the parser buffer, which re-encodes to UTF-8.
38
60
  def parse_file(path)
39
- # Ruby's file encoding rules apply here. Projects that use explicit source
40
- # encoding comments can be handled by a future encoding-aware option.
41
- parse(File.read(path), path:)
61
+ parse(File.read(path, encoding: Encoding::UTF_8), path:)
42
62
  end
43
63
 
44
64
  private
45
65
 
46
- def source_buffer(source, path)
47
- Parser::Source::Buffer.new(path).tap do |buffer|
66
+ def source_buffer(source, path, line = 1)
67
+ Parser::Source::Buffer.new(path, line).tap do |buffer|
48
68
  buffer.source = source
49
69
  end
50
70
  end
@@ -10,9 +10,11 @@ module Henitai
10
10
  DEFAULT_PER_TEST_COVERAGE_REPORT_PATH = CoverageReportReader::DEFAULT_PER_TEST_COVERAGE_REPORT_PATH
11
11
 
12
12
  def initialize(coverage_report_reader: CoverageReportReader.new,
13
- skip_directives: MutationSkipDirectives.new)
13
+ skip_directives: MutationSkipDirectives.new,
14
+ fidelity_check: FidelityCheck.new)
14
15
  @coverage_report_reader = coverage_report_reader
15
16
  @skip_directives = skip_directives
17
+ @fidelity_check = fidelity_check
16
18
  end
17
19
 
18
20
  # This method is the gate-level filter orchestrator.
@@ -22,6 +24,7 @@ module Henitai
22
24
 
23
25
  Array(mutants).each do |mutant|
24
26
  next if ignored_mutant?(mutant, config) || skip_directive_mutant?(mutant)
27
+ next if unfaithful_mutant?(mutant)
25
28
 
26
29
  mark_equivalent_mutant(mutant)
27
30
  mark_no_coverage_mutant(
@@ -64,21 +67,22 @@ module Henitai
64
67
 
65
68
  private
66
69
 
67
- attr_reader :coverage_report_reader, :skip_directives
70
+ attr_reader :coverage_report_reader, :skip_directives, :fidelity_check
68
71
 
69
- def ignored?(mutant, config)
70
- source = source_for(mutant)
71
- return false unless source
72
+ def ignore_pattern_for(mutant, config)
73
+ patterns = compiled_ignore_patterns(config)
74
+ return if patterns.empty?
72
75
 
73
- compiled_ignore_patterns(config).any? do |pattern|
74
- pattern.match?(source)
75
- end
76
+ line = source_line_for(mutant)
77
+ patterns.find { |pattern| pattern.match?(line) }
76
78
  end
77
79
 
78
80
  def ignored_mutant?(mutant, config)
79
- return false unless ignored?(mutant, config)
81
+ pattern = ignore_pattern_for(mutant, config)
82
+ return false unless pattern
80
83
 
81
84
  mutant.status = :ignored
85
+ mutant.ignore_reason = "ignore_patterns: #{pattern.source}" if mutant.respond_to?(:ignore_reason=)
82
86
  true
83
87
  end
84
88
 
@@ -91,6 +95,17 @@ module Henitai
91
95
  true
92
96
  end
93
97
 
98
+ # A mutant whose activated code is not the reported mutation must not
99
+ # run: any test failure would be credited to a change it never made.
100
+ def unfaithful_mutant?(mutant)
101
+ reason = fidelity_check.violation(mutant)
102
+ return false unless reason
103
+
104
+ mutant.status = :compile_error
105
+ mutant.status_reason = reason
106
+ true
107
+ end
108
+
94
109
  def mark_equivalent_mutant(mutant)
95
110
  return unless mutant.pending?
96
111
 
@@ -111,11 +126,27 @@ module Henitai
111
126
  covered.any? { |line| (mutant.location[:start_line]..mutant.location[:end_line]).cover?(line) }
112
127
  end
113
128
 
114
- def source_for(mutant)
115
- original_node = mutant.original_node
116
- location = original_node&.location
117
- expression = location&.expression
118
- expression&.source
129
+ # Ignore patterns are line-scoped: they match the source line on which the
130
+ # mutant's original node starts, like a trailing `# henitai:disable`.
131
+ def source_line_for(mutant)
132
+ line_number = mutant.location[:start_line]
133
+ return unless line_number.is_a?(Integer) && line_number.positive?
134
+
135
+ lines = source_lines_for(mutant.location[:file])
136
+ lines&.fetch(line_number - 1, nil)
137
+ end
138
+
139
+ def source_lines_for(path)
140
+ @source_lines ||= {}
141
+ return @source_lines[path] if @source_lines.key?(path)
142
+
143
+ @source_lines[path] = read_source_lines(path)
144
+ end
145
+
146
+ def read_source_lines(path)
147
+ SourceParser.read_file(path).lines
148
+ rescue SystemCallError, IOError, EncodingError
149
+ nil
119
150
  end
120
151
 
121
152
  def compiled_ignore_patterns(config)
@@ -1,11 +1,81 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "prism"
3
4
  require "unparser"
4
5
 
5
6
  module Henitai
6
7
  # Suppresses mutants that do not produce syntactically valid Ruby.
8
+ #
9
+ # A mutant of a resolved method is checked in context: its activation
10
+ # source -- the whole method with the mutation spliced in -- must parse. A
11
+ # fragment such as `size: 2` (one keyword argument left) or a `next` guard
12
+ # is only valid inside its call or block, so checking it on its own would
13
+ # drop valid mutants. Without a method to splice into, the rendered
14
+ # fragment is compiled inside a bare method instead.
15
+ #
16
+ # A mutant that removes the only assignment of a local which is read later
17
+ # is suppressed as well (ACT-10): Ruby then parses the later read as a
18
+ # method call, so the mutant would fail with a NameError wherever it runs
19
+ # -- a trivial kill of a program other than the reported one.
7
20
  class StillbornFilter
21
+ # Collects the names of identifiers Ruby parses as method calls.
22
+ class VariableCalls < Prism::Visitor
23
+ def self.tally(node)
24
+ new.tap { |visitor| node.accept(visitor) }.names.tally
25
+ end
26
+
27
+ attr_reader :names
28
+
29
+ def initialize
30
+ super
31
+ @names = []
32
+ end
33
+
34
+ def visit_call_node(node)
35
+ @names << node.name if node.variable_call?
36
+ super
37
+ end
38
+ end
39
+
40
+ def initialize
41
+ @original_calls = {}.compare_by_identity
42
+ end
43
+
8
44
  def suppressed?(mutant)
45
+ activation = in_context_activation(mutant)
46
+ return fragment_invalid?(mutant) unless activation
47
+
48
+ result = Prism.parse(activation.source)
49
+ result.errors.any? || drops_local?(mutant.subject.ast_node, result.value)
50
+ end
51
+
52
+ private
53
+
54
+ def drops_local?(method_node, activated)
55
+ original = @original_calls[method_node] ||= original_calls(method_node)
56
+ VariableCalls.tally(activated).any? do |name, count|
57
+ count > original.fetch(name, 0) && local_read?(method_node, name)
58
+ end
59
+ end
60
+
61
+ def original_calls(method_node)
62
+ VariableCalls.tally(Prism.parse(method_node.location.expression.source).value)
63
+ end
64
+
65
+ def local_read?(node, name)
66
+ return false unless node.is_a?(Parser::AST::Node)
67
+ return true if node.type == :lvar && node.children.first == name
68
+
69
+ node.children.any? { |child| local_read?(child, name) }
70
+ end
71
+
72
+ def in_context_activation(mutant)
73
+ return unless mutant.subject.respond_to?(:ast_node) && mutant.subject.ast_node
74
+
75
+ Mutant::Activator.activation_for(mutant)
76
+ end
77
+
78
+ def fragment_invalid?(mutant)
9
79
  source = render(mutant)
10
80
  return true unless source
11
81
 
@@ -15,8 +85,6 @@ module Henitai
15
85
  true
16
86
  end
17
87
 
18
- private
19
-
20
88
  def render(mutant)
21
89
  Unparser.unparse(mutant.mutated_node)
22
90
  rescue StandardError
@@ -14,7 +14,7 @@ module Henitai
14
14
  # descriptions in the test suite.
15
15
  class Subject
16
16
  attr_reader :namespace, :method_name, :method_type, :source_file,
17
- :source_range, :ast_node
17
+ :source_range, :ast_node, :lexical_scope
18
18
 
19
19
  # @param expression [String] subject expression, e.g. "Foo::Bar#method"
20
20
  def self.parse(expression)
@@ -25,11 +25,15 @@ module Henitai
25
25
  # @param method_name [String] method name (nil for wildcard subjects)
26
26
  # @param method_type [Symbol] :instance or :class
27
27
  # @param source_location [Hash] file/range metadata for the subject source
28
+ # @param lexical_scope [Array<String>, nil] the openers of the modules and
29
+ # classes around the method as written ("module Outer", "class Inner",
30
+ # "class << self", "Point.class_eval do"); nil when a scope cannot be
31
+ # reopened by name
28
32
  # The explicit keyword API matches the public call sites and keeps the
29
33
  # metadata fields discoverable without a single options hash.
30
34
  # rubocop:disable Metrics/ParameterLists
31
35
  def initialize(expression: nil, namespace: nil, method_name: nil,
32
- method_type: :instance, source_location: nil, ast_node: nil)
36
+ method_type: :instance, source_location: nil, ast_node: nil, lexical_scope: nil)
33
37
  if expression
34
38
  parse_expression(expression)
35
39
  else
@@ -40,6 +44,7 @@ module Henitai
40
44
  @source_file = source_location&.fetch(:file, nil)
41
45
  @source_range = source_location&.fetch(:range, nil)
42
46
  @ast_node = ast_node
47
+ @lexical_scope = lexical_scope
43
48
  end
44
49
  # rubocop:enable Metrics/ParameterLists
45
50
 
@@ -56,7 +61,7 @@ module Henitai
56
61
  private
57
62
 
58
63
  def parse_expression(expr)
59
- if (m = expr.match(/\A(.+?)([#.])(\w+)\z/))
64
+ if (m = expr.match(/\A([\w:]+)([#.])([^#.\s]+)\z/))
60
65
  @namespace = m[1]
61
66
  @method_type = m[2] == "." ? :class : :instance
62
67
  @method_name = m[3]