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
@@ -8,14 +8,17 @@ module Henitai
8
8
  # Carries a mutant child's report to the parent through a small JSON file
9
9
  # next to the child's logs. The parent clears the file before each fork so
10
10
  # a retry can never read the previous attempt's report.
11
+ #
12
+ # The child also keeps a running-file marker next to the report: the test
13
+ # file it is executing. A timeout leaves the marker behind, so the parent
14
+ # learns which file hung.
11
15
  class ChildReportStore
16
+ RUNNING_SUFFIX = ".running"
17
+
12
18
  def write(path, data)
13
19
  return unless path
14
20
 
15
- FileUtils.mkdir_p(File.dirname(path))
16
- temp_path = "#{path}.#{Process.pid}.tmp"
17
- File.write(temp_path, JSON.generate(data))
18
- File.rename(temp_path, path)
21
+ write_atomically(path, JSON.generate(data))
19
22
  nil
20
23
  end
21
24
 
@@ -27,10 +30,36 @@ module Henitai
27
30
  nil
28
31
  end
29
32
 
33
+ def write_running(report_path, file)
34
+ return unless report_path
35
+
36
+ write_atomically(running_path(report_path), file.to_s)
37
+ nil
38
+ end
39
+
40
+ def read_running(report_path)
41
+ return unless report_path && File.exist?(running_path(report_path))
42
+
43
+ File.read(running_path(report_path))
44
+ end
45
+
30
46
  def clear(path)
31
- FileUtils.rm_f(path) if path
47
+ return nil unless path
48
+
49
+ FileUtils.rm_f([path, running_path(path)])
32
50
  nil
33
51
  end
52
+
53
+ private
54
+
55
+ def running_path(report_path) = "#{report_path}#{RUNNING_SUFFIX}"
56
+
57
+ def write_atomically(path, content)
58
+ FileUtils.mkdir_p(File.dirname(path))
59
+ temp_path = "#{path}.#{Process.pid}.tmp"
60
+ File.write(temp_path, content)
61
+ File.rename(temp_path, path)
62
+ end
34
63
  end
35
64
  end
36
65
  end
@@ -23,19 +23,12 @@ module Henitai
23
23
  include MutantRunSupport
24
24
  include RspecTestSelection
25
25
 
26
- DEFAULT_SUITE_TIMEOUT = 300.0
27
-
28
26
  def test_files = spec_files
29
27
 
30
28
  def per_test_coverage_supported?
31
29
  true
32
30
  end
33
31
 
34
- def run_mutant(mutant:, test_files:, timeout:)
35
- MinitestLoadPath.ensure!
36
- super
37
- end
38
-
39
32
  def spawn_mutant(mutant:, test_files:)
40
33
  MinitestLoadPath.ensure!
41
34
  super
@@ -47,7 +40,7 @@ module Henitai
47
40
  super
48
41
  end
49
42
 
50
- def run_suite(test_files, timeout: DEFAULT_SUITE_TIMEOUT)
43
+ def run_suite(test_files, timeout: Configuration::DEFAULT_SUITE_TIMEOUT)
51
44
  log_paths = scenario_log_paths("baseline")
52
45
  pid = spawn_suite_process(test_files, log_paths)
53
46
  wait_result = wait_with_timeout(pid, timeout)
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest"
4
+
5
+ module Henitai
6
+ module Integration
7
+ # Ends a mutant child's Minitest run at its first failing test: one
8
+ # failure already kills the mutant. Minitest has no fail-fast of its own;
9
+ # once stopped, every test class that has not started yet reports no test
10
+ # methods, so Minitest skips it. The class running at the failure still
11
+ # finishes. Armed only around the child's Minitest.run, never for the
12
+ # baseline suite.
13
+ module MinitestFailFast
14
+ # Prepended to Minitest::Test's singleton class; Minitest 5 and 6 both
15
+ # ask a test class for its runnable methods when it starts.
16
+ module StoppableSuites
17
+ def runnable_methods
18
+ MinitestFailFast.stopped? ? [] : super
19
+ end
20
+ end
21
+
22
+ class << self
23
+ def arm!
24
+ suites = ::Minitest::Test.singleton_class
25
+ suites.prepend(StoppableSuites) unless suites.include?(StoppableSuites)
26
+ @state = :armed
27
+ end
28
+
29
+ def disarm!
30
+ @state = nil
31
+ end
32
+
33
+ def stop!
34
+ @state = :stopped if @state
35
+ end
36
+
37
+ def armed? = !@state.nil?
38
+
39
+ def stopped? = @state == :stopped
40
+ end
41
+ end
42
+ end
43
+ end
@@ -7,22 +7,38 @@ require "henitai/minitest_result_file"
7
7
  module Henitai
8
8
  module Integration
9
9
  # Records the outcome of each finished Minitest result per test file.
10
+ # +on_failure+ is called for every failing (not skipped) result.
10
11
  class MinitestOutcomeReporter < ::Minitest::Reporter
11
- def initialize(outcomes, report_path: nil, io: $stdout)
12
+ def initialize(outcomes, report_path: nil, io: $stdout, on_failure: nil)
12
13
  super(io, {})
13
14
  @outcomes = outcomes
14
15
  @report_path = report_path
16
+ @on_failure = on_failure
17
+ end
18
+
19
+ # Reports the file of the test about to run, so a timeout can name it.
20
+ def prerecord(klass, name)
21
+ super
22
+ file = MinitestResultFile.class_file_of(klass.name.to_s) || method_file(klass, name)
23
+ @outcomes.started(file) if file
15
24
  end
16
25
 
17
26
  def record(result)
18
27
  super
19
28
  MinitestResultFile.record(@outcomes, result)
29
+ @on_failure&.call if !result.passed? && !result.skipped?
20
30
  end
21
31
 
22
32
  def report
23
33
  super
24
34
  @outcomes.write(@report_path) if @report_path
25
35
  end
36
+
37
+ private
38
+
39
+ def method_file(klass, name)
40
+ MinitestResultFile.usable(klass.instance_method(name).source_location&.first)
41
+ end
26
42
  end
27
43
  end
28
44
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "henitai/integration/minitest_autorun"
4
+ require "henitai/integration/minitest_fail_fast"
4
5
  require "henitai/integration/minitest_outcome_reporter"
5
6
  require "henitai/integration/minitest_runnable_filter"
6
7
  require "henitai/test_file_outcomes"
@@ -27,22 +28,33 @@ module Henitai
27
28
  return 1 if after_load && !after_load.call
28
29
 
29
30
  install_outcome_plugin(outcomes)
31
+ exit_code(run_fail_fast)
32
+ end
33
+
34
+ private
35
+
36
+ # One failing test kills the mutant, so the run stops there.
37
+ def run_fail_fast
38
+ MinitestFailFast.arm!
30
39
  # @type var empty_args: Array[String]
31
40
  empty_args = []
32
- status = ::Minitest.run(empty_args)
41
+ ::Minitest.run(empty_args)
42
+ ensure
43
+ MinitestFailFast.disarm!
44
+ end
45
+
46
+ def exit_code(status)
33
47
  return status if status.is_a?(Integer)
34
48
 
35
49
  status == true ? 0 : 1
36
50
  end
37
51
 
38
- private
39
-
40
52
  def install_outcome_plugin(outcomes)
41
53
  ::Minitest.extensions << PLUGIN_NAME unless ::Minitest.extensions.include?(PLUGIN_NAME)
42
54
  init = :"plugin_#{PLUGIN_NAME}_init"
43
55
  ::Minitest.singleton_class.send(:remove_method, init) if ::Minitest.respond_to?(init)
44
56
  ::Minitest.define_singleton_method(init) do |_options|
45
- reporter.reporters << MinitestOutcomeReporter.new(outcomes)
57
+ reporter.reporters << MinitestOutcomeReporter.new(outcomes, on_failure: -> { MinitestFailFast.stop! })
46
58
  end
47
59
  end
48
60
 
@@ -18,10 +18,6 @@ module Henitai
18
18
  RspecProcessRunner.new.spawn_mutant(self, mutant:, test_files:, log_paths:)
19
19
  end
20
20
 
21
- def run_mutant(mutant:, test_files:, timeout:)
22
- RspecProcessRunner.new.run_mutant(self, mutant:, test_files:, timeout:)
23
- end
24
-
25
21
  def scenario_log_paths(name)
26
22
  reports_dir = ENV.fetch("HENITAI_REPORTS_DIR", "reports")
27
23
  log_dir = File.join(reports_dir, "mutation-logs")
@@ -46,8 +42,13 @@ module Henitai
46
42
  output = { stdout:, stderr:, log_path: log_paths[:log_path] }
47
43
  return ScenarioExecutionResult.build(wait_result:, **output) unless log_paths.key?(:report_path)
48
44
 
45
+ build_mutant_result(wait_result, log_paths, output)
46
+ end
47
+
48
+ def build_mutant_result(wait_result, log_paths, output)
49
49
  report = child_report_store.read(log_paths[:report_path])
50
- ScenarioExecutionResult.build_for_mutant(wait_result:, report:, **output)
50
+ running_test = child_report_store.read_running(log_paths[:report_path])
51
+ ScenarioExecutionResult.build_for_mutant(wait_result:, report:, running_test:, **output)
51
52
  end
52
53
 
53
54
  def run_in_child(mutant:, test_files:, log_paths:)
@@ -70,9 +71,10 @@ module Henitai
70
71
 
71
72
  def run_child_activation_and_tests(mutant:, test_files:, log_paths:)
72
73
  report = ->(data) { child_report_store.write(log_paths[:report_path], data) }
74
+ running = ->(file) { child_report_store.write_running(log_paths[:report_path], file) }
73
75
  scenario_log_support.with_coverage_dir(mutant.id) do
74
76
  scenario_log_support.capture_child_output(log_paths) do
75
- run_tests_in_child(mutant, test_files, report)
77
+ run_tests_in_child(mutant, test_files, report, running)
76
78
  end
77
79
  end
78
80
  end
@@ -108,9 +110,10 @@ module Henitai
108
110
  # files (and the code they require) are loaded, before any test runs.
109
111
  # Activating earlier would load the subject file on its own, without the
110
112
  # superclasses and constants the rest of the application defines.
111
- def run_tests_in_child(mutant, test_files, report)
113
+ # +running+ records the test file being executed, so a timeout can name it.
114
+ def run_tests_in_child(mutant, test_files, report, running)
112
115
  activated = nil
113
- outcomes = TestFileOutcomes.new
116
+ outcomes = TestFileOutcomes.new(on_started: running)
114
117
  exit_code = run_tests(test_files, outcomes) { activated = activate_in_child(mutant, test_files, report) }
115
118
  return finish_without_tests(activated, report) unless activated
116
119
 
@@ -35,17 +35,18 @@ module Henitai
35
35
 
36
36
  # A fork-server process loaded the union of the run's spec files; each
37
37
  # mutant process runs only the groups defined in its own files.
38
+ # The groups run in the order of test_files, so the child's fail-fast
39
+ # stops at the file the parent expects to fail first, whatever RSpec's
40
+ # random ordering did to the top-level groups.
38
41
  def example_groups_to_run(test_files)
39
42
  groups = ::RSpec.world.ordered_example_groups
40
- return groups unless @preloaded_runner
41
-
42
- wanted = test_files.map { |file| File.expand_path(file) }
43
- groups.select { |group| wanted.include?(example_group_file(group)) }
43
+ groups = filter_to_wanted_files(groups, test_files) if @preloaded_runner
44
+ RspecFileOrder.new(test_files).sort(groups)
44
45
  end
45
46
 
46
- def example_group_file(group)
47
- metadata = group.metadata
48
- metadata[:absolute_file_path] || File.expand_path(metadata[:file_path])
47
+ def filter_to_wanted_files(groups, test_files)
48
+ wanted = test_files.map { |file| File.expand_path(file) }
49
+ groups.select { |group| wanted.include?(RspecFileOrder.file_of(group)) }
49
50
  end
50
51
 
51
52
  # The after-load hook (mutant activation) runs only when every spec file
@@ -86,7 +87,11 @@ module Henitai
86
87
  child_debug_log.write("[henitai-debug-child] load_spec_files_return")
87
88
  end
88
89
 
90
+ # One failing example kills the mutant, so the run stops there. Set
91
+ # after the spec files loaded, so a project's own fail_fast setting
92
+ # cannot turn it off.
89
93
  def run_rspec_specs(runner, example_groups)
94
+ ::RSpec.configuration.fail_fast = 1
90
95
  child_debug_log.write("[henitai-debug-child] run_specs_start")
91
96
  result = runner.send(:run_specs, example_groups)
92
97
  child_debug_log.write("[henitai-debug-child] run_specs_return result=#{result.inspect}")
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ module Integration
5
+ # Orders RSpec example groups by the position of their spec file in the
6
+ # list handed to the child. The sort is stable: groups from one file keep
7
+ # RSpec's own order, and groups from files not in the list come last.
8
+ class RspecFileOrder
9
+ # @param group [RSpec::Core::ExampleGroup] a top-level example group
10
+ # @return [String] the group's absolute spec file path
11
+ def self.file_of(group)
12
+ metadata = group.metadata
13
+ metadata[:absolute_file_path] || File.expand_path(metadata[:file_path])
14
+ end
15
+
16
+ # @param test_files [Array<String>] spec files in the order to run them
17
+ def initialize(test_files)
18
+ expanded = test_files.map { |file| File.expand_path(file) }.uniq
19
+ @positions = expanded.each_with_index.to_h
20
+ end
21
+
22
+ # @param groups [Array<RSpec::Core::ExampleGroup>] groups in RSpec's order
23
+ # @return [Array<RSpec::Core::ExampleGroup>] groups sorted by file position
24
+ def sort(groups)
25
+ groups.each_with_index
26
+ .sort_by { |group, index| [position(group), index] }
27
+ .map(&:first)
28
+ end
29
+
30
+ private
31
+
32
+ def position(group)
33
+ @positions.fetch(self.class.file_of(group), @positions.size)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -59,16 +59,6 @@ module Henitai
59
59
 
60
60
  # Runs RSpec child and suite processes on behalf of the integration.
61
61
  class RspecProcessRunner
62
- def run_mutant(integration, mutant:, test_files:, timeout:)
63
- handle = integration.spawn_mutant(mutant:, test_files:)
64
- SchedulerDiagnostics.child_started(handle.pid)
65
- wait_result = integration.wait_with_timeout(handle.pid, timeout)
66
- integration.build_result(wait_result, handle.log_paths)
67
- ensure
68
- SchedulerDiagnostics.child_ended(handle&.pid)
69
- finalize_mutant_run(integration, handle&.pid, wait_result)
70
- end
71
-
72
62
  def run_suite(integration, test_files, timeout:)
73
63
  log_paths = integration.scenario_log_paths("baseline")
74
64
  wait_result = nil
@@ -105,15 +95,6 @@ module Henitai
105
95
  end
106
96
  ChildHandle.new(pid:, log_paths:)
107
97
  end
108
-
109
- private
110
-
111
- def finalize_mutant_run(integration, pid, wait_result)
112
- return unless pid
113
-
114
- integration.cleanup_process_group(pid) unless wait_result == :timeout
115
- integration.reap_child(pid) if wait_result.nil?
116
- end
117
98
  end
118
99
  end
119
100
  end
@@ -12,6 +12,7 @@ require_relative "integration/child_debug_log"
12
12
  require_relative "integration/loaded_features"
13
13
  require_relative "integration/base"
14
14
  require_relative "integration/mutant_run_support"
15
+ require_relative "integration/rspec_file_order"
15
16
  require_relative "integration/rspec_child_runner"
16
17
  require_relative "rspec_outcomes"
17
18
  require_relative "integration/rspec_preload"
@@ -56,15 +57,13 @@ module Henitai
56
57
  include RspecPreload
57
58
  include RspecTestSelection
58
59
 
59
- DEFAULT_SUITE_TIMEOUT = 300.0
60
-
61
60
  def test_files = spec_files
62
61
 
63
62
  def per_test_coverage_supported?
64
63
  true
65
64
  end
66
65
 
67
- def run_suite(test_files, timeout: DEFAULT_SUITE_TIMEOUT)
66
+ def run_suite(test_files, timeout: Configuration::DEFAULT_SUITE_TIMEOUT)
68
67
  RspecProcessRunner.new.run_suite(self, test_files, timeout:)
69
68
  end
70
69
 
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "json"
5
+
6
+ module Henitai
7
+ class KillHistory
8
+ # Reads and writes the `henitai_test_order.json` file behind {KillHistory}.
9
+ #
10
+ # Paths are stored relative to Dir.pwd (the project root) when they are
11
+ # inside it and absolute otherwise; they are expanded again on read. A
12
+ # missing, invalid or foreign-version file reads as empty.
13
+ module Store
14
+ VERSION = 1
15
+ Snapshot = Struct.new(:kills, :hangs)
16
+
17
+ module_function
18
+
19
+ # @return [Snapshot] kills (expanded path => count) and hangs (subject =>
20
+ # expanded paths), both empty when the file is unusable
21
+ def read(path)
22
+ data = parse(path)
23
+ return Snapshot.new({}, {}) unless data.is_a?(Hash) && data["version"] == VERSION
24
+
25
+ Snapshot.new(expand_kills(data["kills"]), expand_hangs(data["hangs"]))
26
+ end
27
+
28
+ def write(path, kills:, hangs:)
29
+ FileUtils.mkdir_p(File.dirname(path))
30
+ File.write(path, JSON.pretty_generate(payload(kills, hangs)))
31
+ end
32
+
33
+ def parse(path)
34
+ JSON.parse(File.read(path))
35
+ rescue Errno::ENOENT, JSON::ParserError
36
+ nil
37
+ end
38
+
39
+ def expand_kills(raw)
40
+ return {} unless raw.is_a?(Hash)
41
+
42
+ raw.select { |_file, count| count.is_a?(Integer) }
43
+ .to_h { |file, count| [File.expand_path(file), count] }
44
+ end
45
+
46
+ def expand_hangs(raw)
47
+ return {} unless raw.is_a?(Hash)
48
+
49
+ raw.to_h { |subject, files| [subject, Array(files).map { |file| File.expand_path(file) }] }
50
+ end
51
+
52
+ def payload(kills, hangs)
53
+ {
54
+ "version" => VERSION,
55
+ "kills" => kills.to_h { |file, count| [stored_path(file), count] }.sort.to_h,
56
+ "hangs" => hangs.to_h { |subject, files| [subject, files.map { |file| stored_path(file) }.uniq.sort] }
57
+ .sort.to_h
58
+ }
59
+ end
60
+
61
+ def stored_path(file)
62
+ root = File.join(Dir.pwd, "")
63
+ file.start_with?(root) ? file.delete_prefix(root) : file
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "kill_history/store"
4
+
5
+ module Henitai
6
+ # Counts, per test file, the mutants it killed so far in this run, so
7
+ # {TestPrioritizer} can put proven killers first for the mutants still to
8
+ # come. A progress observer behind the {ControlGate}: only final kills
9
+ # count, never one a failed control reclassified.
10
+ #
11
+ # It also remembers which test file was running when a child timed out, per
12
+ # subject: a file that hung for one mutant of a subject is probably the
13
+ # hang for the others, so the prioritizer runs it last.
14
+ #
15
+ # Both are persisted in `henitai_test_order.json` (see {Store}): the next run
16
+ # starts from the loaded kills and hangs. A file that killed in this run
17
+ # gets this run's count saved; other files keep their loaded count.
18
+ class KillHistory
19
+ FILE_NAME = "henitai_test_order.json"
20
+
21
+ # @return [KillHistory] seeded from +path+; empty when the file is missing
22
+ def self.load(path)
23
+ snapshot = Store.read(path)
24
+ new(loaded_kills: snapshot.kills, loaded_hangs: snapshot.hangs)
25
+ end
26
+
27
+ def initialize(loaded_kills: {}, loaded_hangs: {})
28
+ @loaded_kills = loaded_kills
29
+ @run_kills = Hash.new(0)
30
+ @hung = {}
31
+ loaded_hangs.each { |subject, files| files.each { |file| @hung[[subject, file]] = true } }
32
+ end
33
+
34
+ # Saves the kills and hangs to +path+, creating its directory.
35
+ def write(path)
36
+ Store.write(path, kills: @loaded_kills.merge(@run_kills), hangs: hangs_by_subject)
37
+ end
38
+
39
+ def progress(mutant, scenario_result: nil)
40
+ case scenario_result&.status
41
+ when :killed then count_kills(scenario_result)
42
+ when :timeout then remember_hang(mutant, scenario_result)
43
+ end
44
+ end
45
+
46
+ # @return [Integer, Object] kills of +file+ (an expanded path), or
47
+ # +default+ when it killed nothing yet
48
+ def fetch(file, default = nil)
49
+ return default unless @loaded_kills.key?(file) || @run_kills.key?(file)
50
+
51
+ @loaded_kills.fetch(file, 0) + @run_kills.fetch(file, 0)
52
+ end
53
+
54
+ # @return [Boolean] whether +file+ hung for a mutant of the same subject
55
+ def hung?(mutant, file)
56
+ key = subject_key(mutant)
57
+ return false unless key && file
58
+
59
+ @hung.key?([key, File.expand_path(file)])
60
+ end
61
+
62
+ private
63
+
64
+ def count_kills(scenario_result)
65
+ scenario_result.killing_tests.each { |file| @run_kills[file] += 1 }
66
+ end
67
+
68
+ def hangs_by_subject
69
+ @hung.keys.group_by(&:first).transform_values { |pairs| pairs.map(&:last) }
70
+ end
71
+
72
+ def remember_hang(mutant, scenario_result)
73
+ key = subject_key(mutant)
74
+ hanging_test = scenario_result.hanging_test if scenario_result.respond_to?(:hanging_test)
75
+ return unless key && hanging_test
76
+
77
+ @hung[[key, File.expand_path(hanging_test)]] = true
78
+ end
79
+
80
+ def subject_key(mutant)
81
+ subject = mutant.subject if mutant.respond_to?(:subject)
82
+ subject.expression if subject.respond_to?(:expression)
83
+ end
84
+ end
85
+ end
@@ -48,6 +48,9 @@ module Henitai
48
48
  :precomputed_stable_id, :precomputed_activation, :precomputed_replacement
49
49
  attr_accessor :status, :killing_test, :duration, :covered_by, :tests_completed,
50
50
  :ignore_reason, :status_reason, :from_cache
51
+ # Set when a shutdown vetoed the flaky-kill retries this verdict would
52
+ # have needed; such a verdict is reported but never cached (ADR-19).
53
+ attr_accessor :retry_cut_short
51
54
  # Set by MutantGenerator only for mutants whose identity would otherwise
52
55
  # collide with another one (see MutantIdentity).
53
56
  attr_accessor :identity_discriminator
@@ -87,6 +90,7 @@ module Henitai
87
90
 
88
91
  def killed? = @status == :killed
89
92
  def from_cache? = @from_cache == true
93
+ def retry_cut_short? = @retry_cut_short == true
90
94
  def survived? = @status == :survived
91
95
  def pending? = @status == :pending
92
96
  def ignored? = @status == :ignored
@@ -103,6 +107,7 @@ module Henitai
103
107
  @killing_test = @duration = @covered_by = @tests_completed = nil
104
108
  @ignore_reason = @status_reason = nil
105
109
  @from_cache = false
110
+ @retry_cut_short = false
106
111
  end
107
112
  end
108
113
  end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ class MutantHistoryStore
5
+ # Connection, schema and transaction handling for {MutantHistoryStore}.
6
+ # Mixed in; relies on its +path+.
7
+ module Database
8
+ private
9
+
10
+ # One transaction per write, committed or rolled back as a whole: signals
11
+ # wait until it ends, and any exception -- not only a StandardError, which
12
+ # is all sqlite3's block form rolls back for -- undoes it.
13
+ def write
14
+ FileUtils.mkdir_p(File.dirname(path))
15
+ SignalDeferral.call do
16
+ with_database do |db|
17
+ ensure_schema(db)
18
+ atomically(db) { yield db }
19
+ end
20
+ end
21
+ end
22
+
23
+ def atomically(db)
24
+ db.transaction
25
+ yield
26
+ db.commit
27
+ rescue Exception # rubocop:disable Lint/RescueException -- an Interrupt must roll back too
28
+ db.rollback if db.transaction_active?
29
+ raise
30
+ end
31
+
32
+ def with_database
33
+ db = SQLite3::Database.new(path)
34
+ db.results_as_hash = true
35
+ yield db
36
+ ensure
37
+ db&.close
38
+ end
39
+
40
+ def ensure_schema(db)
41
+ db.execute_batch(Sql::RUNS_TABLE)
42
+ db.execute_batch(Sql::MUTANTS_TABLE)
43
+ migrate_subject_scans_table(db)
44
+ db.execute_batch(SubjectScans::SUBJECT_SCANS_TABLE)
45
+ migrate_mutants_table(db)
46
+ end
47
+
48
+ # Additive, idempotent in-place migration: databases created before the
49
+ # verdict-cache columns existed gain them on first contact, with existing
50
+ # rows left intact (NULL hashes — valid but never reusable).
51
+ def migrate_mutants_table(db)
52
+ existing = db.execute("PRAGMA table_info(mutants)").map { |row| row["name"] }
53
+ Sql::MIGRATION_COLUMNS.each do |column, type|
54
+ next if existing.include?(column)
55
+
56
+ db.execute("ALTER TABLE mutants ADD COLUMN #{column} #{type}")
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -54,7 +54,9 @@ module Henitai
54
54
  # filter treats as never-reusable — the correct conservative behavior.
55
55
  MIGRATION_COLUMNS = {
56
56
  "subject_source_hash" => "TEXT",
57
- "covered_tests_fingerprint" => "TEXT"
57
+ "covered_tests_fingerprint" => "TEXT",
58
+ "subject_expression" => "TEXT",
59
+ "operators" => "TEXT"
58
60
  }.freeze
59
61
 
60
62
  UPSERT_MUTANT = <<~SQL
@@ -68,9 +70,13 @@ module Henitai
68
70
  status_history,
69
71
  days_alive,
70
72
  subject_source_hash,
71
- covered_tests_fingerprint
72
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
73
+ covered_tests_fingerprint,
74
+ subject_expression,
75
+ operators
76
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
73
77
  ON CONFLICT(mutant_id) DO UPDATE SET
78
+ subject_expression = excluded.subject_expression,
79
+ operators = excluded.operators,
74
80
  last_seen_version = excluded.last_seen_version,
75
81
  last_seen_at = excluded.last_seen_at,
76
82
  current_status = excluded.current_status,