henitai 0.6.0 → 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 (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +111 -1
  3. data/README.md +23 -6
  4. data/assets/schema/henitai.schema.json +6 -0
  5. data/lib/henitai/bootstrap_error_message.rb +20 -2
  6. data/lib/henitai/checkpoint_reporter.rb +5 -0
  7. data/lib/henitai/cli/operator_command.rb +10 -3
  8. data/lib/henitai/cli.rb +2 -1
  9. data/lib/henitai/composite_progress_reporter.rb +15 -2
  10. data/lib/henitai/configuration.rb +6 -1
  11. data/lib/henitai/configuration_validator/rules.rb +1 -0
  12. data/lib/henitai/configuration_validator/scalars.rb +13 -0
  13. data/lib/henitai/configuration_validator.rb +2 -1
  14. data/lib/henitai/control_gate.rb +78 -0
  15. data/lib/henitai/control_run.rb +37 -43
  16. data/lib/henitai/coverage_bootstrapper.rb +12 -3
  17. data/lib/henitai/execution_engine/env_scope.rb +3 -3
  18. data/lib/henitai/execution_engine/fork_server_scope.rb +6 -6
  19. data/lib/henitai/execution_engine.rb +74 -99
  20. data/lib/henitai/execution_provenance.rb +67 -0
  21. data/lib/henitai/flaky_detection_baseline.rb +5 -3
  22. data/lib/henitai/fork_server/client.rb +0 -9
  23. data/lib/henitai/fork_server/mutant_runner.rb +14 -45
  24. data/lib/henitai/fork_server.rb +0 -1
  25. data/lib/henitai/history_checkpointer.rb +72 -0
  26. data/lib/henitai/integration/base.rb +6 -14
  27. data/lib/henitai/integration/child_report_store.rb +34 -5
  28. data/lib/henitai/integration/minitest.rb +1 -8
  29. data/lib/henitai/integration/minitest_fail_fast.rb +43 -0
  30. data/lib/henitai/integration/minitest_outcome_reporter.rb +17 -1
  31. data/lib/henitai/integration/minitest_test_runner.rb +16 -4
  32. data/lib/henitai/integration/mutant_run_support.rb +11 -8
  33. data/lib/henitai/integration/rspec_child_runner.rb +12 -7
  34. data/lib/henitai/integration/rspec_file_order.rb +37 -0
  35. data/lib/henitai/integration/rspec_process_runner.rb +0 -19
  36. data/lib/henitai/integration.rb +2 -3
  37. data/lib/henitai/kill_history/store.rb +67 -0
  38. data/lib/henitai/kill_history.rb +85 -0
  39. data/lib/henitai/mutant.rb +5 -0
  40. data/lib/henitai/mutant_history_store/database.rb +61 -0
  41. data/lib/henitai/mutant_history_store/sql.rb +9 -3
  42. data/lib/henitai/mutant_history_store/subject_scans.rb +101 -0
  43. data/lib/henitai/mutant_history_store/verdict_cache.rb +40 -11
  44. data/lib/henitai/mutant_history_store.rb +66 -50
  45. data/lib/henitai/operator.rb +6 -2
  46. data/lib/henitai/operators/equality_identity_operator.rb +2 -2
  47. data/lib/henitai/operators/hash_key_type.rb +11 -1
  48. data/lib/henitai/operators/hash_literal.rb +30 -5
  49. data/lib/henitai/operators/string_literal.rb +17 -4
  50. data/lib/henitai/process_worker_runner.rb +23 -3
  51. data/lib/henitai/reuse_context.rb +4 -2
  52. data/lib/henitai/rspec_outcomes.rb +3 -1
  53. data/lib/henitai/runner.rb +54 -2
  54. data/lib/henitai/runner_dependencies.rb +23 -3
  55. data/lib/henitai/scenario_execution_result.rb +27 -4
  56. data/lib/henitai/signal_deferral.rb +25 -0
  57. data/lib/henitai/slot_scheduler/control_slots.rb +58 -0
  58. data/lib/henitai/slot_scheduler/draining.rb +25 -6
  59. data/lib/henitai/slot_scheduler/retry_policy.rb +13 -1
  60. data/lib/henitai/slot_scheduler.rb +18 -14
  61. data/lib/henitai/test_file_outcomes.rb +15 -1
  62. data/lib/henitai/test_prioritizer.rb +10 -2
  63. data/lib/henitai/version.rb +1 -1
  64. data/lib/henitai.rb +5 -1
  65. data/sig/henitai.rbs +42 -24
  66. metadata +21 -6
  67. data/lib/henitai/flaky_mutant_retrier.rb +0 -43
  68. data/lib/henitai/fork_server/group_terminator.rb +0 -47
@@ -146,20 +146,29 @@ module Henitai
146
146
 
147
147
  def bootstrap_coverage(integration, config, test_files = nil)
148
148
  test_files ||= integration.test_files
149
+ timeout = suite_timeout(config)
149
150
 
150
151
  with_reports_dir(config) do
151
152
  with_coverage_dir(config) do
152
- result = integration.run_suite(test_files)
153
+ result = integration.run_suite(test_files, timeout:)
153
154
  if result == :survived
154
155
  record_dependency_manifest(config)
155
156
  return
156
157
  end
157
158
 
158
- raise CoverageError, BootstrapErrorMessage.build(result)
159
+ raise CoverageError, BootstrapErrorMessage.build(result, timeout:)
159
160
  end
160
161
  end
161
162
  end
162
163
 
164
+ # The limit (seconds) for one baseline run. Specs pass bare config doubles,
165
+ # so the default applies when the config does not expose one.
166
+ def suite_timeout(config)
167
+ return config.suite_timeout if config.respond_to?(:suite_timeout)
168
+
169
+ Configuration::DEFAULT_SUITE_TIMEOUT
170
+ end
171
+
163
172
  # Flaky detection needs two complete baseline runs, so the existing
164
173
  # coverage is never reused in this mode.
165
174
  def flaky_detection?(config)
@@ -169,7 +178,7 @@ module Henitai
169
178
  def run_flaky_baseline(integration, config, test_files)
170
179
  with_reports_dir(config) do
171
180
  with_coverage_dir(config) do
172
- FlakyDetectionBaseline.new(reports_dir: reports_dir(config))
181
+ FlakyDetectionBaseline.new(reports_dir: reports_dir(config), timeout: suite_timeout(config))
173
182
  .run(integration:, test_files:)
174
183
  record_dependency_manifest(config)
175
184
  end
@@ -37,9 +37,9 @@ module Henitai
37
37
  restore_env(env_key, original) if cap
38
38
  end
39
39
 
40
- # Linear-path children inherit slot 0 so suite-side isolation code works
41
- # identically in both execution modes; the parallel scheduler overwrites
42
- # the value per spawn. Restored afterwards like the other run-scoped vars.
40
+ # Slot 0 for whatever the engine starts outside a scheduler slot (the
41
+ # fork server's boot); the scheduler sets each child's slot at spawn.
42
+ # Restored afterwards like the other run-scoped vars.
43
43
  def with_worker_slot
44
44
  original = ENV.fetch(SlotScheduler::WORKER_SLOT_ENV, nil)
45
45
  ENV[SlotScheduler::WORKER_SLOT_ENV] = "0"
@@ -4,11 +4,11 @@ require_relative "../fork_server"
4
4
 
5
5
  module Henitai
6
6
  class ExecutionEngine
7
- # Opt-in fork-server execution (`mutation.fork_server`, ADR-15) for the
8
- # engine's linear path. While the block runs, @fork_server_runner (when a
9
- # server booted) runs the mutants; it is nil when the flag is off, the
10
- # integration cannot boot once, or the server failed to boot, and the
11
- # integration then boots per mutant.
7
+ # Opt-in fork-server execution (`mutation.fork_server`, ADR-15). While
8
+ # the block runs, @fork_server_runner (when a server booted) stands in for
9
+ # the integration and its runtime for the worker runner's; it is nil when
10
+ # the flag is off, the integration cannot boot once, or the server failed
11
+ # to boot, and the integration then boots per mutant.
12
12
  module ForkServerScope
13
13
  private
14
14
 
@@ -45,7 +45,7 @@ module Henitai
45
45
  end
46
46
 
47
47
  # The scheduler learns of served processes' exits through the server.
48
- def parallel_runtime_options
48
+ def fork_server_runtime_options
49
49
  @fork_server_runner&.active? ? { runtime: @fork_server_runner.runtime } : {}
50
50
  end
51
51
 
@@ -6,16 +6,32 @@ require_relative "execution_engine/fork_server_scope"
6
6
 
7
7
  module Henitai
8
8
  # Runs pending mutants through the selected integration.
9
+ #
10
+ # Every run, whatever `jobs` says, goes through one {ProcessWorkerRunner}
11
+ # (ADR-20): `jobs` is only its slot count. The engine supplies what the
12
+ # scheduler cannot know: which tests a mutant runs, its timeout, the flaky
13
+ # set, control runs and the fork server.
9
14
  class ExecutionEngine
10
15
  include EnvScope
11
16
  include ForkServerScope
12
17
 
18
+ # @param runtime [ProcessWorkerRunner::Runtime, nil] process primitives of
19
+ # the worker runner; nil uses the real ones. Replaced by the fork
20
+ # server's runtime while a server is active.
21
+ # @param wakeup [ProcessWakeup, nil] the runner's wakeup; nil installs one
22
+ def initialize(runtime: nil, wakeup: nil)
23
+ @runtime = runtime
24
+ @wakeup = wakeup
25
+ end
26
+
13
27
  def run(mutants, integration, config, progress_reporter: nil)
14
28
  with_reports_dir(config) do
15
29
  with_coverage_dir(config) do
16
30
  with_max_log_bytes(config) do
17
31
  with_worker_slot do
18
- execute(mutants, integration, config, progress_reporter)
32
+ with_test_order(config) do
33
+ execute(mutants, integration, config, progress_reporter)
34
+ end
19
35
  end
20
36
  end
21
37
  end
@@ -24,104 +40,81 @@ module Henitai
24
40
 
25
41
  private
26
42
 
43
+ # Verdicts pass a ControlGate before any observer sees them (ADR-19); the
44
+ # worker runner builds the gate and runs its controls as scheduler jobs.
27
45
  def execute(mutants, integration, config, progress_reporter)
28
46
  pending_mutants = Array(mutants).select(&:pending?)
29
- mutex = Mutex.new
30
47
  @flaky_tests = flaky_tests_for(config)
31
- @retrier = FlakyMutantRetrier.new(flaky_tests: @flaky_tests, max_retries: max_flaky_retries(config))
32
- run_pending(pending_mutants, integration, config, progress_reporter, mutex)
33
- warn_flaky_mutants
34
- verify_controls(pending_mutants, integration, config)
48
+ control_run = ControlRun.new if control_runs?(config)
49
+ runner = with_fork_server(pending_mutants, integration, config) do
50
+ run_workers(pending_mutants, integration, config, progress_reporter, control_run)
51
+ end
52
+ warn_control_failures(runner.control_stats) if control_run
53
+ warn_flaky_mutants(runner.flaky_retry_count)
35
54
  mutants
36
55
  end
37
56
 
38
- def run_pending(pending_mutants, integration, config, progress_reporter, mutex)
39
- with_fork_server(pending_mutants, integration, config) do
40
- if parallel_execution?(config, pending_mutants)
41
- run_parallel(pending_mutants, integration, config, progress_reporter)
42
- else
43
- run_linear(pending_mutants, integration, config, progress_reporter, mutex)
44
- @flaky_retry_count = @retrier.retried_count
45
- end
46
- end
57
+ # Seeds the kill history from the reports dir and saves it back in an
58
+ # ensure, so an interrupted run keeps what it learned. Without a reports
59
+ # dir there is nowhere to keep it, so nothing is read or written.
60
+ def with_test_order(config)
61
+ path = test_order_path(config)
62
+ @kill_history = path ? KillHistory.load(path) : KillHistory.new
63
+ yield
64
+ ensure
65
+ save_test_order(path) if path
47
66
  end
48
67
 
49
- def verify_controls(mutants, integration, config)
50
- return unless config.respond_to?(:control_runs) && config.control_runs
51
-
52
- timeout_for = ->(test_files) { resolved_timeout(test_files, config) }
53
- stats = ControlRun.new(integration:, timeout_for:).verify(mutants)
54
- return if stats[:failed].zero?
55
-
56
- warn format("henitai: control runs failed for %<failed>d of %<verified>d subjects; " \
57
- "%<reclassified>d detected verdicts reclassified as CompileError", **stats)
68
+ # A learned order is a convenience: a read-only or full reports dir must
69
+ # not fail the run or hide its verdicts, so a save error only warns.
70
+ def save_test_order(path)
71
+ @kill_history&.write(path)
72
+ rescue SystemCallError => e
73
+ warn format(
74
+ "henitai: could not save the learned test order (%<class>s: %<message>s)",
75
+ class: e.class, message: e.message
76
+ )
58
77
  end
59
78
 
60
- def parallel_execution?(config, mutants)
61
- worker_count(config) > 1 && mutants.size > 1
79
+ def test_order_path(config)
80
+ dir = config.reports_dir if config.respond_to?(:reports_dir)
81
+ File.join(dir, KillHistory::FILE_NAME) unless dir.nil? || dir.empty?
62
82
  end
63
83
 
64
- def worker_count(config)
65
- configured_jobs = config.respond_to?(:jobs) ? config.jobs : nil
66
- return configured_jobs if configured_jobs
67
-
68
- # The fallback stays conservative for now; the execution policy still
69
- # defaults to a single worker even though AvailableCpuCount exists as a
70
- # future policy hook.
71
- 1
72
- end
84
+ def control_runs?(config) = config.respond_to?(:control_runs) && config.control_runs
73
85
 
74
- def run_linear(mutants, integration, config, progress_reporter, mutex)
75
- mutants.each do |mutant|
76
- process_mutant(mutant, integration, config, progress_reporter, mutex)
77
- end
78
- end
86
+ def warn_control_failures(stats)
87
+ return if stats[:failed].zero?
79
88
 
80
- def run_parallel(mutants, integration, config, progress_reporter)
81
- runner = ProcessWorkerRunner.new(
82
- worker_count: worker_count(config),
83
- flaky_tests: @flaky_tests,
84
- **parallel_runtime_options
85
- )
86
- results = runner.run(
87
- mutants,
88
- @fork_server_runner || integration,
89
- config,
90
- progress_reporter,
91
- test_file_resolver: ->(mutant) { prioritized_tests_for(mutant, integration, config) },
92
- timeout_resolver: ->(_mutant, test_files) { resolved_timeout(test_files, config) }
93
- )
94
- @flaky_retry_count = runner.flaky_retry_count
95
- results
89
+ warn format("henitai: control runs failed for %<failed>d of %<verified>d subjects; " \
90
+ "%<reclassified>d detected verdicts reclassified as CompileError", **stats)
96
91
  end
97
92
 
98
- def process_mutant(mutant, integration, config, progress_reporter, mutex)
99
- test_files = prioritized_tests_for(mutant, integration, config)
100
- record_test_files(mutant, test_files)
101
- return record_no_coverage(mutant, progress_reporter, mutex) if test_files.empty?
102
-
103
- scenario_result = @retrier.run(
104
- mutant: mutant, integration: @fork_server_runner || integration, test_files: test_files,
105
- timeout: resolved_timeout(test_files, config)
106
- )
107
- mutant.status = scenario_status(scenario_result)
108
- ScenarioExecutionResult.copy_reason(scenario_result, mutant)
109
-
110
- report_progress(mutant, scenario_result, progress_reporter, mutex)
93
+ # `jobs` is a slot count; unset or zero means one slot. The default stays
94
+ # conservative: AvailableCpuCount exists as a future policy hook.
95
+ def worker_count(config)
96
+ [config.jobs.to_i, 1].max
111
97
  end
112
98
 
113
- def record_test_files(mutant, test_files)
114
- mutant.covered_by = test_files if mutant.respond_to?(:covered_by=)
115
- mutant.tests_completed = test_files.size if mutant.respond_to?(:tests_completed=)
99
+ def run_workers(mutants, integration, config, progress_reporter, control_run)
100
+ runner = ProcessWorkerRunner.new(worker_count: worker_count(config), flaky_tests: @flaky_tests,
101
+ **runner_primitives)
102
+ observers = CompositeProgressReporter.new([@kill_history, progress_reporter])
103
+ runner.run(mutants, @fork_server_runner || integration, config, observers,
104
+ **run_options(integration, config, control_run))
105
+ runner
116
106
  end
117
107
 
118
- def record_no_coverage(mutant, progress_reporter, mutex)
119
- mutant.status = :no_coverage
120
- report_progress(mutant, nil, progress_reporter, mutex)
108
+ def runner_primitives
109
+ { runtime: @runtime, wakeup: @wakeup }.compact.merge(fork_server_runtime_options)
121
110
  end
122
111
 
123
- def report_progress(mutant, scenario_result, progress_reporter, mutex)
124
- mutex.synchronize { progress_reporter&.progress(mutant, scenario_result:) }
112
+ def run_options(integration, config, control_run)
113
+ {
114
+ test_file_resolver: ->(mutant) { prioritized_tests_for(mutant, integration, config) },
115
+ timeout_resolver: ->(_mutant, test_files) { resolved_timeout(test_files, config) },
116
+ control_run:
117
+ }
125
118
  end
126
119
 
127
120
  def prioritized_tests_for(mutant, integration, config)
@@ -131,7 +124,7 @@ module Henitai
131
124
  test_files: -> { reject_excluded_tests(integration.test_files, config) },
132
125
  fallback: -> { reject_excluded_tests(integration.select_tests(mutant.subject), config) }
133
126
  )
134
- test_prioritizer(config).sort(tests, mutant, test_history(config))
127
+ test_prioritizer(config).sort(tests, mutant, @kill_history)
135
128
  end
136
129
 
137
130
  def reject_excluded_tests(tests, config)
@@ -215,37 +208,19 @@ module Henitai
215
208
 
216
209
  def per_test_coverage_selector = @per_test_coverage_selector ||= PerTestCoverageSelector.new
217
210
 
218
- def test_history(config)
219
- return {} unless config.respond_to?(:history)
220
-
221
- config.history || {}
222
- end
223
-
224
211
  def flaky_tests_for(config)
225
212
  FlakyTestRegistry.for_config(config)
226
213
  end
227
214
 
228
- def scenario_status(result)
229
- return result if result.is_a?(Symbol)
230
-
231
- result.status
232
- end
233
-
234
- def warn_flaky_mutants
235
- return if @flaky_retry_count.to_i.zero?
215
+ def warn_flaky_mutants(count)
216
+ return if count.zero?
236
217
 
237
218
  warn format(
238
219
  "Flaky-test mitigation: %<count>d killed mutant(s) were re-checked because their killing tests " \
239
220
  "are flaky (%<files>s)",
240
- count: @flaky_retry_count,
221
+ count:,
241
222
  files: @flaky_tests.all.join(", ")
242
223
  )
243
224
  end
244
-
245
- def max_flaky_retries(config)
246
- return 3 unless config.respond_to?(:max_flaky_retries)
247
-
248
- config.max_flaky_retries || 3
249
- end
250
225
  end
251
226
  end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "verdict_fingerprint"
4
+
5
+ module Henitai
6
+ # Binds cached verdicts to the inputs that were executed (ADR-19 §4).
7
+ #
8
+ # Before Gate 1 parses anything, the run records size and mtime of every
9
+ # source and test file it may parse, load or execute, and of every test
10
+ # dependency file ({VerdictFingerprint.dependency_files}). A verdict may be
11
+ # fingerprinted only while its files still match that record: hashing them
12
+ # then hashes the content that was parsed, loaded and executed.
13
+ #
14
+ # Any change counts, including an edit that was later undone: editors and
15
+ # git move the mtime forward, so a restored file still differs from the
16
+ # record. A dependency file changing, appearing or disappearing invalidates
17
+ # every verdict of the run for good, since a helper loaded once (for
18
+ # example by the fork server) affects every mutant. A tool that rewrites a
19
+ # file and also restores its mtime is not detected; ADR-19 accepts that.
20
+ class ExecutionProvenance
21
+ # Stands in until a run has captured its inputs: nothing is intact, so a
22
+ # verdict recorded without a capture is never cached.
23
+ UNCAPTURED = Class.new { def intact?(_paths) = false }.new.freeze
24
+
25
+ def self.capture(paths, root: Dir.pwd)
26
+ dependencies = VerdictFingerprint.dependency_files(root)
27
+ stats = (Array(paths) + dependencies).to_h { |path| [File.expand_path(path), stat_of(path)] }
28
+ new(stats:, dependencies:, root:)
29
+ end
30
+
31
+ def self.stat_of(path)
32
+ stat = File.stat(path)
33
+ [stat.size, stat.mtime.to_i, stat.mtime.nsec]
34
+ rescue SystemCallError
35
+ nil
36
+ end
37
+
38
+ def initialize(stats:, dependencies:, root:)
39
+ @stats = stats
40
+ @dependencies = dependencies
41
+ @root = root
42
+ @dependencies_drifted = false
43
+ end
44
+
45
+ # True when every path was captured and is unchanged, and no test
46
+ # dependency changed since the capture.
47
+ def intact?(paths)
48
+ dependencies_intact? && Array(paths).all? { |path| unchanged?(File.expand_path(path.to_s)) }
49
+ end
50
+
51
+ private
52
+
53
+ def dependencies_intact?
54
+ return false if @dependencies_drifted
55
+
56
+ intact = VerdictFingerprint.dependency_files(@root) == @dependencies &&
57
+ @dependencies.all? { |path| unchanged?(File.expand_path(path)) }
58
+ @dependencies_drifted = true unless intact
59
+ intact
60
+ end
61
+
62
+ def unchanged?(path)
63
+ recorded = @stats[path]
64
+ !recorded.nil? && self.class.stat_of(path) == recorded
65
+ end
66
+ end
67
+ end
@@ -9,8 +9,10 @@ module Henitai
9
9
  # per-file outcomes via the integration's coverage hooks; the files that
10
10
  # disagree between the two runs are written to `henitai_flaky_tests.json`.
11
11
  class FlakyDetectionBaseline
12
- def initialize(reports_dir:)
12
+ # @param timeout [Numeric] seconds each baseline run may take
13
+ def initialize(reports_dir:, timeout: Configuration::DEFAULT_SUITE_TIMEOUT)
13
14
  @reports_dir = reports_dir
15
+ @timeout = timeout
14
16
  end
15
17
 
16
18
  def run(integration:, test_files:)
@@ -29,7 +31,7 @@ module Henitai
29
31
  def run_once(integration, test_files)
30
32
  outcome_path = TestFileOutcomes.report_path(@reports_dir)
31
33
  FileUtils.rm_f(outcome_path)
32
- result = integration.run_suite(test_files)
34
+ result = integration.run_suite(test_files, timeout: @timeout)
33
35
  raise CoverageError, "could not record suite outcomes" unless File.exist?(outcome_path)
34
36
 
35
37
  [TestFileOutcomes.load(outcome_path), result]
@@ -44,7 +46,7 @@ module Henitai
44
46
  return if failing.empty?
45
47
  return if failing.size < runs.size && failing.all? { |outcomes, _result| explained?(registry, outcomes) }
46
48
 
47
- raise CoverageError, BootstrapErrorMessage.build(failing.first.last)
49
+ raise CoverageError, BootstrapErrorMessage.build(failing.first.last, timeout: @timeout)
48
50
  end
49
51
 
50
52
  def explained?(registry, outcomes)
@@ -99,15 +99,6 @@ module Henitai
99
99
  !@exits.empty? || @reader.buffered_message?
100
100
  end
101
101
 
102
- # Stops tracking a pid whose result is no longer wanted, such as a child
103
- # the caller gave up on after a timeout. Its exit, if one already
104
- # arrived or arrives later, is dropped rather than left for a recycled pid.
105
- def forget(pid)
106
- drain_available
107
- @outstanding.delete(pid)
108
- @exits.delete(pid)
109
- end
110
-
111
102
  # Gives up on a server that is lost or stalled: it must not fork another
112
103
  # mutant that a local run is about to repeat.
113
104
  def abandon
@@ -5,12 +5,10 @@ require "forwardable"
5
5
  module Henitai
6
6
  module ForkServer
7
7
  # Runs mutants through the fork server instead of booting the test
8
- # environment in every mutant process. Offers the integration's
9
- # #run_mutant, so the engine's retry and timeout logic is unchanged.
10
- #
11
- # Also stands in for the integration of a parallel run: #spawn_mutant hands
12
- # the scheduler a handle for a process the server forked, and #runtime lets
13
- # it learn of that process's exit from the server's events.
8
+ # environment in every mutant process. Stands in for the integration of
9
+ # the scheduler (ADR-20): #spawn_mutant hands it a handle for a process the
10
+ # server forked, and #runtime lets it learn of that process's exit from
11
+ # the server's events.
14
12
  #
15
13
  # Falls back to the integration's own per-mutant boot when the server is
16
14
  # lost, or when it cannot fork one particular mutant (for example the
@@ -18,16 +16,14 @@ module Henitai
18
16
  class MutantRunner
19
17
  extend Forwardable
20
18
 
21
- EXIT_GRACE_SECONDS = 5.0
22
-
23
19
  def_delegators :@integration, :build_result, :select_tests, :test_files
24
20
 
25
21
  # @param fallback_timeout [#call, nil] test files -> timeout for a mutant
26
- # the parent boots itself; the timeout handed to #run_mutant excludes
27
- # boot while the server is active and would be far too short for it.
22
+ # the parent boots itself; the scheduler's timeout excludes boot while
23
+ # the server is active and would be far too short for it.
28
24
  # rubocop:disable Metrics/ParameterLists
29
25
  def initialize(integration:, client:, env: {}, warner: nil, fallback_timeout: nil,
30
- report_store: Integration::ChildReportStore.new, terminator: GroupTerminator.new)
26
+ report_store: Integration::ChildReportStore.new)
31
27
  # rubocop:enable Metrics/ParameterLists
32
28
  @integration = integration
33
29
  @client = client
@@ -35,7 +31,6 @@ module Henitai
35
31
  @warner = warner || ->(message) { Kernel.warn(message) }
36
32
  @fallback_timeout = fallback_timeout
37
33
  @report_store = report_store
38
- @terminator = terminator
39
34
  @active = true
40
35
  end
41
36
 
@@ -62,15 +57,13 @@ module Henitai
62
57
  spawn_locally(mutant, test_files)
63
58
  end
64
59
 
65
- def run_mutant(mutant:, test_files:, timeout:)
66
- return fallback(mutant, test_files, timeout) unless active?
67
-
68
- run_through_server(mutant, test_files, timeout)
69
- rescue Client::SpawnFailed
70
- fallback(mutant, test_files, timeout)
71
- rescue Client::ServerLost => e
72
- deactivate(e.message)
73
- fallback(mutant, test_files, timeout)
60
+ # Forks on the per-mutant path without asking the server, for a process
61
+ # the server never saw (a control run, ADR-19). The pid is registered
62
+ # with the runtime so the scheduler reaps it, and the timeout includes
63
+ # booting the suite.
64
+ # @return [Integration::ChildHandle]
65
+ def spawn_local(mutant:, test_files:)
66
+ spawn_locally(mutant, test_files)
74
67
  end
75
68
 
76
69
  def stop
@@ -101,30 +94,6 @@ module Henitai
101
94
  slot ? @env.merge(SlotScheduler::WORKER_SLOT_ENV => slot) : @env
102
95
  end
103
96
 
104
- def run_through_server(mutant, test_files, timeout)
105
- log_paths = @integration.mutant_log_paths(mutant)
106
- @report_store.clear(log_paths[:report_path])
107
- pid = @client.spawn_mutant(mutant_id: mutant.id, test_files:, log_paths:, env: @env)
108
- wait_result = wait_for(pid, timeout)
109
- @integration.build_result(wait_result, log_paths)
110
- ensure
111
- @terminator.terminate(pid, leader: false) if pid && wait_result != :timeout
112
- end
113
-
114
- def wait_for(pid, timeout)
115
- status = @client.wait_exit(pid, timeout)
116
- return status if status
117
-
118
- @terminator.terminate(pid)
119
- @client.wait_exit(pid, EXIT_GRACE_SECONDS)
120
- @client.forget(pid)
121
- :timeout
122
- end
123
-
124
- def fallback(mutant, test_files, timeout)
125
- @integration.run_mutant(mutant:, test_files:, timeout: fallback_timeout(test_files) || timeout)
126
- end
127
-
128
97
  def fallback_timeout(test_files)
129
98
  @fallback_timeout&.call(test_files)
130
99
  end
@@ -4,7 +4,6 @@ require_relative "fork_server/protocol"
4
4
  require_relative "fork_server/status"
5
5
  require_relative "fork_server/reaper"
6
6
  require_relative "fork_server/group_killer"
7
- require_relative "fork_server/group_terminator"
8
7
  require_relative "fork_server/server"
9
8
  require_relative "fork_server/launcher"
10
9
  require_relative "fork_server/client"
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ # Progress observer that checkpoints verdicts into the history store while
5
+ # a run is in progress, so an aborted run can be resumed with
6
+ # `--incremental` (ADR-19). Every verdict it sees is final: a
7
+ # {ControlGate} sits in front of all progress observers.
8
+ #
9
+ # Batches with the cadence of {CheckpointReporter} (`checkpoint_every`,
10
+ # `checkpoint_interval`). A failed write is contained like the end-of-run
11
+ # write: one warning, no further checkpoints, the run goes on, and the
12
+ # end-of-run `record` still writes every verdict.
13
+ class HistoryCheckpointer
14
+ # @return [Integer] verdicts in committed checkpoint transactions
15
+ attr_reader :committed
16
+
17
+ # rubocop:disable Metrics/ParameterLists -- one keyword per collaborator
18
+ def initialize(history_store:, config:, run_id:, version: Henitai::VERSION,
19
+ clock: CheckpointReporter::MONOTONIC, warner: ->(message) { Kernel.warn(message) })
20
+ # rubocop:enable Metrics/ParameterLists
21
+ @history_store = history_store
22
+ @config = config
23
+ @run_id = run_id
24
+ @version = version
25
+ @clock = clock
26
+ @warner = warner
27
+ @batch = []
28
+ @committed = 0
29
+ @last_flush_at = clock.call
30
+ end
31
+
32
+ def progress(mutant, **)
33
+ return if @disabled
34
+
35
+ @batch << mutant
36
+ flush if due?
37
+ end
38
+
39
+ # Writes the pending verdicts. The count is updated inside the same
40
+ # signal-deferral section as the write, so a signal arriving during the
41
+ # write cannot leave a committed batch uncounted.
42
+ def flush
43
+ return if @disabled || @batch.empty?
44
+
45
+ SignalDeferral.call { commit(@batch) }
46
+ rescue StandardError => e
47
+ disable(e)
48
+ end
49
+
50
+ private
51
+
52
+ def due?
53
+ @batch.size >= @config.checkpoint_every ||
54
+ (@clock.call - @last_flush_at) >= @config.checkpoint_interval
55
+ end
56
+
57
+ def commit(batch)
58
+ @committed += @history_store.checkpoint(
59
+ batch, version: @version, run_id: @run_id, operators: @config.operators
60
+ )
61
+ @batch = []
62
+ @last_flush_at = @clock.call
63
+ end
64
+
65
+ def disable(error)
66
+ @disabled = true
67
+ @batch = []
68
+ @warner.call("henitai: could not checkpoint mutation history (#{error.class}: #{error.message}); " \
69
+ "verdicts are recorded at the end of the run")
70
+ end
71
+ end
72
+ end
@@ -8,8 +8,9 @@ require_relative "scenario_log_support"
8
8
 
9
9
  module Henitai
10
10
  module Integration
11
- # Base class for all integrations. Provides the framework-agnostic child
12
- # process lifecycle (wait, timeout handling, cleanup) and subprocess
11
+ # Base class for all integrations. Provides the framework-agnostic
12
+ # lifecycle of the baseline-suite process (wait, timeout handling,
13
+ # cleanup; mutant children belong to the SlotScheduler) and subprocess
13
14
  # environment helpers. Concrete adapters mix in MutantRunSupport and
14
15
  # implement #run_tests plus test selection.
15
16
  class Base
@@ -35,19 +36,10 @@ module Henitai
35
36
  raise NotImplementedError
36
37
  end
37
38
 
38
- # Run test files in a child process with the mutant active.
39
- #
40
- # @param mutant [Mutant]
41
- # @param test_files [Array<String>]
42
- # @param timeout [Float] seconds
43
- # @return [ScenarioExecutionResult]
44
- def run_mutant(mutant:, test_files:, timeout:)
45
- raise NotImplementedError
46
- end
47
-
48
39
  # Fork a child process for the mutant without waiting for it to finish.
49
- # Returns a ChildHandle carrying the OS pid and log file paths.
50
- # The caller is responsible for waiting and cleanup.
40
+ # Returns a ChildHandle carrying the OS pid and log file paths. The
41
+ # SlotScheduler waits for it and cleans up (ADR-20); the synchronous
42
+ # wait below serves the baseline suite only.
51
43
  #
52
44
  # @param mutant [Mutant]
53
45
  # @param test_files [Array<String>]