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
@@ -10,32 +10,17 @@ module Henitai
10
10
  INVARIANT_METHODS = %i[is_a? respond_to? kind_of?].freeze
11
11
  DSL_METHODS = %i[let subject before after].freeze
12
12
 
13
- def suppressed?(node, config)
14
- custom_pattern_match?(node, config) || catalog_match?(node)
13
+ def suppressed?(node)
14
+ catalog_match?(node)
15
15
  end
16
16
 
17
17
  private
18
18
 
19
- def custom_pattern_match?(node, config)
20
- source = node.location&.expression&.source
21
- return false unless source
22
-
23
- compiled_ignore_patterns(config).any? do |pattern|
24
- pattern.match?(source)
25
- end
26
- end
27
-
28
- def compiled_ignore_patterns(config)
29
- patterns = Array(config&.ignore_patterns).dup.freeze
30
- @compiled_ignore_patterns ||= {}
31
- @compiled_ignore_patterns[patterns] ||= patterns.map { |pattern| Regexp.new(pattern) }
32
- end
33
-
34
19
  def catalog_match?(node)
35
20
  case node.type
36
21
  when :send
37
22
  send_match?(node)
38
- when :block
23
+ when :block, :numblock
39
24
  block_match?(node)
40
25
  when :or_asgn
41
26
  # Memoization-style ||= is treated as arid on purpose. UpdateOperator
@@ -64,7 +49,7 @@ module Henitai
64
49
  return false unless send_node&.type == :send
65
50
 
66
51
  receiver, method_name, = send_node.children
67
- receiver.nil? && DSL_METHODS.include?(method_name.to_sym)
52
+ (receiver.nil? && DSL_METHODS.include?(method_name.to_sym)) || send_match?(send_node)
68
53
  end
69
54
 
70
55
  def direct_output_call?(receiver, method_name)
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ # Explains a failed baseline suite run, pointing at its log when one exists.
5
+ module BootstrapErrorMessage
6
+ GENERIC = "Configured test suite failed while generating coverage"
7
+
8
+ module_function
9
+
10
+ # +timeout+ is the limit (seconds) the baseline ran under; it is named in
11
+ # the message when the run timed out.
12
+ def build(result, timeout: nil)
13
+ return GENERIC unless result.respond_to?(:log_path)
14
+
15
+ tail = result.tail(12).strip
16
+ message = +headline(result, timeout)
17
+ message << " (see #{result.log_path})"
18
+ message << "\n#{tail}" unless tail.empty?
19
+ message
20
+ end
21
+
22
+ def headline(result, timeout)
23
+ return GENERIC unless timed_out?(result)
24
+
25
+ limit = timeout ? "#{format_seconds(timeout)} s" : "the time limit"
26
+ "Configured test suite did not finish within #{limit} while generating coverage; " \
27
+ "raise mutation.suite_timeout to allow more time"
28
+ end
29
+
30
+ def timed_out?(result)
31
+ result.respond_to?(:status) && result.status == :timeout
32
+ end
33
+
34
+ def format_seconds(seconds)
35
+ seconds.is_a?(Float) && seconds == seconds.floor ? seconds.to_i : seconds
36
+ end
37
+ end
38
+ end
@@ -11,6 +11,11 @@ module Henitai
11
11
  # falls back to the current run's schema by itself -- structurally never
12
12
  # worse than the unconditional overwrite this replaces.
13
13
  #
14
+ # Files the current run lists under `completeFiles` (it mutated every subject
15
+ # of them) are the exception: their prior entry is replaced wholesale, or
16
+ # removed when the run produced no mutants for them, so stale mutants of
17
+ # changed or deleted code cannot linger next to the new ones.
18
+ #
14
19
  # Merges at mutant granularity, keyed by `stableId`, not file granularity:
15
20
  # a `--survivors-from` rerun's schema only contains the re-verified
16
21
  # survivors for a file, so replacing that file's whole entry would drop
@@ -37,10 +42,12 @@ module Henitai
37
42
  end
38
43
 
39
44
  def self.merge_files(current, prior, prune_missing:)
40
- merged_files = overlay_current_mutants(deep_dup(prior["files"]), current)
45
+ prior_files = deep_dup(prior["files"])
46
+ Array(current["completeFiles"]).each { |file| prior_files.delete(file) }
47
+ merged_files = overlay_current_mutants(prior_files, current)
41
48
  merged_files.reject! { |_, file| file["mutants"].empty? }
42
49
  prune_missing_source_files(merged_files) if prune_missing
43
- current.merge("files" => merged_files)
50
+ current.except("completeFiles").merge("files" => merged_files)
44
51
  end
45
52
  private_class_method :merge_files
46
53
 
@@ -68,11 +75,25 @@ module Henitai
68
75
  def self.append_current_mutants(merged_files, current)
69
76
  current.fetch("files", {}).each do |file, entry|
70
77
  target = merged_files[file] ||= { "mutants" => [] }
78
+ merge_provenance(target, entry)
71
79
  target["source"] = entry["source"]
72
80
  target["language"] = entry["language"]
73
81
  target["mutants"] += entry["mutants"]
74
82
  end
75
83
  end
84
+
85
+ # A file merged from several sessions has no single snapshot holding all
86
+ # its mutants, so its provenance is dropped unless both sides agree.
87
+ def self.merge_provenance(target, entry)
88
+ provenance = entry.slice("sessionId", "gitSha")
89
+ # A fresh run with no prior mutants left in this file: the current
90
+ # entry's provenance covers every mutant in it.
91
+ fresh = target["mutants"].empty?
92
+ agree = fresh || target["sessionId"] == entry["sessionId"]
93
+ %w[sessionId gitSha].each { |key| target.delete(key) }
94
+ target.merge!(provenance) if agree
95
+ end
96
+ private_class_method :merge_provenance
76
97
  private_class_method :append_current_mutants
77
98
 
78
99
  def self.mutant_ids(schema)
@@ -33,6 +33,11 @@ module Henitai
33
33
  flush! if due?
34
34
  end
35
35
 
36
+ # Writes the pending batch, e.g. when the run is interrupted (ADR-19).
37
+ def flush
38
+ flush! unless @batch.empty?
39
+ end
40
+
36
41
  private
37
42
 
38
43
  def due?
@@ -57,14 +57,19 @@ module Henitai
57
57
  HELP
58
58
  end
59
59
 
60
+ # Sets nest, so each section lists only what it adds to the previous
61
+ # one; printing every set in full would repeat the light operators
62
+ # three times. The header states the nesting so a section title is
63
+ # never mistaken for the whole set.
60
64
  def operator_list_text
61
65
  validate_operator_metadata!
62
66
  sections = [
63
67
  operator_list_section("Light set", Operator::LIGHT_SET),
64
- operator_list_section("Full set", Operator::FULL_SET)
68
+ operator_list_section("Full set (adds to light)", Operator::FULL_SET - Operator::LIGHT_SET),
69
+ operator_list_section("Hard set (adds to full)", Operator::HARD_SET - Operator::FULL_SET)
65
70
  ]
66
71
 
67
- ["Available operators", *sections].join("\n")
72
+ ["Available operators (full includes light, hard includes full)", *sections].join("\n")
68
73
  end
69
74
 
70
75
  def operator_list_section(title, names)
@@ -86,8 +91,10 @@ module Henitai
86
91
  ["No metadata available", "n/a"]
87
92
  end
88
93
 
94
+ # The widest set is the registry: every operator henitai can name needs
95
+ # a metadata row, not just the ones a default run enables.
89
96
  def validate_operator_metadata!
90
- missing = Operator::FULL_SET - operator_metadata.keys
97
+ missing = Operator::HARD_SET - operator_metadata.keys
91
98
  return if missing.empty?
92
99
 
93
100
  raise ArgumentError, "Missing operator metadata for: #{missing.join(', ')}"
@@ -43,7 +43,8 @@ module Henitai
43
43
  survivors_from: resolved_survivors_from,
44
44
  mode: {
45
45
  dry_run: options.fetch(:dry_run, false),
46
- incremental: options.fetch(:incremental, false) && !options.fetch(:force, false)
46
+ incremental: options.fetch(:incremental, false) && !options.fetch(:force, false),
47
+ force: options.fetch(:force, false)
47
48
  }
48
49
  )
49
50
  runner.run
@@ -64,7 +65,7 @@ module Henitai
64
65
  # being a snapshot path; this keeps activation-recipes lookup correct.
65
66
  return survivors_from if File.basename(parent_dir) == "sessions"
66
67
 
67
- session_id = session_id_from_report(survivors_from)
68
+ session_id = single_session_id_from_report(survivors_from)
68
69
  return survivors_from if session_id.nil?
69
70
 
70
71
  snapshot_path = survivors_snapshot_path(report_dir, session_id)
@@ -85,9 +86,16 @@ module Henitai
85
86
  File.join(report_dir, "sessions", session_id, "mutation-report.json")
86
87
  end
87
88
 
88
- def session_id_from_report(path)
89
+ # The report's sessionId only names a snapshot holding every survivor when
90
+ # each file entry was produced by that very session. A merged canonical
91
+ # report (or a legacy one without per-file provenance) is used as is.
92
+ def single_session_id_from_report(path)
89
93
  parsed = JSON.parse(File.read(path))
90
- parsed["sessionId"]
94
+ session_id = parsed["sessionId"]
95
+ files = parsed["files"]
96
+ return nil unless files.is_a?(Hash) && files.each_value.all? { |f| f["sessionId"] == session_id }
97
+
98
+ session_id
91
99
  rescue JSON::ParserError, Errno::ENOENT
92
100
  nil
93
101
  end
@@ -104,6 +112,18 @@ module Henitai
104
112
  strict_status || threshold_status_for(result, config)
105
113
  end
106
114
 
115
+ # An empty score denominator passes only when there was nothing to score
116
+ # (no changed code, everything ignored). When harness errors emptied it,
117
+ # nothing was tested, and the run must not pass its threshold.
118
+ def empty_score_status(result)
119
+ harness_errors = result.mutants.count { |mutant| mutant.status == :compile_error }
120
+ return 0 if harness_errors.zero?
121
+
122
+ warn "henitai: no mutant could be scored: #{harness_errors} harness error(s) " \
123
+ "(CompileError, see statusReason in the report)"
124
+ 1
125
+ end
126
+
107
127
  # Expanded, opt-in exit codes (precedence: timeout > runtime/compile
108
128
  # error > threshold miss). The timeout code is informational and
109
129
  # independent of coverage_criteria.timeout: a run can pass its threshold
@@ -118,9 +138,7 @@ module Henitai
118
138
 
119
139
  def threshold_status_for(result, config)
120
140
  score = result.mutation_score
121
- # No valid mutants to evaluate (e.g. an incremental run with no changed
122
- # code) cannot fail a threshold — treat it as success.
123
- return 0 if score.nil?
141
+ return empty_score_status(result) if score.nil?
124
142
 
125
143
  score.to_i >= config.thresholds.fetch(:low, 60) ? 0 : 1
126
144
  end
@@ -79,7 +79,7 @@ module Henitai
79
79
  def add_force_option(opts, options)
80
80
  opts.on(
81
81
  "--force",
82
- "Bypass verdict reuse and execute every mutant (only meaningful with --incremental)"
82
+ "Bypass verdict reuse (--incremental, --survivors-from) and execute every mutant"
83
83
  ) do
84
84
  options[:force] = true
85
85
  end
data/lib/henitai/cli.rb CHANGED
@@ -17,7 +17,7 @@ module Henitai
17
17
  # --since GIT_REF Only mutate subjects changed since GIT_REF
18
18
  # --use INTEGRATION Override integration from config (e.g. rspec)
19
19
  # --config PATH Path to .henitai.yml (default: .henitai.yml)
20
- # --operators SET Operator set: light (default) | full
20
+ # --operators SET Operator set: light (default) | full | hard
21
21
  # --jobs N Number of parallel workers (default: 1)
22
22
  # --all-logs Print all captured child logs
23
23
  # -h, --help Show this help message
@@ -37,7 +37,11 @@ module Henitai
37
37
  %w[coverage .resultset.json],
38
38
  %w[coverage .last_run.json],
39
39
  ["henitai_per_test.json"],
40
- [CoverageBootstrapper::DEPENDENCY_MANIFEST_FILE]
40
+ [PerTestCoverageCollector::BOOT_FILE_NAME],
41
+ [CoverageBootstrapper::DEPENDENCY_MANIFEST_FILE],
42
+ [FlakyTestRegistry::FILE_NAME],
43
+ [KillHistory::FILE_NAME],
44
+ [TestFileOutcomes::REPORT_FILE_NAME]
41
45
  ].freeze
42
46
 
43
47
  # Whole directories of per-mutant scratch artifacts, removed recursively.
@@ -9,8 +9,11 @@ module Henitai
9
9
  # when terminal output is on, and the checkpoint writer when enabled and a
10
10
  # file report (json/html) is configured. Returns a single reporter when only
11
11
  # one is active, the composite when several are, or nil when none are.
12
- def self.for(config:, source_provider:, full_run:)
13
- active = [terminal_for(config), checkpoint_for(config, source_provider, full_run)].compact
12
+ #
13
+ # A {HistoryCheckpointer}, when given, joins them (ADR-19).
14
+ def self.for(config:, source_provider:, full_run:, history_checkpointer: nil)
15
+ active = [terminal_for(config), checkpoint_for(config, source_provider, full_run),
16
+ history_checkpointer].compact
14
17
  return active.first if active.one?
15
18
 
16
19
  active.empty? ? nil : new(active)
@@ -38,5 +41,15 @@ module Henitai
38
41
  def progress(mutant, scenario_result: nil)
39
42
  @reporters.each { |reporter| reporter.progress(mutant, scenario_result:) }
40
43
  end
44
+
45
+ # Flushes every reporter that buffers. Each failure is reported and
46
+ # contained, so one reporter cannot keep the others from saving.
47
+ def flush
48
+ @reporters.each do |reporter|
49
+ reporter.flush if reporter.respond_to?(:flush)
50
+ rescue StandardError => e
51
+ warn "henitai: could not flush progress (#{e.class}: #{e.message})"
52
+ end
53
+ end
41
54
  end
42
55
  end
@@ -22,6 +22,9 @@ module Henitai
22
22
  # Ceiling for the per-mutant auto-calibrated timeout (seconds). Keeps a
23
23
  # runaway from running for minutes when the calibrated value is large.
24
24
  DEFAULT_MAX_TIMEOUT = 30.0
25
+ # Limit (seconds) for one baseline suite run: coverage bootstrap and flaky
26
+ # detection. A run that exceeds it reports a timeout, not a failure.
27
+ DEFAULT_SUITE_TIMEOUT = 900.0
25
28
  # Incremental report checkpoint cadence for long full runs.
26
29
  DEFAULT_CHECKPOINT_EVERY = 200
27
30
  DEFAULT_CHECKPOINT_INTERVAL = 30.0
@@ -43,7 +46,8 @@ module Henitai
43
46
 
44
47
  attr_reader :integration, :includes, :excludes, :test_excludes, :operators, :timeout,
45
48
  :timeout_multiplier, :ignore_patterns, :sampling, :jobs,
46
- :max_flaky_retries, :max_log_bytes, :max_timeout,
49
+ :max_flaky_retries, :max_log_bytes, :max_timeout, :suite_timeout,
50
+ :control_runs, :flaky_detection, :fork_server,
47
51
  :coverage_criteria, :thresholds,
48
52
  :reporters, :reports_dir,
49
53
  :checkpoint_enabled, :checkpoint_every, :checkpoint_interval,
@@ -124,6 +128,10 @@ module Henitai
124
128
  @max_flaky_retries = mutation.fetch(:max_flaky_retries, DEFAULT_MAX_FLAKY_RETRIES)
125
129
  @max_log_bytes = mutation[:max_log_bytes] || DEFAULT_MAX_LOG_BYTES
126
130
  @max_timeout = mutation[:max_timeout] || DEFAULT_MAX_TIMEOUT
131
+ @suite_timeout = mutation[:suite_timeout] || DEFAULT_SUITE_TIMEOUT
132
+ @control_runs = mutation.fetch(:control_runs, true)
133
+ @flaky_detection = mutation.fetch(:flaky_detection, false)
134
+ @fork_server = mutation.fetch(:fork_server, false)
127
135
  end
128
136
 
129
137
  def apply_reports_defaults(raw)
@@ -100,6 +100,14 @@ module Henitai
100
100
  Scalars.validate_max_flaky_retries(value[:max_flaky_retries])
101
101
  Scalars.validate_max_log_bytes(value[:max_log_bytes])
102
102
  Scalars.validate_max_timeout(value[:max_timeout])
103
+ Scalars.validate_suite_timeout(value[:suite_timeout])
104
+ validate_mutation_flags(value)
105
+ end
106
+
107
+ def validate_mutation_flags(value)
108
+ %i[control_runs flaky_detection fork_server].each do |key|
109
+ Scalars.validate_boolean(value[key], "mutation.#{key}") unless value[key].nil?
110
+ end
103
111
  end
104
112
 
105
113
  def validate_mutation_filters(value)
@@ -133,6 +141,7 @@ module Henitai
133
141
  Scalars.validate_sampling_completeness(value)
134
142
  Scalars.validate_sampling_ratio(value[:ratio])
135
143
  Scalars.validate_sampling_strategy(value[:strategy])
144
+ Scalars.validate_sampling_seed(value[:seed])
136
145
  end
137
146
 
138
147
  def warn_unknown_keys(raw, allowed_keys, path = nil)
@@ -111,6 +111,15 @@ module Henitai
111
111
  )
112
112
  end
113
113
 
114
+ def validate_suite_timeout(value)
115
+ return if value.nil?
116
+ return if value.is_a?(Numeric) && value.positive?
117
+
118
+ Rules.configuration_error(
119
+ "Invalid configuration value for mutation.suite_timeout: expected positive Numeric, got #{value.inspect}"
120
+ )
121
+ end
122
+
114
123
  def validate_checkpoint_every(value)
115
124
  return if value.nil?
116
125
  return if value.is_a?(Integer) && value.positive?
@@ -150,6 +159,15 @@ module Henitai
150
159
  )
151
160
  end
152
161
 
162
+ def validate_sampling_seed(value)
163
+ return if value.nil?
164
+ return if value.is_a?(Integer) && value >= 0
165
+
166
+ Rules.configuration_error(
167
+ "Invalid configuration value for mutation.sampling.seed: expected Integer >= 0, got #{value.inspect}"
168
+ )
169
+ end
170
+
153
171
  def validate_sampling_completeness(value)
154
172
  return if value.key?(:ratio) && value.key?(:strategy)
155
173
 
@@ -158,6 +176,10 @@ module Henitai
158
176
  )
159
177
  end
160
178
 
179
+ # The result is only ever interpolated into an error message, and
180
+ # Class#to_s returns exactly Class#name for the named classes a config
181
+ # value can hold — so dropping `.name` produces identical output.
182
+ # henitai:disable MethodChainUnwrap: `.class` and `.class.name` interpolate identically
161
183
  def describe_array_type(value)
162
184
  return value.class.name unless value.is_a?(Array)
163
185
 
@@ -25,10 +25,12 @@ module Henitai
25
25
  jobs
26
26
  ].freeze
27
27
  VALID_MUTATION_KEYS = %i[
28
- operators timeout timeout_multiplier ignore_patterns max_flaky_retries max_log_bytes max_timeout sampling
28
+ operators timeout timeout_multiplier ignore_patterns max_flaky_retries max_log_bytes max_timeout
29
+ suite_timeout sampling
30
+ control_runs flaky_detection fork_server
29
31
  ].freeze
30
32
  VALID_REPORTS_KEYS = %i[checkpoint checkpoint_every checkpoint_interval].freeze
31
- VALID_SAMPLING_KEYS = %i[ratio strategy].freeze
33
+ VALID_SAMPLING_KEYS = %i[ratio strategy seed].freeze
32
34
  VALID_COVERAGE_CRITERIA_KEYS = %i[test_result timeout process_abort].freeze
33
35
  VALID_THRESHOLDS_KEYS = %i[high low].freeze
34
36
  VALID_DASHBOARD_KEYS = %i[project base_url].freeze
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ # Names constants as the source writes them and attributes them to the
5
+ # namespace they belong to, as far as the source alone can tell.
6
+ module ConstantPath
7
+ module_function
8
+
9
+ # The constant path of a +const+ or +casgn+ node as written;
10
+ # root-qualified paths keep their leading +::+. nil for other nodes.
11
+ def of(node)
12
+ return unless node.respond_to?(:type)
13
+
14
+ case node.type
15
+ when :const, :casgn
16
+ parent = of(node.children[0])
17
+ own = node.children[1].to_s
18
+ parent.nil? ? own : "#{parent}::#{own}"
19
+ when :cbase
20
+ ""
21
+ end
22
+ end
23
+
24
+ # +name+ inside +namespace+. A root-qualified name (+::Top+) ignores the
25
+ # enclosing namespace.
26
+ def qualify(namespace, name)
27
+ return name.delete_prefix("::") if name&.start_with?("::")
28
+ return name if namespace.nil? || namespace.empty?
29
+ return namespace if name.nil? || name.empty?
30
+
31
+ "#{namespace}::#{name}"
32
+ end
33
+
34
+ # The object a singleton method or singleton class is defined on: the
35
+ # enclosing namespace for +self+, the named constant otherwise, nil for
36
+ # any other object. A relative constant is attributed to the innermost
37
+ # lexical candidate, which Ruby also tries first. The source alone cannot
38
+ # tell whether +Other+ inside +module Outer+ is +Outer::Other+ or
39
+ # +::Other+, so this name can be off; activation resolves the real object
40
+ # at runtime (Mutant::Activator).
41
+ def owner(receiver, namespace)
42
+ case receiver.type
43
+ when :self then namespace
44
+ when :const then qualify(namespace, of(receiver))
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ # Holds a detected verdict back from the progress observers until a control
5
+ # run has shown that the unmutated method passes the same tests (ADR-14,
6
+ # ADR-19). Every verdict an observer sees is therefore final: the terminal,
7
+ # the report checkpoints and the history never see a detection that a later
8
+ # control would reclassify.
9
+ #
10
+ # One control runs per {ControlRun#group_key}; its outcome is memoized and
11
+ # applied to every later detection of the group. A failing control
12
+ # reclassifies the mutant and its scenario result as CompileError.
13
+ #
14
+ # The launcher starts a group's control; the scheduler that owns it reports
15
+ # the outcome through {#control_finished}, and the gate holds the group's
16
+ # detections until then (ADR-20: controls are scheduler jobs).
17
+ class ControlGate
18
+ attr_reader :stats
19
+
20
+ def initialize(downstream:, control_run:, launcher:)
21
+ @downstream = downstream
22
+ @control_run = control_run
23
+ @launcher = launcher
24
+ @outcomes = {}
25
+ @held = {}
26
+ @stats = { verified: 0, failed: 0, reclassified: 0 }
27
+ end
28
+
29
+ def progress(mutant, scenario_result: nil)
30
+ return deliver(mutant, scenario_result) unless @control_run.controllable?(mutant)
31
+
32
+ key = @control_run.group_key(mutant)
33
+ return release(mutant, scenario_result, @outcomes[key]) if @outcomes.key?(key)
34
+
35
+ hold(key, mutant, scenario_result)
36
+ end
37
+
38
+ def control_finished(key, result)
39
+ @outcomes[key] = result
40
+ @stats[:verified] += 1
41
+ @stats[:failed] += 1 unless @control_run.passed?(result)
42
+ Array(@held.delete(key)).each { |mutant, scenario_result| release(mutant, scenario_result, result) }
43
+ end
44
+
45
+ # Discards every held detection, e.g. on shutdown; they re-execute in the
46
+ # next run. @return [Integer] how many were dropped
47
+ def drop_held
48
+ count = @held.values.sum(&:size)
49
+ @held.clear
50
+ count
51
+ end
52
+
53
+ private
54
+
55
+ def hold(key, mutant, scenario_result)
56
+ first = !@held.key?(key)
57
+ (@held[key] ||= []) << [mutant, scenario_result]
58
+ @launcher.call(key, mutant) if first
59
+ end
60
+
61
+ def release(mutant, scenario_result, control_result)
62
+ return deliver(mutant, scenario_result) if @control_run.passed?(control_result)
63
+
64
+ @stats[:reclassified] += @control_run.reclassify([mutant], control_result)
65
+ deliver(mutant, reclassified(scenario_result, mutant))
66
+ end
67
+
68
+ def reclassified(scenario_result, mutant)
69
+ return scenario_result unless scenario_result.respond_to?(:reclassified)
70
+
71
+ scenario_result.reclassified(status: mutant.status, status_reason: mutant.status_reason)
72
+ end
73
+
74
+ def deliver(mutant, scenario_result)
75
+ @downstream&.progress(mutant, scenario_result:)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ # Verifies that the activation path itself does not break a subject's tests.
5
+ #
6
+ # For every subject and test-file set with a detected verdict (Killed,
7
+ # Timeout, RuntimeError) the unmutated method is injected through the same
8
+ # activation and fork path, against the same test files. The tests must
9
+ # pass. If they do not, injecting the method is what made them fail -- a
10
+ # lost constant scope, `super` or `yield` inside `define_method`, a rebuilt
11
+ # parameter list -- so none of the group's detections can be credited to
12
+ # their mutations. They are reclassified as CompileError with the control's
13
+ # outcome as reason. {ControlGate} decides when a control runs: at the
14
+ # group's first detection (ADR-19); the scheduler runs it as a control job
15
+ # (`SlotScheduler::ControlSlots`, ADR-20).
16
+ #
17
+ # Verdicts reused from history are left alone: they were not produced by
18
+ # this run's activation.
19
+ class ControlRun
20
+ DETECTED = %i[killed timeout runtime_error].freeze
21
+
22
+ # True for a verdict this run's activation produced and a control can
23
+ # check: a detection with the original node at hand. Reused verdicts and
24
+ # activation-recipe stubs (no original node) are not controllable.
25
+ def controllable?(mutant)
26
+ DETECTED.include?(mutant.status) && !from_cache?(mutant) && original_node?(mutant)
27
+ end
28
+
29
+ # Detections sharing a key share one control run.
30
+ def group_key(mutant) = [mutant.subject, test_files(mutant)]
31
+
32
+ def passed?(result) = result.status == :survived
33
+
34
+ # The unmutated method as a mutant the activation path can inject.
35
+ def control_mutant(mutant)
36
+ node = mutant.original_node
37
+ Mutant.new(subject: mutant.subject, operator: "Control", nodes: { original: node, mutated: node },
38
+ description: "unmutated control", location: mutant.location)
39
+ end
40
+
41
+ # The test files a group's control runs against: the detection's own.
42
+ def test_files(mutant)
43
+ Array(mutant.covered_by).sort
44
+ end
45
+
46
+ # @return [Integer] the number of reclassified mutants
47
+ def reclassify(detected, result)
48
+ reason = reason_for(result)
49
+ detected.each do |mutant|
50
+ mutant.status = :compile_error
51
+ mutant.status_reason = reason
52
+ end
53
+ detected.size
54
+ end
55
+
56
+ private
57
+
58
+ def from_cache?(mutant) = mutant.respond_to?(:from_cache?) && mutant.from_cache?
59
+
60
+ def original_node?(mutant) = mutant.respond_to?(:original_node) && !mutant.original_node.nil?
61
+
62
+ def reason_for(result)
63
+ "control run failed: the unmutated method fails its tests when injected " \
64
+ "(#{[result.status, result.status_reason].compact.join(': ')})"
65
+ end
66
+ end
67
+ end