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
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ module ForkServer
5
+ # The request loop of the fork server process. It boots once (the boot
6
+ # callable loads the integration environment), then forks one mutant
7
+ # process per `run` request through the launcher and relays how each one
8
+ # ended, because only this process can wait on them.
9
+ class Server
10
+ POLL_SECONDS = 0.5
11
+
12
+ # @param socket [IO] the server end of the connection to the parent
13
+ # @param boot [#call] loads the environment; raising reports `boot_failed`
14
+ # @param launcher [#call] request Hash -> pid of the forked mutant process
15
+ # @param reaper [#call] -> Array of [pid, Status] for ended children
16
+ # @param wake_reader [IO, nil] readable when a child ended (CHLD wakeup)
17
+ # @param killer [#call] pid -> kills that pid's process group
18
+ # rubocop:disable Metrics/ParameterLists
19
+ def initialize(socket:, boot:, launcher:, reaper: Reaper.method(:reap), wake_reader: nil,
20
+ killer: GroupKiller.method(:kill))
21
+ # rubocop:enable Metrics/ParameterLists
22
+ @socket = socket
23
+ @reader = Protocol::Reader.new(socket)
24
+ @boot = boot
25
+ @launcher = launcher
26
+ @reaper = reaper
27
+ @wake_reader = wake_reader
28
+ @killer = killer
29
+ @live = []
30
+ @shutdown = false
31
+ end
32
+
33
+ def serve
34
+ return unless announce_boot
35
+
36
+ until @shutdown
37
+ wait_for_activity
38
+ relay_exits
39
+ handle_requests
40
+ end
41
+ ensure
42
+ @live.each { |pid| @killer.call(pid) }
43
+ end
44
+
45
+ private
46
+
47
+ def announce_boot
48
+ @boot.call
49
+ send_event("type" => "ready")
50
+ true
51
+ rescue StandardError, ScriptError, SystemExit => e
52
+ send_event("type" => "boot_failed", "reason" => "#{e.class}: #{e.message}")
53
+ false
54
+ end
55
+
56
+ def wait_for_activity
57
+ ready = IO.select([@socket, @wake_reader].compact, nil, nil, POLL_SECONDS)
58
+ drain_wake_reader if @wake_reader && ready&.first&.include?(@wake_reader)
59
+ end
60
+
61
+ def drain_wake_reader
62
+ loop { @wake_reader.read_nonblock(4096) }
63
+ rescue IO::WaitReadable, EOFError
64
+ nil
65
+ end
66
+
67
+ def relay_exits
68
+ @reaper.call.each do |pid, status|
69
+ next unless @live.delete(pid)
70
+
71
+ send_event({ "type" => "exited", "pid" => pid }.merge(status.to_event))
72
+ end
73
+ end
74
+
75
+ def handle_requests
76
+ loop do
77
+ message = @reader.next_message(0)
78
+ return @shutdown = true if message.nil?
79
+ return unless message.is_a?(Hash)
80
+
81
+ dispatch(message)
82
+ end
83
+ end
84
+
85
+ def dispatch(message)
86
+ case message["type"]
87
+ when "run" then launch(message)
88
+ when "shutdown" then @shutdown = true
89
+ end
90
+ end
91
+
92
+ def launch(request)
93
+ pid = @launcher.call(request)
94
+ @live << pid
95
+ send_event("type" => "spawned", "request_id" => request["request_id"], "pid" => pid)
96
+ rescue StandardError => e
97
+ send_event("type" => "spawn_failed", "request_id" => request["request_id"],
98
+ "reason" => "#{e.class}: #{e.message}")
99
+ end
100
+
101
+ def send_event(event)
102
+ @socket.write(Protocol.encode(event))
103
+ rescue IOError, SystemCallError
104
+ @shutdown = true
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "socket"
4
+ require "fileutils"
5
+
6
+ module Henitai
7
+ module ForkServer
8
+ # Forks the fork server from the parent, after the mutants exist, and
9
+ # returns the parent's Client for it. In the server process: set up like
10
+ # any mutant child, send boot output to a log file, boot the integration
11
+ # environment once, then serve requests until told to stop.
12
+ class ServerProcess
13
+ def self.start(integration:, mutants:, test_files:, log_path:)
14
+ new(integration:, mutants:, test_files:, log_path:).start
15
+ end
16
+
17
+ def initialize(integration:, mutants:, test_files:, log_path:)
18
+ @integration = integration
19
+ @mutants = mutants.to_h { |mutant| [mutant.id, mutant] }
20
+ @test_files = test_files
21
+ @log_path = log_path
22
+ end
23
+
24
+ # @return [Client]
25
+ def start
26
+ FileUtils.mkdir_p(File.dirname(@log_path))
27
+ parent_socket, server_socket = UNIXSocket.pair
28
+ InheritedFdRegistry.register(parent_socket)
29
+ parent_pid = Process.pid
30
+ pid = Process.fork do
31
+ parent_socket.close
32
+ serve(server_socket, parent_pid)
33
+ end
34
+ server_socket.close
35
+ Client.new(socket: parent_socket, pid:)
36
+ end
37
+
38
+ private
39
+
40
+ def serve(socket, parent_pid)
41
+ Integration::ChildBootstrap.after_fork!(parent_pid:)
42
+ redirect_output
43
+ wake_reader, wake_writer = IO.pipe
44
+ [wake_reader, wake_writer].each { |io| InheritedFdRegistry.register(io) }
45
+ launcher = Launcher.new(integration: @integration, mutants: @mutants, socket:)
46
+ boot = -> { boot_environment(wake_writer) }
47
+ Server.new(socket:, boot:, launcher:, wake_reader:).serve
48
+ Process.exit!(0)
49
+ end
50
+
51
+ def boot_environment(wake_writer)
52
+ @integration.preload_environment(@test_files)
53
+ Signal.trap("CHLD") { wake(wake_writer) }
54
+ end
55
+
56
+ def wake(writer)
57
+ writer.write_nonblock(".")
58
+ rescue IO::WaitWritable, IOError
59
+ nil
60
+ end
61
+
62
+ def redirect_output
63
+ File.open(@log_path, "w") do |log|
64
+ $stdout.reopen(log)
65
+ $stderr.reopen(log)
66
+ end
67
+ $stdin.reopen(File::NULL)
68
+ $stdout.sync = true
69
+ $stderr.sync = true
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Henitai
4
+ module ForkServer
5
+ # Stands in for Process::Status of a mutant process that the parent cannot
6
+ # wait on itself: the fork server reaps it and relays how it ended.
7
+ # Answers the subset MutantVerdict and ScenarioExecutionResult read.
8
+ class Status
9
+ attr_reader :exitstatus, :termsig
10
+
11
+ def self.from_event(event)
12
+ new(exitstatus: event["exitstatus"], termsig: event["termsig"])
13
+ end
14
+
15
+ def self.from_process_status(status)
16
+ new(exitstatus: status.exitstatus, termsig: status.termsig)
17
+ end
18
+
19
+ # Stands in for a process whose end the lost server never relayed. It
20
+ # says nothing about the mutant, so it must never become a verdict.
21
+ def self.lost
22
+ new(exitstatus: nil, termsig: 9, lost: true)
23
+ end
24
+
25
+ def initialize(exitstatus:, termsig:, lost: false)
26
+ @exitstatus = exitstatus
27
+ @termsig = termsig
28
+ @lost = lost
29
+ end
30
+
31
+ def lost?
32
+ @lost
33
+ end
34
+
35
+ def exited?
36
+ !signaled?
37
+ end
38
+
39
+ def signaled?
40
+ !termsig.nil?
41
+ end
42
+
43
+ def success?
44
+ !signaled? && exitstatus.to_i.zero?
45
+ end
46
+
47
+ def to_event
48
+ { "exitstatus" => exitstatus, "termsig" => termsig }
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "fork_server/protocol"
4
+ require_relative "fork_server/status"
5
+ require_relative "fork_server/reaper"
6
+ require_relative "fork_server/group_killer"
7
+ require_relative "fork_server/server"
8
+ require_relative "fork_server/launcher"
9
+ require_relative "fork_server/client"
10
+ require_relative "fork_server/runtime"
11
+ require_relative "fork_server/server_process"
12
+ require_relative "fork_server/mutant_runner"
13
+
14
+ module Henitai
15
+ # Opt-in execution model (ADR-15): the test environment boots once in a
16
+ # long-lived server process and every mutant process is forked from it.
17
+ module ForkServer
18
+ BOOT_TIMEOUT = 300.0
19
+
20
+ # Starts a server for the run and returns a runner that sends mutants to
21
+ # it, or nil, after a warning, when the server cannot be booted. The
22
+ # caller then keeps the per-mutant boot.
23
+ #
24
+ # @return [MutantRunner, nil]
25
+ # rubocop:disable Metrics/ParameterLists
26
+ def self.start(integration:, mutants:, test_files:, log_path:, warner: nil, boot_timeout: BOOT_TIMEOUT,
27
+ fallback_timeout: nil)
28
+ warner ||= ->(message) { Kernel.warn(message) }
29
+ client = ServerProcess.start(integration:, mutants:, test_files:, log_path:)
30
+ begin
31
+ client.wait_until_ready(boot_timeout)
32
+ rescue Client::BootFailed, Client::ServerLost => e
33
+ client.shutdown
34
+ warner.call("henitai: the fork server failed to boot (#{e.message}); see #{log_path}; " \
35
+ "falling back to per-mutant boot")
36
+ return nil
37
+ end
38
+ MutantRunner.new(integration:, client:, warner:, fallback_timeout:)
39
+ end
40
+ # rubocop:enable Metrics/ParameterLists
41
+ end
42
+ end
@@ -18,7 +18,10 @@ module Henitai
18
18
  mutation-history.sqlite3
19
19
  mutation-history.json
20
20
  henitai_per_test.json
21
+ henitai_suite_boot.json
21
22
  henitai_dependency_manifest.json
23
+ henitai_flaky_tests.json
24
+ henitai_suite_outcomes.json
22
25
  .henitai-run.lock
23
26
  mutation-logs
24
27
  mutation-coverage
@@ -9,14 +9,23 @@ module Henitai
9
9
  # Shells out to git to discover changed files between two refs.
10
10
  #
11
11
  # By default the analyzer runs in the current working directory. Callers can
12
- # pass dir: to point it at another repository root without changing cwd.
12
+ # pass dir: to point it at another directory without changing cwd.
13
+ #
14
+ # Every path is reported relative to that directory, which need not be the
15
+ # repository root: `git diff` would otherwise print root-relative paths
16
+ # while `git ls-files` prints directory-relative ones. Paths are read
17
+ # NUL-separated so git never quotes non-ASCII names.
13
18
  class GitDiffAnalyzer
14
- def changed_files(from:, to:, dir: Dir.pwd)
15
- stdout, stderr, status = git_diff(dir, "--name-only", from, to)
19
+ NAME_ONLY = ["--name-only", "--relative", "-z"].freeze
16
20
 
17
- raise GitDiffError, stderr.strip unless status.success?
21
+ def changed_files(from:, to:, dir: Dir.pwd)
22
+ diff_paths(dir, from, to)
23
+ end
18
24
 
19
- stdout.split("\n").reject(&:empty?)
25
+ # Changes on HEAD since its merge base with ref. Commits that landed on
26
+ # ref after the fork point are not part of this diff.
27
+ def changed_files_since(ref, dir: Dir.pwd)
28
+ diff_paths(dir, "#{ref}...HEAD")
20
29
  end
21
30
 
22
31
  def working_tree_changed_files(dir: Dir.pwd)
@@ -55,7 +64,7 @@ module Henitai
55
64
  end
56
65
 
57
66
  def changed_line_ranges(path, from:, to:, dir:)
58
- stdout, stderr, status = git_diff(dir, "--unified=0", from, to, "--", path)
67
+ stdout, stderr, status = git_diff(dir, "--unified=0", "--relative", from, to, "--", path)
59
68
 
60
69
  raise GitDiffError, stderr.strip unless status.success?
61
70
 
@@ -94,23 +103,31 @@ module Henitai
94
103
  Open3.capture3(*command)
95
104
  end
96
105
 
97
- def working_tree_tracked_files(dir)
98
- stdout, stderr, status = git_diff(dir, "--name-only", "HEAD")
106
+ def diff_paths(dir, *revisions)
107
+ stdout, stderr, status = git_diff(dir, *NAME_ONLY, *revisions)
99
108
 
100
109
  raise GitDiffError, stderr.strip unless status.success?
101
110
 
102
- stdout.split("\n").reject(&:empty?)
111
+ paths(stdout)
112
+ end
113
+
114
+ def working_tree_tracked_files(dir)
115
+ diff_paths(dir, "HEAD")
103
116
  end
104
117
 
105
118
  def untracked_files(dir)
106
119
  command = ["git"]
107
120
  command += ["-C", dir] if dir
108
- command += ["ls-files", "--others", "--exclude-standard"]
121
+ command += ["ls-files", "--others", "--exclude-standard", "-z"]
109
122
  stdout, stderr, status = Open3.capture3(*command)
110
123
 
111
124
  raise GitDiffError, stderr.strip unless status.success?
112
125
 
113
- stdout.split("\n").reject(&:empty?)
126
+ paths(stdout)
127
+ end
128
+
129
+ def paths(stdout)
130
+ stdout.dup.force_encoding(Encoding::UTF_8).split("\0").reject(&:empty?)
114
131
  end
115
132
  end
116
133
  end
@@ -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
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "signature_line"
3
4
  require_relative "verdict_fingerprint"
4
5
 
5
6
  module Henitai
@@ -8,23 +9,26 @@ module Henitai
8
9
  #
9
10
  # Reuse rules (conservative — any doubt resolves to re-execution):
10
11
  #
12
+ # * Both verdicts: the stored {ReuseContext} fingerprint (test
13
+ # dependencies, verdict-relevant configuration, henitai version) must
14
+ # equal the current one. Rows recorded without a context are never
15
+ # reused.
11
16
  # * Killed: the stored verdict must carry both fingerprints, and both the
12
17
  # subject's source and every recorded covering test file must be
13
18
  # byte-identical to what was recorded. Killed is monotone, so no live
14
19
  # coverage is needed.
15
20
  # * Survived: additionally the LIVE covering set (the full-map
16
21
  # intersection computed via {PerTestCoverage}) must equal the recorded
17
- # set in membership, and the run-level dependency fingerprint must be
18
- # unchanged — a new or edited test reaching the mutant always forces a
19
- # re-run (ADR-11). Without a live per-test map or dependency
20
- # fingerprint, survivors always re-execute.
22
+ # set in membership — a new or edited test reaching the mutant always
23
+ # forces a re-run (ADR-11). Without a live per-test map or a context,
24
+ # survivors always re-execute.
21
25
  #
22
26
  # Timeouts, errors, unknown ids and legacy rows always re-execute.
23
27
  class IncrementalFilter
24
- def initialize(history_store:, per_test_coverage: nil, dependency_fingerprint: nil)
28
+ def initialize(history_store:, per_test_coverage: nil, reuse_context: nil)
25
29
  @history_store = history_store
26
30
  @per_test_coverage = per_test_coverage
27
- @dependency_fingerprint = dependency_fingerprint
31
+ @reuse_context = reuse_context
28
32
  end
29
33
 
30
34
  # @return [Array<Mutant>] the same collection; cache hits get the stored
@@ -48,7 +52,7 @@ module Henitai
48
52
 
49
53
  private
50
54
 
51
- attr_reader :history_store, :per_test_coverage, :dependency_fingerprint
55
+ attr_reader :history_store, :per_test_coverage, :reuse_context
52
56
 
53
57
  # MutantIdentity deliberately omits source coordinates (line-drift
54
58
  # tolerance), so distinct mutants inside one subject can share a stable
@@ -75,7 +79,9 @@ module Henitai
75
79
 
76
80
  def killed_reusable?(mutant, verdict)
77
81
  subject_source_unchanged?(mutant, verdict) &&
78
- VerdictFingerprint.tests_fingerprint_current?(verdict.fetch(:covered_tests_fingerprint))
82
+ VerdictFingerprint.tests_fingerprint_current?(
83
+ verdict.fetch(:covered_tests_fingerprint), context_sha: context_sha
84
+ )
79
85
  end
80
86
 
81
87
  # The filter checks per-test map availability itself instead of trusting
@@ -84,17 +90,21 @@ module Henitai
84
90
  # unknowable means re-execute.
85
91
  def survived_reusable?(mutant, verdict)
86
92
  return false unless per_test_coverage&.available?
93
+ return false if SignatureLine.on?(mutant)
87
94
  return false unless subject_source_unchanged?(mutant, verdict)
88
95
 
89
96
  VerdictFingerprint.survivor_fingerprint_current?(
90
97
  verdict.fetch(:covered_tests_fingerprint),
91
98
  live_paths: per_test_coverage.tests_covering(mutant),
92
- dependency_sha: dependency_fingerprint
99
+ context_sha: context_sha
93
100
  )
94
101
  end
95
102
 
103
+ def context_sha = reuse_context&.fingerprint
104
+
96
105
  def subject_source_unchanged?(mutant, verdict)
97
- VerdictFingerprint.subject_source_hash(mutant) == verdict.fetch(:subject_source_hash)
106
+ current = VerdictFingerprint.subject_source_hash(mutant)
107
+ !current.nil? && current == verdict.fetch(:subject_source_hash)
98
108
  end
99
109
  end
100
110
  end
@@ -8,13 +8,17 @@ 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
16
17
  include ChildRuntimeControl
17
18
 
19
+ EXITED_GROUP_POLL_INTERVAL = 0.05
20
+ EXITED_GROUP_POLL_COUNT = 40
21
+
18
22
  # Child-run diagnostics. Public so the child-side modules mixed into
19
23
  # concrete adapters can reach it without `send`.
20
24
  def child_debug_log
@@ -32,19 +36,10 @@ module Henitai
32
36
  raise NotImplementedError
33
37
  end
34
38
 
35
- # Run test files in a child process with the mutant active.
36
- #
37
- # @param mutant [Mutant]
38
- # @param test_files [Array<String>]
39
- # @param timeout [Float] seconds
40
- # @return [ScenarioExecutionResult]
41
- def run_mutant(mutant:, test_files:, timeout:)
42
- raise NotImplementedError
43
- end
44
-
45
39
  # Fork a child process for the mutant without waiting for it to finish.
46
- # Returns a ChildHandle carrying the OS pid and log file paths.
47
- # 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.
48
43
  #
49
44
  # @param mutant [Mutant]
50
45
  # @param test_files [Array<String>]
@@ -57,6 +52,20 @@ module Henitai
57
52
  false
58
53
  end
59
54
 
55
+ # True when the integration can boot once in a fork server (ADR-15) and
56
+ # run mutants from the booted state.
57
+ def fork_server_supported?
58
+ false
59
+ end
60
+
61
+ # Loads the test environment and the given test files without running
62
+ # anything; only called inside the fork server process.
63
+ #
64
+ # @param test_files [Array<String>]
65
+ def preload_environment(test_files)
66
+ raise NotImplementedError
67
+ end
68
+
60
69
  def wait_with_timeout(pid, timeout)
61
70
  wakeup = Henitai.const_get(:ProcessWakeup).new.install
62
71
  return Process.last_status if wait_nonblocking(pid)
@@ -78,6 +87,29 @@ module Henitai
78
87
  end
79
88
 
80
89
  def cleanup_process_group(pid)
90
+ if leader_exited?(pid)
91
+ terminate_exited_group(pid)
92
+ else
93
+ terminate_running_group(pid)
94
+ end
95
+ rescue Errno::EPERM
96
+ cleanup_child_process(pid)
97
+ rescue Errno::ESRCH
98
+ nil
99
+ end
100
+
101
+ private
102
+
103
+ # A leader that already exited normally cannot be waited on again, so
104
+ # the group is polled for members instead of burning the grace period.
105
+ def terminate_exited_group(pid)
106
+ Process.kill(:SIGTERM, -pid)
107
+ return unless group_members_remain?(pid)
108
+
109
+ Process.kill(:SIGKILL, -pid)
110
+ end
111
+
112
+ def terminate_running_group(pid)
81
113
  grace_period = 2.0
82
114
  wakeup = Henitai.const_get(:ProcessWakeup).new.install
83
115
  Process.kill(:SIGTERM, -pid)
@@ -88,15 +120,34 @@ module Henitai
88
120
  return if wait_nonblocking(pid)
89
121
 
90
122
  Process.kill(:SIGKILL, -pid)
91
- rescue Errno::EPERM
92
- cleanup_child_process(pid)
93
- rescue Errno::ESRCH
94
- nil
95
123
  ensure
96
124
  wakeup&.close
97
125
  end
98
126
 
99
- private
127
+ def leader_exited?(pid)
128
+ !Process.wait(pid, Process::WNOHANG).nil?
129
+ rescue Errno::ECHILD, Errno::ESRCH
130
+ true
131
+ end
132
+
133
+ def group_members_remain?(pid)
134
+ EXITED_GROUP_POLL_COUNT.times do
135
+ return false unless group_alive?(pid)
136
+
137
+ pause(EXITED_GROUP_POLL_INTERVAL)
138
+ end
139
+ group_alive?(pid)
140
+ end
141
+
142
+ # Signal 0 probes existence only: ESRCH means the group is empty.
143
+ def group_alive?(pid)
144
+ Process.kill(0, -pid)
145
+ true
146
+ rescue Errno::ESRCH
147
+ false
148
+ rescue Errno::EPERM
149
+ true
150
+ end
100
151
 
101
152
  def pause(seconds)
102
153
  sleep(seconds)
@@ -133,6 +184,10 @@ module Henitai
133
184
  { "PARALLEL_WORKERS" => "1" }
134
185
  end
135
186
 
187
+ def suite_process_env
188
+ subprocess_env.merge(SuiteBootClock::STARTED_AT_ENV => Time.now.to_f.to_s)
189
+ end
190
+
136
191
  def wait_nonblocking(pid)
137
192
  Process.wait(pid, Process::WNOHANG)
138
193
  rescue Errno::ECHILD, Errno::ESRCH
@@ -143,6 +198,10 @@ module Henitai
143
198
  @scenario_log_support ||= ScenarioLogSupport.new
144
199
  end
145
200
 
201
+ def child_report_store
202
+ @child_report_store ||= ChildReportStore.new
203
+ end
204
+
146
205
  def with_subprocess_env
147
206
  original_env = {} # : Hash[String, String?]
148
207
  subprocess_env.each do |key, value|