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
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ class MutantHistoryStore
5
+ # Tracks when each subject was last mutated in full, which is what lets
6
+ # the trend export tell a retired mutant identity ("its subject was
7
+ # regenerated and this id was not among the results") from one that was
8
+ # merely out of scope ("no run has mutated that subject since").
9
+ #
10
+ # Without the per-subject baseline any narrow run — `--since`, a single
11
+ # subject pattern — would retire every mutant it did not re-record.
12
+ # Mixed into {MutantHistoryStore}.
13
+ module SubjectScans
14
+ # When each subject was last mutated in full. Partial reruns
15
+ # (`--survivors-from`) deliberately do not touch this table: they
16
+ # record a subset of a subject's mutants, which must not make the rest
17
+ # look retired.
18
+ # Keyed by operator set as well as subject: a `light` scan regenerates
19
+ # only the light mutants, so it can speak for those and no others.
20
+ SUBJECT_SCANS_TABLE = <<~SQL
21
+ CREATE TABLE IF NOT EXISTS subject_scans (
22
+ subject_expression TEXT NOT NULL,
23
+ operators TEXT NOT NULL,
24
+ last_scanned_at TEXT NOT NULL,
25
+ PRIMARY KEY (subject_expression, operators)
26
+ );
27
+ SQL
28
+
29
+ UPSERT_SUBJECT_SCAN = <<~SQL
30
+ INSERT INTO subject_scans (subject_expression, operators, last_scanned_at)
31
+ VALUES (?, ?, ?)
32
+ ON CONFLICT(subject_expression, operators) DO UPDATE SET
33
+ last_scanned_at = excluded.last_scanned_at
34
+ WHERE excluded.last_scanned_at > subject_scans.last_scanned_at
35
+ SQL
36
+
37
+ # A mutant is retired when its own subject has been mutated in full
38
+ # since it was last seen: the scan regenerated that subject's mutants
39
+ # and this identity was not among them, so it can never match again.
40
+ # Rows predating the subject_expression column (NULL) have no scan to
41
+ # compare against and stay live — the conservative reading.
42
+ LOAD_MUTANTS = <<~SQL
43
+ SELECT mutants.*
44
+ FROM mutants
45
+ LEFT JOIN subject_scans
46
+ ON subject_scans.subject_expression = mutants.subject_expression
47
+ AND subject_scans.operators = mutants.operators
48
+ WHERE subject_scans.last_scanned_at IS NULL
49
+ OR mutants.last_seen_at >= subject_scans.last_scanned_at
50
+ ORDER BY mutants.first_seen_at, mutants.mutant_id
51
+ SQL
52
+
53
+ private
54
+
55
+ # subject_scans carries a composite primary key, which SQLite cannot add
56
+ # by ALTER TABLE. The table is a derived retirement baseline, never a
57
+ # record of anything: dropping it means "no subject has been scanned
58
+ # yet", so the next export retires nothing until a full scan rebuilds it.
59
+ # Losing it is conservative; migrating it in place is not possible.
60
+ def migrate_subject_scans_table(db)
61
+ columns = db.execute("PRAGMA table_info(subject_scans)").map { |row| row["name"] }
62
+ return if columns.empty? || columns.include?("operators")
63
+
64
+ db.execute("DROP TABLE subject_scans")
65
+ end
66
+
67
+ # Only a run that regenerated a subject's mutants in full may advance
68
+ # its baseline. Three things narrow a run below that bar, and all three
69
+ # must leave the baseline alone or they retire mutants they merely did
70
+ # not produce:
71
+ #
72
+ # - partial reruns (`--survivors-from`), which re-record only survivors
73
+ # - sampling (`mutation.sampling.ratio`), which cuts the generated set
74
+ # at generation time — this repo's own config samples at 0.05
75
+ # - a narrower operator set, handled by keying the scan on `operators`
76
+ # so a `light` scan never speaks for `full`-only mutants
77
+ # Only a run that generated every mutant of its subjects under a known
78
+ # operator set may advance the retirement baseline.
79
+ def complete_scan?(result, full_scan, operators)
80
+ full_scan && operators && !partial_rerun?(result)
81
+ end
82
+
83
+ def record_subject_scans(db, result, recorded_at, operators)
84
+ subject_expressions(result).each do |expression|
85
+ db.execute(
86
+ UPSERT_SUBJECT_SCAN,
87
+ [expression, operators.to_s, recorded_at.iso8601]
88
+ )
89
+ end
90
+ end
91
+
92
+ def subject_expressions(result)
93
+ Array(result.mutants).filter_map { |mutant| mutant_subject_expression(mutant) }.uniq
94
+ end
95
+
96
+ def mutant_subject_expression(mutant)
97
+ mutant.subject&.expression
98
+ end
99
+ end
100
+ end
101
+ end
@@ -21,16 +21,39 @@ module Henitai
21
21
  status = mutant.status.to_s
22
22
  return [nil, nil] unless CACHEABLE_STATUSES.include?(status)
23
23
  return existing_cache_bindings(existing_row) if from_cache?(mutant)
24
+ return [nil, nil] if retry_cut_short?(mutant)
24
25
  return killed_cache_bindings(mutant) if status == "killed"
25
26
 
26
27
  survived_cache_bindings(mutant, existing_row)
27
28
  end
28
29
 
29
30
  def killed_cache_bindings(mutant)
30
- [
31
- VerdictFingerprint.subject_source_hash(mutant),
32
- VerdictFingerprint.tests_fingerprint(covered_tests_for(mutant), context_sha: reuse_context&.fingerprint)
33
- ]
31
+ tests = covered_tests_for(mutant)
32
+ executed_bindings(mutant, tests) do
33
+ [
34
+ VerdictFingerprint.subject_source_hash(mutant),
35
+ VerdictFingerprint.tests_fingerprint(tests, context_sha: reuse_context&.fingerprint)
36
+ ]
37
+ end
38
+ end
39
+
40
+ # Fingerprints describe the inputs that were executed only while the
41
+ # provenance reports them unchanged since the run's capture (ADR-19).
42
+ # Checked before and after hashing, so an edit during hashing cannot
43
+ # slip in either.
44
+ def executed_bindings(mutant, test_paths)
45
+ paths = [subject_source_file(mutant), *Array(test_paths)].compact
46
+ return [nil, nil] unless inputs_intact?(paths)
47
+
48
+ bindings = yield
49
+ inputs_intact?(paths) ? bindings : [nil, nil]
50
+ end
51
+
52
+ def inputs_intact?(paths) = provenance.nil? || provenance.intact?(paths)
53
+
54
+ def subject_source_file(mutant)
55
+ subject = mutant.subject
56
+ subject.source_file if subject.respond_to?(:source_file)
34
57
  end
35
58
 
36
59
  # Survived rows fingerprint the full-map intersection set — the same
@@ -42,9 +65,14 @@ module Henitai
42
65
  # row of the same subject source: a killed row's or an edited method's
43
66
  # fingerprints would vouch for a state this survivor never ran in.
44
67
  def survived_cache_bindings(mutant, existing_row)
68
+ tests = per_test_coverage&.tests_covering(mutant)
69
+ executed_bindings(mutant, tests) { survived_bindings(mutant, tests, existing_row) }
70
+ end
71
+
72
+ def survived_bindings(mutant, tests, existing_row)
45
73
  bindings = [
46
74
  VerdictFingerprint.subject_source_hash(mutant),
47
- survived_tests_fingerprint(mutant)
75
+ survived_tests_fingerprint(tests)
48
76
  ]
49
77
  return bindings unless bindings.any?(&:nil?)
50
78
  return [nil, nil] unless carry_survived_bindings?(bindings.first, existing_row)
@@ -58,13 +86,10 @@ module Henitai
58
86
  source_hash.nil? || source_hash == existing_row["subject_source_hash"]
59
87
  end
60
88
 
61
- def survived_tests_fingerprint(mutant)
62
- return nil unless per_test_coverage
89
+ def survived_tests_fingerprint(tests)
90
+ return nil unless tests
63
91
 
64
- VerdictFingerprint.survivor_tests_fingerprint(
65
- per_test_coverage.tests_covering(mutant),
66
- context_sha: reuse_context&.fingerprint
67
- )
92
+ VerdictFingerprint.survivor_tests_fingerprint(tests, context_sha: reuse_context&.fingerprint)
68
93
  end
69
94
 
70
95
  def existing_cache_bindings(existing_row)
@@ -77,6 +102,10 @@ module Henitai
77
102
  mutant.respond_to?(:from_cache?) && mutant.from_cache?
78
103
  end
79
104
 
105
+ def retry_cut_short?(mutant)
106
+ mutant.respond_to?(:retry_cut_short?) && mutant.retry_cut_short?
107
+ end
108
+
80
109
  def covered_tests_for(mutant)
81
110
  mutant.respond_to?(:covered_by) ? mutant.covered_by : nil
82
111
  end
@@ -7,40 +7,71 @@ require "sqlite3"
7
7
  require "time"
8
8
  require_relative "mutant_identity"
9
9
  require_relative "mutant_history_store/sql"
10
+ require_relative "mutant_history_store/subject_scans"
10
11
  require_relative "mutant_history_store/verdict_cache"
12
+ require_relative "mutant_history_store/database"
11
13
 
12
14
  module Henitai
13
15
  # Persists mutant outcomes across runs in a lightweight SQLite database.
14
16
  class MutantHistoryStore
17
+ # What every mutant row of one write shares.
18
+ UpsertContext = Data.define(:version, :recorded_at, :run_id, :operators)
19
+
20
+ include SubjectScans
15
21
  include VerdictCache
22
+ include Database
16
23
 
17
24
  # @param per_test_coverage [PerTestCoverage, nil] live per-test coverage
18
25
  # view used to record the full-map intersection set for survived
19
26
  # verdicts; without it survived rows stay NULL (never reusable).
20
27
  # @param reuse_context [ReuseContext, nil] fingerprint recorded with every
21
28
  # cached verdict; without it no verdict is cached (never reusable).
22
- def initialize(path:, per_test_coverage: nil, reuse_context: nil)
29
+ # @param provenance [ExecutionProvenance, nil] fingerprints are written
30
+ # only for verdicts whose inputs it reports intact (ADR-19); nil skips
31
+ # the check.
32
+ def initialize(path:, per_test_coverage: nil, reuse_context: nil, provenance: nil)
23
33
  @path = path
24
34
  @per_test_coverage = per_test_coverage
25
35
  @reuse_context = reuse_context
36
+ @provenance = provenance
26
37
  end
27
38
 
28
39
  attr_reader :path
29
40
 
30
- def record(result, version:, recorded_at: Time.now.utc)
31
- FileUtils.mkdir_p(File.dirname(path))
32
-
33
- with_database do |db|
34
- ensure_schema(db)
35
- db.transaction do
36
- insert_run(db, result, version, recorded_at) if full_run?(result)
37
- Array(result.mutants).each do |mutant|
38
- upsert_mutant(db, mutant, version, recorded_at)
39
- end
40
- end
41
+ # Timestamps are normalized to UTC on the way in: they are stored as
42
+ # ISO8601 strings and compared lexicographically (retirement compares a
43
+ # mutant's last_seen_at against its subject's scan), and callers pass
44
+ # local-zone times — `Runner#run` hands over `Time.now`. Mixing offsets
45
+ # makes "2026-01-01T13:00:00Z" < "2026-01-01T14:00:00+02:00" as strings
46
+ # while the opposite is true as instants. `getutc`, not `utc`, so the
47
+ # caller's Time object is not mutated.
48
+ # @param operators [Symbol, nil] the operator set this run generated from;
49
+ # stored per mutant so a later scan only retires mutants recorded under
50
+ # the same set.
51
+ # @param full_scan [Boolean] whether this run regenerated each touched
52
+ # subject's mutants in full. False for sampled runs, which produce a
53
+ # fraction of the set and must not advance the retirement baseline.
54
+ # @param run_id [String, nil] one history entry per mutant and run: a
55
+ # second write of the same run replaces the entry its checkpoint wrote.
56
+ # rubocop:disable Metrics/ParameterLists -- one keyword per recorded fact
57
+ def record(result, version:, recorded_at: Time.now, operators: nil, full_scan: false, run_id: nil)
58
+ # rubocop:enable Metrics/ParameterLists
59
+ recorded_at = recorded_at.getutc
60
+ write do |db|
61
+ insert_run(db, result, version, recorded_at) if full_run?(result)
62
+ record_subject_scans(db, result, recorded_at, operators) if complete_scan?(result, full_scan, operators)
63
+ upsert_mutants(db, result.mutants, UpsertContext.new(version:, recorded_at:, run_id:, operators:))
41
64
  end
42
65
  end
43
66
 
67
+ # Persists verdicts while a run is in progress (ADR-19): mutant rows
68
+ # only, never a run row. @return [Integer] how many mutants were written
69
+ def checkpoint(mutants, version:, run_id:, operators: nil, recorded_at: Time.now)
70
+ context = UpsertContext.new(version:, recorded_at: recorded_at.getutc, run_id:, operators:)
71
+ write { |db| upsert_mutants(db, mutants, context) }
72
+ Array(mutants).size
73
+ end
74
+
44
75
  # Verdict-cache lookup for `--incremental`: returns the stored hashes for
45
76
  # the mutant's LATEST verdict when it is Killed or Survived — the upsert
46
77
  # keeps exactly one row per stable id, so current_status is always the
@@ -75,7 +106,7 @@ module Henitai
75
106
 
76
107
  private
77
108
 
78
- attr_reader :per_test_coverage, :reuse_context
109
+ attr_reader :per_test_coverage, :reuse_context, :provenance
79
110
 
80
111
  def verdict_from_row(row)
81
112
  return nil unless row && VerdictCache::CACHEABLE_STATUSES.include?(row["current_status"])
@@ -97,40 +128,18 @@ module Henitai
97
128
  result.respond_to?(:partial_rerun?) && result.partial_rerun?
98
129
  end
99
130
 
100
- def with_database
101
- db = SQLite3::Database.new(path)
102
- db.results_as_hash = true
103
- yield db
104
- ensure
105
- db&.close
106
- end
107
-
108
- def ensure_schema(db)
109
- db.execute_batch(Sql::RUNS_TABLE)
110
- db.execute_batch(Sql::MUTANTS_TABLE)
111
- migrate_mutants_table(db)
112
- end
113
-
114
- # Additive, idempotent in-place migration: databases created before the
115
- # verdict-cache columns existed gain them on first contact, with existing
116
- # rows left intact (NULL hashes — valid but never reusable).
117
- def migrate_mutants_table(db)
118
- existing = db.execute("PRAGMA table_info(mutants)").map { |row| row["name"] }
119
- Sql::MIGRATION_COLUMNS.each do |column, type|
120
- next if existing.include?(column)
121
-
122
- db.execute("ALTER TABLE mutants ADD COLUMN #{column} #{type}")
123
- end
131
+ def upsert_mutants(db, mutants, context)
132
+ Array(mutants).each { |mutant| upsert_mutant(db, mutant, context) }
124
133
  end
125
134
 
126
135
  def insert_run(db, result, version, recorded_at)
127
136
  db.execute(Sql::INSERT_RUN, insert_run_bindings(result, version, recorded_at))
128
137
  end
129
138
 
130
- def upsert_mutant(db, mutant, version, recorded_at)
139
+ def upsert_mutant(db, mutant, context)
131
140
  db.execute(
132
141
  Sql::UPSERT_MUTANT,
133
- upsert_mutant_bindings(mutant_history_data(db, mutant, version, recorded_at))
142
+ upsert_mutant_bindings(mutant_history_data(db, mutant, context), context.operators)
134
143
  )
135
144
  end
136
145
 
@@ -147,18 +156,17 @@ module Henitai
147
156
  mutant.respond_to?(:stable_id) ? mutant.stable_id : MutantIdentity.stable_id(mutant)
148
157
  end
149
158
 
150
- def mutation_history_entry(mutant, version, recorded_at)
151
- {
152
- version: version,
153
- status: mutant.status.to_s,
154
- recordedAt: recorded_at.iso8601
155
- }
159
+ def mutation_history_entry(mutant, context)
160
+ entry = { version: context.version, status: mutant.status.to_s, recordedAt: context.recorded_at.iso8601 }
161
+ context.run_id ? entry.merge(runId: context.run_id) : entry
156
162
  end
157
163
 
158
- def mutant_history_data(db, mutant, version, recorded_at)
164
+ def mutant_history_data(db, mutant, context)
165
+ version = context.version
166
+ recorded_at = context.recorded_at
159
167
  mutant_id = stable_mutant_id(mutant)
160
168
  existing = existing_mutant_row(db, mutant_id)
161
- history = existing_status_history(existing) << mutation_history_entry(mutant, version, recorded_at)
169
+ history = append_history(existing_status_history(existing), mutation_history_entry(mutant, context))
162
170
  first_seen = first_seen_metadata(existing, version, recorded_at)
163
171
 
164
172
  {
@@ -168,6 +176,12 @@ module Henitai
168
176
  }
169
177
  end
170
178
 
179
+ # A later write of the same run replaces that run's entry.
180
+ def append_history(history, entry)
181
+ history.pop if entry[:runId] && history.last&.dig(:runId) == entry[:runId]
182
+ history << entry
183
+ end
184
+
171
185
  def existing_mutant_row(db, mutant_id)
172
186
  db.get_first_row(
173
187
  "SELECT * FROM mutants WHERE mutant_id = ?",
@@ -211,7 +225,7 @@ module Henitai
211
225
  end
212
226
 
213
227
  def load_mutants(db)
214
- db.execute("SELECT * FROM mutants ORDER BY first_seen_at, mutant_id").map do |row|
228
+ db.execute(SubjectScans::LOAD_MUTANTS).map do |row|
215
229
  {
216
230
  mutantId: row["mutant_id"],
217
231
  firstSeenVersion: row["first_seen_version"],
@@ -242,7 +256,7 @@ module Henitai
242
256
  ]
243
257
  end
244
258
 
245
- def upsert_mutant_bindings(data)
259
+ def upsert_mutant_bindings(data, operators)
246
260
  mutant = data.fetch(:mutant)
247
261
  [
248
262
  data.fetch(:mutant_id),
@@ -253,7 +267,9 @@ module Henitai
253
267
  mutant.status.to_s,
254
268
  JSON.generate(data.fetch(:history)),
255
269
  data.fetch(:days_alive),
256
- *verdict_cache_bindings(mutant, data[:existing_row])
270
+ *verdict_cache_bindings(mutant, data[:existing_row]),
271
+ mutant_subject_expression(mutant),
272
+ operators&.to_s
257
273
  ]
258
274
  end
259
275
  end
@@ -7,6 +7,8 @@ module Henitai
7
7
  # Operator names follow the Stryker-compatible naming convention so that the
8
8
  # JSON report is compatible with stryker-dashboard filters and HTML reports.
9
9
  #
10
+ # The sets nest: light ⊂ full ⊂ hard.
11
+ #
10
12
  # Built-in operators (light set):
11
13
  # ArithmeticOperator, EqualityOperator, LogicalOperator, BooleanLiteral,
12
14
  # ConditionalExpression, StringLiteral, ReturnValue
@@ -14,8 +16,10 @@ module Henitai
14
16
  # Additional operators (full set):
15
17
  # ArrayDeclaration, HashLiteral, RangeLiteral, SafeNavigation,
16
18
  # PatternMatch, BlockStatement, MethodExpression, AssignmentExpression,
17
- # UnaryOperator, UpdateOperator, RegexMutator, MethodChainUnwrap,
18
- # EqualityIdentityOperator
19
+ # UnaryOperator, UpdateOperator, RegexMutator, MethodChainUnwrap
20
+ #
21
+ # Usually-unkillable operators (hard set, ADR-12):
22
+ # EqualityIdentityOperator, HashKeyType
19
23
  #
20
24
  # Each operator subclass must implement:
21
25
  # - .node_types → Array<Symbol> AST node types this operator handles
@@ -10,8 +10,8 @@ module Henitai
10
10
  # This is the noisy half of the equality/identity pairing split out of
11
11
  # EqualityOperator: most Ruby objects don't observably distinguish `==`
12
12
  # from `eql?`/`equal?`, so these mutations are frequently unkillable by
13
- # ordinary tests. They stay available in the full operator set rather
14
- # than the default light set.
13
+ # ordinary tests. As of ADR-12 they live in the hard set — available on
14
+ # request, but out of both `light` and `full`.
15
15
  class EqualityIdentityOperator < Henitai::Operator
16
16
  NODE_TYPES = [:send].freeze
17
17
  RELATIONAL = %i[== != < > <= >= <=>].freeze
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../parser_current"
4
+
3
5
  module Henitai
4
6
  module Operators
5
7
  # Mutates symbol hash keys into string keys, one pair at a time
@@ -22,7 +24,7 @@ module Henitai
22
24
  subject:,
23
25
  original_node: node,
24
26
  mutated_node: mutated_hash(node, index),
25
- description: "replaced symbol key with string key"
27
+ description: "replaced symbol key #{key_label(pair, index)} with string key"
26
28
  )
27
29
  end
28
30
  end
@@ -33,6 +35,14 @@ module Henitai
33
35
  node.type == :pair && node.children.first&.type == :sym
34
36
  end
35
37
 
38
+ # Names the key so the mutants of one hash stay distinguishable. A
39
+ # symbol may contain a newline (`:"a\nb"`), which would break every
40
+ # single-line report surface, so such keys fall back to their position.
41
+ def key_label(pair, index)
42
+ label = pair.children.first.children.first.to_s
43
+ label.include?("\n") ? "##{index + 1}" : label
44
+ end
45
+
36
46
  def mutated_hash(node, index)
37
47
  pairs = node.children.each_with_index.map do |pair, pair_index|
38
48
  pair_index == index ? stringified_pair(pair) : pair
@@ -14,6 +14,14 @@ module Henitai
14
14
  # ones into a positional hash.
15
15
  NODE_TYPES = %i[hash kwargs].freeze
16
16
 
17
+ # Key node types whose first child is the literal value itself.
18
+ SCALAR_KEY_TYPES = %i[sym int float].freeze
19
+ # Same, but rendered quoted so a string key stays distinguishable from
20
+ # the symbol key of the same name.
21
+ QUOTED_KEY_TYPES = [:str].freeze
22
+ # Keyword literals carry no children; the node type is the label.
23
+ KEYWORD_KEY_TYPES = %i[true false nil].freeze
24
+
17
25
  def self.node_types
18
26
  NODE_TYPES
19
27
  end
@@ -48,7 +56,7 @@ module Henitai
48
56
  subject:,
49
57
  original_node: node,
50
58
  mutated_node: hash_without_pair(node, index),
51
- description: "removed hash pair #{pair_key_label(pair)}"
59
+ description: "removed hash pair #{pair_key_label(pair, index)}"
52
60
  )
53
61
  end
54
62
  end
@@ -58,10 +66,27 @@ module Henitai
58
66
  node.updated(nil, remaining)
59
67
  end
60
68
 
61
- # Pair keys are always AST nodes (sym/str/…); their first child is the
62
- # literal value used purely as a human-readable label.
63
- def pair_key_label(pair)
64
- pair.children.first.children.first
69
+ # Only literal keys have a meaningful name. Anything else — a variable,
70
+ # a method call, an array, a nested hash, an interpolated symbol — falls
71
+ # back to its position: rendering those nodes would put a raw
72
+ # s-expression (and, for a nested hash, embedded newlines) into every
73
+ # report surface and into MutantIdentity.stable_id.
74
+ def pair_key_label(pair, index)
75
+ label = literal_key_label(pair.children.first)
76
+ return "##{index + 1}" if label.nil? || label.include?("\n")
77
+
78
+ label
79
+ end
80
+
81
+ # A symbol can itself contain a newline (`:"a\nb"`), which would put the
82
+ # break straight back into every single-line surface — hence the
83
+ # newline check on the rendered label above, not just on the node type.
84
+ def literal_key_label(key)
85
+ case key.type
86
+ when *SCALAR_KEY_TYPES then key.children.first.to_s
87
+ when *QUOTED_KEY_TYPES then key.children.first.inspect
88
+ when *KEYWORD_KEY_TYPES then key.type.to_s
89
+ end
65
90
  end
66
91
  end
67
92
  end
@@ -16,9 +16,9 @@ module Henitai
16
16
  def mutate(node, subject:)
17
17
  case node.type
18
18
  when :str
19
- mutate_plain_string(node, subject:)
19
+ mutate_plain_string(node, node.children.first, subject:)
20
20
  when :dstr
21
- mutate_interpolated_string(node, subject:)
21
+ mutate_dstr(node, subject:)
22
22
  else
23
23
  []
24
24
  end
@@ -26,8 +26,21 @@ module Henitai
26
26
 
27
27
  private
28
28
 
29
- def mutate_plain_string(node, subject:)
30
- original_value = node.children.first
29
+ # Adjacent literals and heredocs without interpolation parse as a +dstr+
30
+ # of +str+ parts. Collapsing their "interpolation" would reproduce the
31
+ # original string -- an equivalent mutant -- so they are mutated like a
32
+ # plain string with the concatenated value.
33
+ def mutate_dstr(node, subject:)
34
+ return mutate_plain_string(node, static_string(node), subject:) if static?(node)
35
+
36
+ mutate_interpolated_string(node, subject:)
37
+ end
38
+
39
+ def static?(node)
40
+ node.children.all? { |child| child.type == :str }
41
+ end
42
+
43
+ def mutate_plain_string(node, original_value, subject:)
31
44
  REPLACEMENTS.reject { |r| r == original_value }.map do |replacement|
32
45
  build_mutant(
33
46
  subject:,
@@ -54,6 +54,12 @@ module Henitai
54
54
  @scheduler ? @scheduler.flaky_retry_count : 0
55
55
  end
56
56
 
57
+ # Statistics of the run's ControlGate (verified:, failed:, reclassified:),
58
+ # or nil when the run had no control runs.
59
+ def control_stats
60
+ @control_gate&.stats
61
+ end
62
+
57
63
  # True once a graceful shutdown has been requested; read by the scheduler.
58
64
  def shutdown_requested?
59
65
  @shutdown_requested
@@ -72,7 +78,8 @@ module Henitai
72
78
  # @param integration [Integration::Base]
73
79
  # @param config [Configuration]
74
80
  # @param progress_reporter [#progress, nil]
75
- # @param options [Hash]
81
+ # @param options [Hash] scheduler options; +control_run:+ ({ControlRun})
82
+ # puts a {ControlGate} in front of +progress_reporter+ (ADR-19).
76
83
  # @return [Array<ScenarioExecutionResult>]
77
84
  def run(mutants, integration, config, progress_reporter, options = {})
78
85
  Integration::SchedulerDiagnostics.reset! if Integration::SchedulerDiagnostics.enabled?
@@ -91,17 +98,30 @@ module Henitai
91
98
  @shutdown_requested = false
92
99
  @wakeup = Henitai::ProcessWakeup.new.install if @wakeup.nil?
93
100
  @wakeup.watch { @runtime.wakeup_ios } if @runtime.respond_to?(:wakeup_ios)
101
+ @control_gate = control_gate(progress_reporter, options[:control_run])
94
102
  @scheduler = SlotScheduler.new(
95
103
  integration: integration,
96
104
  config: config,
97
- progress_reporter: progress_reporter,
105
+ progress_reporter: @control_gate || progress_reporter,
98
106
  options: options,
99
107
  host: self,
100
- flaky_tests: @flaky_tests
108
+ flaky_tests: @flaky_tests,
109
+ control_listener: @control_gate
101
110
  )
102
111
  @scheduler.enqueue(mutants)
103
112
  end
104
113
 
114
+ # The gate's controls become scheduler jobs; the scheduler reports each
115
+ # control's outcome back to the gate.
116
+ def control_gate(progress_reporter, control_run)
117
+ return unless control_run
118
+
119
+ launcher = lambda do |key, mutant|
120
+ @scheduler.enqueue_control(key, control_run.control_mutant(mutant), control_run.test_files(mutant))
121
+ end
122
+ ControlGate.new(downstream: progress_reporter, control_run:, launcher:)
123
+ end
124
+
105
125
  def event_loop
106
126
  saved_traps = install_signal_traps
107
127
  @scheduler.fill_idle_slots
@@ -8,7 +8,8 @@ require_relative "verdict_fingerprint"
8
8
  module Henitai
9
9
  # Run-level fingerprint of everything outside a mutant and its covering
10
10
  # tests that can change a verdict: the test dependency files, the
11
- # verdict-relevant configuration and the henitai version. Recorded with every
11
+ # verdict-relevant configuration (including whether control runs verified
12
+ # the detections, ADR-19) and the henitai version. Recorded with every
12
13
  # cached Killed/Survived verdict and required to match for reuse.
13
14
  #
14
15
  # Computed once per instance, so the filter that reads verdicts and the
@@ -43,7 +44,8 @@ module Henitai
43
44
  "integration" => @config.integration.to_s,
44
45
  "operators" => @config.operators.to_s,
45
46
  "test_excludes" => Array(@config.test_excludes).map(&:to_s).sort,
46
- "coverage_criteria" => @config.coverage_criteria.to_h.sort_by { |key, _| key.to_s }
47
+ "coverage_criteria" => @config.coverage_criteria.to_h.sort_by { |key, _| key.to_s },
48
+ "control_runs" => @config.control_runs == true
47
49
  }
48
50
  end
49
51
  end
@@ -29,7 +29,9 @@ module Henitai
29
29
  end
30
30
 
31
31
  def example_group_started(notification)
32
- @open_group_files.push(RspecOutcomes.group_file(notification.group))
32
+ file = RspecOutcomes.group_file(notification.group)
33
+ @open_group_files.push(file)
34
+ @outcomes.started(file) if file
33
35
  end
34
36
 
35
37
  def example_group_finished(_notification)