mutineer 1.8.1 → 1.9.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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +733 -0
  3. data/README.md +85 -50
  4. data/lib/mutineer/changed_lines.rb +2 -1
  5. data/lib/mutineer/child_cause.rb +149 -0
  6. data/lib/mutineer/child_stdout.rb +12 -1
  7. data/lib/mutineer/cli.rb +540 -59
  8. data/lib/mutineer/command_redact.rb +38 -0
  9. data/lib/mutineer/concern_blocks.rb +95 -24
  10. data/lib/mutineer/config.rb +112 -6
  11. data/lib/mutineer/coverage_map.rb +1345 -156
  12. data/lib/mutineer/daemon_backend.rb +41 -18
  13. data/lib/mutineer/daemon_client.rb +104 -21
  14. data/lib/mutineer/daemon_server.rb +85 -14
  15. data/lib/mutineer/database_urls.rb +189 -0
  16. data/lib/mutineer/external_backend.rb +93 -6
  17. data/lib/mutineer/fs_path.rb +125 -0
  18. data/lib/mutineer/isolation.rb +380 -25
  19. data/lib/mutineer/job_plan.rb +352 -41
  20. data/lib/mutineer/minitest_integration/file_order.rb +56 -0
  21. data/lib/mutineer/minitest_integration/in_test_database_error.rb +243 -19
  22. data/lib/mutineer/minitest_integration.rb +26 -58
  23. data/lib/mutineer/mutant_id.rb +4 -1
  24. data/lib/mutineer/mutation.rb +10 -4
  25. data/lib/mutineer/mutator_registry.rb +6 -3
  26. data/lib/mutineer/mutators/collection_method.rb +195 -6
  27. data/lib/mutineer/mutators/literal_mutation.rb +33 -3
  28. data/lib/mutineer/mutators/range_literal.rb +78 -29
  29. data/lib/mutineer/mutators/rescue_body.rb +126 -0
  30. data/lib/mutineer/mutators/return_nil.rb +23 -6
  31. data/lib/mutineer/output_filter.rb +451 -0
  32. data/lib/mutineer/pairing.rb +463 -31
  33. data/lib/mutineer/parser.rb +53 -0
  34. data/lib/mutineer/project_path.rb +15 -8
  35. data/lib/mutineer/rails_worker_db.rb +366 -6
  36. data/lib/mutineer/reach_probe.rb +229 -0
  37. data/lib/mutineer/redaction.rb +301 -41
  38. data/lib/mutineer/reload_guards.rb +985 -0
  39. data/lib/mutineer/reporter.rb +467 -58
  40. data/lib/mutineer/result.rb +6 -7
  41. data/lib/mutineer/run_seed.rb +16 -0
  42. data/lib/mutineer/runner.rb +194 -55
  43. data/lib/mutineer/source_bytes.rb +187 -0
  44. data/lib/mutineer/statement_lines.rb +108 -14
  45. data/lib/mutineer/test_runners/rspec.rb +118 -10
  46. data/lib/mutineer/test_runners/rspec_seed.rb +151 -0
  47. data/lib/mutineer/test_runners.rb +3 -2
  48. data/lib/mutineer/triage.rb +55 -10
  49. data/lib/mutineer/version.rb +1 -1
  50. data/lib/mutineer.rb +1 -0
  51. data/skills/mutineer/SKILL.md +12 -4
  52. metadata +13 -1
@@ -7,6 +7,7 @@ require_relative "daemon_client"
7
7
  require_relative "progress"
8
8
  require_relative "job_plan"
9
9
  require_relative "orphan_guard"
10
+ require_relative "reach_probe"
10
11
 
11
12
  module Mutineer
12
13
  # Daemon execution backend. Boots the app ONCE in a persistent subprocess under
@@ -53,7 +54,8 @@ module Mutineer
53
54
  # source map, and the {JobPlan.collect_jobs} extras.
54
55
  def self.execute(config, operator_classes)
55
56
  jobs, ignored_results, source_map, extras = JobPlan.collect_jobs(config, operator_classes)
56
- jobs, ignored_results, extras[:unscoped_mutants] = JobPlan.scope_since(jobs, ignored_results, source_map, config)
57
+ jobs, ignored_results, extras[:unscoped_mutants] =
58
+ JobPlan.scope_since(jobs, ignored_results, source_map, config, extras: extras)
57
59
  abs_tests = config.tests.map { |t| File.expand_path(t, config.project_root) }
58
60
 
59
61
  # Nothing to mutate (`--since` matched no changed line, or every mutant is
@@ -251,7 +253,9 @@ module Mutineer
251
253
  map: data["map"] || {},
252
254
  failed_test_files: data["failed_test_files"] || [],
253
255
  project_root: config.project_root,
254
- failed_clean_tests: data["failed_clean_tests"]
256
+ failed_clean_tests: data["failed_clean_tests"],
257
+ failed_clean_details: data["failed_clean_details"] || {},
258
+ clean_timeouts: data["clean_timeouts"] || {}
255
259
  ), framework: config.framework)
256
260
  end
257
261
 
@@ -425,7 +429,7 @@ module Mutineer
425
429
  # (fallback, not narrowed).
426
430
  sel = coverage_map && !coverage_map.map.empty? && JobPlan.coverage_selection(subject.file, mutation, subject, source, coverage_map)
427
431
  r =
428
- if Parser.parse_string(mutated).errors.any?
432
+ if Parser.invalid?(mutated)
429
433
  Result.skipped
430
434
  elsif sel && sel[0] == :verdict
431
435
  sel[1]
@@ -433,20 +437,38 @@ module Mutineer
433
437
  # A :maybe mutant that cannot be placed stays no_coverage, as in-process.
434
438
  sel && sel[0] == :maybe ? Result.no_coverage : Result.unplaceable
435
439
  else
436
- payload = { "code" => mutated, "source_file" => File.expand_path(subject.file, config.project_root) }
437
- if subject.reload_hook_call
438
- # #297: the daemon child places the hook-block def, as in-process (Runner.run).
439
- payload["code"], hook_def = JobPlan.hook_mutant(mutated, mutation, subject, source)
440
- payload["hook_def"] = hook_def.to_h { |key, value| [key.to_s, key == :name ? value.to_s : value] }
440
+ # Same probe as in-process (#345): the daemon child loads this text.
441
+ sentinel = sel && sel[0] == :maybe ? ReachProbe.sentinel : nil
442
+ reach_key = sentinel && ReachProbe.reach_key
443
+ begin
444
+ payload = { "code" => mutated, "source_file" => File.expand_path(subject.file, config.project_root) }
445
+ if subject.reload_hook_call
446
+ # #297: the daemon child places the hook-block def, as in-process (Runner.run).
447
+ payload["code"], hook_def = JobPlan.hook_mutant(mutated, mutation, subject, source,
448
+ reach_path: sentinel&.path, reach_key: reach_key)
449
+ payload["hook_def"] = hook_def.to_h { |key, value| [key.to_s, key == :name ? value.to_s : value] }
450
+ elsif sentinel
451
+ payload["code"] = ReachProbe.wrap_mutation(mutated, mutation, sentinel.path, reach_key)
452
+ end
453
+ # The daemon clears these after load and before tests (#345).
454
+ if sentinel
455
+ payload["reach_path"] = sentinel.path
456
+ payload["reach_key"] = reach_key
457
+ end
458
+ verdict = client.request(
459
+ id: req_id, worker: worker, timeout: config.daemon_timeout || DEFAULT_TIMEOUT,
460
+ payload: payload, tests: sel ? sel[1] : abs_tests
461
+ )
462
+ # A survivor whose line ran at load, as in-process (Runner.run).
463
+ details = client.last_cause if verdict == "error"
464
+ loaded = JobPlan.load_verdict(result_for(verdict, details), subject.file, mutation, subject, source,
465
+ coverage_map)
466
+ # A :maybe survivor the probe saw run counts; one it did not stays
467
+ # no_coverage, as in-process (#254, #345).
468
+ JobPlan.selection_verdict(sel && sel[0], loaded, reached: ReachProbe.reached?(sentinel))
469
+ ensure
470
+ ReachProbe.close_sentinel(sentinel)
441
471
  end
442
- verdict = client.request(
443
- id: req_id, worker: worker, timeout: config.daemon_timeout || DEFAULT_TIMEOUT,
444
- payload: payload, tests: sel ? sel[1] : abs_tests
445
- )
446
- # A survivor whose line ran at load, as in-process (Runner.run).
447
- loaded = JobPlan.load_verdict(result_for(verdict), subject.file, mutation, subject, source, coverage_map)
448
- # Tests that may not have run the mutant, as in-process (#254).
449
- JobPlan.selection_verdict(sel && sel[0], loaded)
450
472
  end
451
473
  r.with(subject: subject, mutation: mutation, id: id)
452
474
  end
@@ -499,14 +521,15 @@ module Mutineer
499
521
  # resolved tool-side before a request is ever sent.
500
522
  #
501
523
  # @param verdict [String] the daemon's verdict word.
524
+ # @param details [String, nil] the cause the daemon sent with an `"error"`.
502
525
  # @api private
503
526
  # @return [Mutineer::Result] the matching result.
504
- def self.result_for(verdict)
527
+ def self.result_for(verdict, details = nil)
505
528
  case verdict
506
529
  when "survived" then Result.survived
507
530
  when "killed" then Result.killed
508
531
  when "timeout" then Result.timeout
509
- else Result.error("daemon verdict: #{verdict}")
532
+ else Result.error(details || "daemon verdict: #{verdict}")
510
533
  end
511
534
  end
512
535
 
@@ -44,9 +44,13 @@ module Mutineer
44
44
  # Seconds the daemon gets to boot the app and answer the handshake. A boot
45
45
  # that never returns ends the run instead of hanging it (#101).
46
46
  BOOT_TIMEOUT = 600
47
- # Seconds close_io waits for the stderr drain to reach EOF before it stops
48
- # the thread, so a last line without a newline is not lost.
47
+ # Most seconds close_io waits for the stderr drain to finish. The drain
48
+ # ends at EOF, or once the daemon has exited and the pipe is empty, so
49
+ # only a grandchild that keeps writing makes close_io wait this long.
49
50
  DRAIN_GRACE = 1
51
+ # Seconds the stderr drain waits for output before it checks whether the
52
+ # daemon has exited. A grandchild that holds the pipe open never sends EOF.
53
+ DRAIN_POLL = 0.1
50
54
  # Bundler's marker for a variable that was unset before it activated.
51
55
  BUNDLER_UNSET = "BUNDLER_ENVIRONMENT_PRESERVER_INTENTIONALLY_NIL"
52
56
 
@@ -83,6 +87,17 @@ module Mutineer
83
87
  # @return [Hash, nil]
84
88
  attr_reader :databases
85
89
 
90
+ # Why the last {#request} answered `"error"`: the redacted `Class: message`
91
+ # line from the daemon's child (#388), or `daemon crashed` / `daemon
92
+ # stopped answering` when no reply came. nil when the reply carried no
93
+ # cause, or the last request answered anything else. Read it right after
94
+ # {#request}: a client serves one worker thread at a time
95
+ # ({DaemonBackend} gives each thread its own), so no other request can
96
+ # replace it in between.
97
+ #
98
+ # @return [String, nil]
99
+ attr_reader :last_cause
100
+
86
101
  # Ask the daemon to create `slots` worker databases. Raises when the copy
87
102
  # fails or another run holds the database, so the caller stops before any
88
103
  # mutant is scored.
@@ -111,8 +126,12 @@ module Mutineer
111
126
  # @param timeout [Numeric] per-mutant wall-clock timeout (seconds).
112
127
  # @param worker [Integer] worker slot; the daemon routes the fork to
113
128
  # `<db>-<worker>` for isolation. Defaults to 0 (serial).
114
- # @return [String] one of survived/killed/error/timeout.
129
+ # @return [String] one of survived/killed/error/timeout. An `"error"`
130
+ # reply's cause is in {#last_cause}.
115
131
  def request(id:, payload:, tests:, timeout:, worker: 0)
132
+ @last_cause = nil
133
+ # A write that fails reads no reply, so a stale flag must not name it a hang.
134
+ @timed_out = false
116
135
  # close_io nils the pipes, so a client whose respawn never completed would
117
136
  # otherwise fail per-mutant forever (NoMethodError on nil) and let the backend
118
137
  # score every remaining mutant against nothing. Deadness is a property of the
@@ -130,8 +149,12 @@ module Mutineer
130
149
  rescue Errno::EPIPE, IOError
131
150
  nil
132
151
  end
133
- return reply["verdict"] if reply && reply["id"] == id
152
+ if reply && reply["id"] == id
153
+ @last_cause = reply["details"] if reply["verdict"] == "error"
154
+ return reply["verdict"]
155
+ end
134
156
 
157
+ @last_cause = @timed_out ? "daemon stopped answering" : "daemon crashed"
135
158
  restart!
136
159
  "error"
137
160
  end
@@ -349,10 +372,13 @@ module Mutineer
349
372
  "-r", DAEMON_PATH, "-e", "Mutineer::DaemonServer.run",
350
373
  unsetenv_others: true, chdir: @app_root
351
374
  )
352
- # Replies are UTF-8 JSON, and the app's stderr is redacted as text: with
353
- # LANG unset both pipes would read as US-ASCII (#303).
375
+ # Requests and replies are UTF-8 JSON, so both pipes are tagged
376
+ # UTF-8 and do not depend on LANG (#303, #346). `JSON.generate`
377
+ # already returns UTF-8; the stdin tag keeps a later write of a
378
+ # UTF-8 string from being converted. The stderr drain reads bytes
379
+ # and marks each line UTF-8 itself (write_redacted).
380
+ @stdin.set_encoding(Encoding::UTF_8)
354
381
  @stdout.set_encoding(Encoding::UTF_8)
355
- @stderr.set_encoding(Encoding::UTF_8)
356
382
  # Drain daemon stderr to the tool's stderr so child/boot errors are visible,
357
383
  # redacted line by line (#288: the app's output goes through here too).
358
384
  # Tracked (not fire-and-forget) so close_io can reclaim it on quit/respawn.
@@ -419,29 +445,86 @@ module Mutineer
419
445
  # A line is the unit the daemon and the app write, so a password is never
420
446
  # split across two redaction calls. The line break is kept outside the
421
447
  # redaction, so a line redaction hides as a whole still ends its line.
422
- # After a write fails (EPIPE from a tool stderr that went away), the drain
423
- # keeps reading and drops the lines, so the daemon never blocks on a full
424
- # pipe. The outer rescue swallows the benign EBADF/IOError raised when
425
- # close_io closes the pipe under an in-flight read, so the thread never
426
- # ends with an error that close_io's join would re-raise.
448
+ # The drain ends at EOF, or once the daemon has exited and the pipe is
449
+ # empty: a grandchild that keeps the pipe open would otherwise hold it
450
+ # until close_io's DRAIN_GRACE (#341). Either way it writes a last line
451
+ # that has no newline. After a write fails (EPIPE from a tool stderr that
452
+ # went away), the drain keeps reading and drops the lines, so the daemon
453
+ # never blocks on a full pipe. The outer rescue swallows the benign
454
+ # EBADF/IOError raised when close_io closes the pipe under an in-flight
455
+ # read, so the thread never ends with an error that close_io's join would
456
+ # re-raise.
427
457
  #
428
458
  # @param io [IO] the daemon's stderr pipe.
429
459
  # @return [nil]
430
460
  def drain_stderr(io)
431
461
  writable = true
432
- io.each_line do |line|
433
- next unless writable
462
+ pending = "".b
463
+ loop do
464
+ # Read the exit before the pipe: everything the daemon wrote is in the
465
+ # pipe by the time it has exited, so an empty read after it is the end.
466
+ exited = @wait_thr && !@wait_thr.alive?
467
+ chunk = io.read_nonblock(4096, exception: false)
468
+ break if chunk.nil?
469
+
470
+ if chunk == :wait_readable
471
+ # After the exit, one more quiet poll before stopping, so a mutant
472
+ # child's last line, written just after the daemon died, still shows.
473
+ ready = io.wait_readable(DRAIN_POLL)
474
+ break if exited && !ready
475
+
476
+ next
477
+ end
478
+ pending << chunk.b
479
+ # Nothing new can be emitted before a newline, so a long line is not
480
+ # rescanned on every read.
481
+ next unless chunk.include?("\n")
434
482
 
435
- body = line.chomp("\n").scrub # an invalid byte must not stop the drain
436
- @errio.write(Redaction.text(body), body.length < line.length ? "\n" : "")
437
- rescue StandardError
438
- writable = false
483
+ ready, pending = Redaction.take_redactable(pending, flush: false)
484
+ writable &&= write_redacted(ready) if writable && !ready.empty?
485
+ end
486
+ if writable && !pending.empty?
487
+ ready, = Redaction.take_redactable(pending, flush: true)
488
+ write_redacted(ready) unless ready.empty?
439
489
  end
440
490
  nil
441
491
  rescue StandardError
442
492
  nil
443
493
  end
444
494
 
495
+ # Write daemon stderr to the tool's stderr, redacted. One line at a time,
496
+ # unless a password form itself contains a newline: then the chunk is one
497
+ # redaction so the form is not split. A failed redaction still keeps each
498
+ # ordinary line's break.
499
+ #
500
+ # @param chunk [String] raw bytes, with or without a trailing newline.
501
+ # @return [Boolean] false when the write failed.
502
+ def write_redacted(chunk)
503
+ return true if chunk.nil? || chunk.empty?
504
+
505
+ if chunk.include?("\n") && Redaction.newline_forms.any?
506
+ write_redacted_piece(chunk)
507
+ else
508
+ chunk.each_line { |line| write_redacted_piece(line) }
509
+ end
510
+ true
511
+ rescue StandardError
512
+ false
513
+ end
514
+
515
+ # Write one piece of daemon stderr, redacted. The trailing newline is kept
516
+ # outside the replacement.
517
+ #
518
+ # @param line [String]
519
+ # @return [void]
520
+ def write_redacted_piece(line)
521
+ newline = line.end_with?("\n")
522
+ body = (newline ? line.chomp("\n") : line).dup.force_encoding(Encoding::UTF_8)
523
+ body.scrub! unless body.valid_encoding? # an invalid byte must not stop the drain
524
+ @errio.write(Redaction.text(body))
525
+ @errio.write("\n") if newline
526
+ end
527
+
445
528
  # Close the IPC pipes, stop the stderr-drain thread, and reap the daemon so a
446
529
  # respawn or quit leaves no leaked fd, thread, or zombie.
447
530
  #
@@ -454,9 +537,9 @@ module Mutineer
454
537
  rescue Errno::ESRCH
455
538
  nil # it exited between the check and the kill
456
539
  end
457
- # The drain holds a partial last line until EOF. Give it a moment to reach
458
- # EOF now the daemon is dead, so a boot error with no final newline still
459
- # prints; then stop it BEFORE closing its fd (avoids an EBADF mid-read).
540
+ # The drain ends on its own once the daemon has exited and the pipe is
541
+ # empty, writing a boot error with no final newline. Wait for that, then
542
+ # stop it BEFORE closing its fd (avoids an EBADF mid-read).
460
543
  @drain&.join(DRAIN_GRACE)
461
544
  @drain&.kill
462
545
  [@stdin, @stdout, @stderr].each { |io| io&.close rescue nil } # rubocop:disable Style/RescueModifier
@@ -7,6 +7,10 @@ require_relative "child_coverage"
7
7
  require_relative "orphan_guard"
8
8
  require_relative "concern_blocks"
9
9
  require_relative "redaction"
10
+ require_relative "database_urls"
11
+ require_relative "output_filter"
12
+ require_relative "child_cause"
13
+ require_relative "source_bytes"
10
14
 
11
15
  module Mutineer
12
16
  # App-side daemon (persistent worker).
@@ -35,9 +39,16 @@ module Mutineer
35
39
  # provision: {"cmd":"provision","slots":N} -> {"ok":true} or {"ok":false,"error":"..."}
36
40
  # run in : {"id":N,"worker":I,"payload":{"code":"<ruby>","source_file":"app/models/order.rb"},
37
41
  # "tests":["test/models/order_test.rb"],"timeout":30}
42
+ # (a :maybe mutant also sends "reach_path" and "reach_key" inside
43
+ # "payload". The child clears that file, and the fiber-local
44
+ # named by reach_key, after load and before tests, so a
45
+ # load-time call is not a test reaching the expression, #345)
38
46
  # (a mutant of a def in a concern's hook block adds to the payload
39
47
  # "hook_def":{"line":12,"name":"total","singleton":false}, #297)
40
48
  # verdict : {"id":N,"verdict":"survived"|"killed"|"error"|"timeout"}
49
+ # (an "error" adds "details" when the child sent a cause line:
50
+ # a raise → "Class: message" (#388); an in-test database error →
51
+ # "a test failed with a database error: Class: message" (#417))
41
52
  # quit in : {"cmd":"quit"}
42
53
  #
43
54
  # Worker isolation: when the app is Rails, each fork is routed to its own
@@ -85,7 +96,9 @@ module Mutineer
85
96
  @errio = errio
86
97
  @output = output
87
98
  # The client sends UTF-8 JSON; with LANG unset the pipe would read as US-ASCII (#303).
99
+ # Replies are UTF-8 too, including a non-ASCII path (#346).
88
100
  input.set_encoding(Encoding::UTF_8)
101
+ output.set_encoding(Encoding::UTF_8)
89
102
  boot_line = input.gets
90
103
  return if boot_line.nil? # client vanished before boot
91
104
 
@@ -153,6 +166,19 @@ module Mutineer
153
166
  @framework = cfg.fetch("framework", "minitest")
154
167
  @source_dirs = Array(cfg["source_dirs"]).map { |d| File.expand_path(d) }
155
168
  Dir.chdir(cfg["project_root"]) if cfg["project_root"]
169
+ # The parent's drain knows the tool's DATABASE_URL. A URL the app
170
+ # loads from a dotenv file, or assigns before it prints, is hidden
171
+ # here, where this process can see it (#341). Stderr only: stdout is
172
+ # the protocol channel reserved above. Point fd 1 at the filtered
173
+ # stderr so `puts` from the app takes the same path.
174
+ Redaction.extra_urls = DatabaseUrls.from_files(Dir.pwd)
175
+ unless OutputFilter.install(streams: [:stderr]) == :started
176
+ raise "cannot redirect stderr, so a database password would be printed"
177
+ end
178
+ if @protocol
179
+ STDOUT.reopen(STDERR)
180
+ $stdout = STDOUT
181
+ end
156
182
  ENV["RAILS_ENV"] ||= "test" if cfg["rails"]
157
183
  Array(cfg["load_paths"]).each { |d| $LOAD_PATH.unshift(File.expand_path(d)) }
158
184
  # Start Coverage BEFORE the app loads, so booted source lines are
@@ -181,6 +207,8 @@ module Mutineer
181
207
  # can surface a clean error rather than hang on the handshake.
182
208
  @output.puts(JSON.generate("ready" => false, "error" => describe_error(e)))
183
209
  @output.flush
210
+ # exit! skips at_exit, so flush the stderr pipe before it.
211
+ OutputFilter.uninstall
184
212
  exit!(1)
185
213
  end
186
214
 
@@ -192,7 +220,8 @@ module Mutineer
192
220
  # `db_role` "prepare" waits for a provision command (the first daemon).
193
221
  # "worker" only takes the shared lock (a worker daemon, including a
194
222
  # respawn). Anything else provisions now, which is what a direct client
195
- # that never sends the command needs. SQLite provisioning does nothing.
223
+ # that never sends the command needs. SQLite takes the run lock and
224
+ # copies files on first use.
196
225
  def setup_worker_db(cfg)
197
226
  require_relative "rails_worker_db"
198
227
  @worker_db = RailsWorkerDb.available? ? RailsWorkerDb : nil
@@ -289,7 +318,10 @@ module Mutineer
289
318
  # daemon does not pay for it (#228).
290
319
  Coverage.suspend if Coverage.running?
291
320
  { "map" => cmap.map, "failed_test_files" => cmap.failed_test_files,
292
- "failed_clean_tests" => cmap.failed_clean_tests, "load_lines" => cmap.load_lines.to_a,
321
+ "failed_clean_tests" => cmap.failed_clean_tests,
322
+ "failed_clean_details" => cmap.failed_clean_details,
323
+ "clean_timeouts" => cmap.clean_timeouts,
324
+ "load_lines" => cmap.load_lines.to_a,
293
325
  "load_methods" => cmap.load_methods.to_a }
294
326
  rescue Exception => e # rubocop:disable Lint/RescueException
295
327
  error = describe_error(e)
@@ -344,7 +376,12 @@ module Mutineer
344
376
  # Seed the slot's DB until the first killed/survived fork for this worker slot.
345
377
  first_use = @worker_db && !@slot_ready[worker]
346
378
  daemon = Process.pid
379
+ cause_rd, cause_wr = IO.pipe
380
+ cause_wr.sync = true
347
381
  pid = fork do
382
+ child = Process.pid
383
+ cause_rd.close
384
+ ChildCause.arm!(cause_wr, child)
348
385
  # New process group so a per-fork timeout can SIGKILL the whole subtree,
349
386
  # and silence the child's stdout so test output stays out of the diagnostics.
350
387
  Process.setpgid(0, 0) rescue nil # rubocop:disable Style/RescueModifier
@@ -358,18 +395,30 @@ module Mutineer
358
395
  # A routing failure raises here and is scored `error`, never a false verdict.
359
396
  @worker_db&.after_fork(worker, first_use ? @schema_path : nil, seed: first_use)
360
397
  apply_payload(req["payload"])
398
+ # A reload can run the probed expression while the file loads.
399
+ # Drop that write before tests, matching the in-process child (#345).
400
+ # A request with no payload (a test stub) has nothing to clear.
401
+ loaded = req["payload"] || {}
402
+ forget_load_reach(loaded["reach_path"], loaded["reach_key"])
361
403
  # A parallel worker records each pool that runs SQL, so a test
362
404
  # that closes its shared connection is still caught below.
363
- @worker_db.watch_sql if @worker_db && @shared_databases&.any?
405
+ if @worker_db && @shared_databases&.any?
406
+ @worker_db.skip_schema_check!
407
+ @worker_db.watch_sql
408
+ end
364
409
  result = run_tests(Array(req["tests"]))
365
410
  check_shared_databases!
366
411
  result
367
412
  rescue Exception => e # rubocop:disable Lint/RescueException
368
413
  @errio.puts("[daemon-child] #{describe_error(e)}")
414
+ ChildCause.write(cause_wr, e, child)
369
415
  2
416
+ ensure
417
+ ChildCause.disarm!
370
418
  end
371
419
  exit!(code)
372
420
  end
421
+ cause_wr.close
373
422
  verdict = wait_verdict(pid, timeout)
374
423
  # Mark ready only when the child finished cleanly after seeding
375
424
  # (killed/survived). Timeout can interrupt mid-copy; error can be a
@@ -382,7 +431,13 @@ module Mutineer
382
431
  # A SIGKILLed timeout child skipped its Tempfile unlink. Sweep the orphan
383
432
  # so it cannot outlive the run.
384
433
  sweep_temps if verdict == "timeout"
385
- { "id" => req["id"], "verdict" => verdict }
434
+ reply = { "id" => req["id"], "verdict" => verdict }
435
+ cause = verdict == "error" && ChildCause.read(cause_rd)
436
+ reply["details"] = cause if cause
437
+ reply
438
+ ensure
439
+ cause_rd&.close
440
+ cause_wr.close if cause_wr && !cause_wr.closed?
386
441
  end
387
442
 
388
443
  # Hide kept mutant files from Zeitwerk before the app builds its loaders.
@@ -461,6 +516,21 @@ module Mutineer
461
516
  end
462
517
  end
463
518
 
519
+ # Empties the reach sentinel after the mutant file has loaded. Same rule
520
+ # as {ReachProbe.forget_load}. Written here because this file cannot
521
+ # require Prism.
522
+ #
523
+ # @param path [String, nil] sentinel path, inside the run payload.
524
+ # @param key [String, nil] fiber-local key, `mutineer_r` plus 16 hex
525
+ # digits, the same pattern as {ReachProbe::KEY_FORMAT}.
526
+ # @return [void]
527
+ def forget_load_reach(path, key)
528
+ return unless path.is_a?(String) && !path.empty?
529
+
530
+ Thread.current[key.to_sym] = nil if key.is_a?(String) && key.match?(/\Amutineer_r[0-9a-f]{16}\z/)
531
+ File.write(path, "")
532
+ end
533
+
464
534
  # Write the tool-built mutated text beside the real source and `load` it,
465
535
  # reopening the mutated class/method in THIS child only. It goes in the
466
536
  # source file's directory (like Isolation.apply_whole_file) so a
@@ -473,30 +543,31 @@ module Mutineer
473
543
  # removes orphans and never touches a live file. Same path for reload
474
544
  # (whole file) and redefine (wrapped snippet). The backend sends the whole
475
545
  # file, so the concern blocks it stores replace the forgotten ones (#256).
476
- # With a `"hook_def"`, the loaded file stored a mutated hook-block def, and
477
- # {ConcernBlocks.place_hook_def} loads it into each class that has the
478
- # method; it raises when none was stored, so the mutant scores `error` (#297).
546
+ # With a `"hook_def"`, the load places the mutated hook-block def into each
547
+ # class that has the method, before the rest of the file runs; it raises
548
+ # when none was stored, so the mutant scores `error` (#297, #348).
479
549
  def apply_payload(payload)
550
+ hook = payload["hook_def"]
551
+ hook &&= { line: hook.fetch("line"), name: hook.fetch("name").to_sym, singleton: hook.fetch("singleton") }
480
552
  dir = File.dirname(File.expand_path(payload.fetch("source_file")))
481
553
  Tempfile.create([".mutineer_daemon", ".rb"], dir) do |f|
482
554
  # Held until this block ends, including SIGKILL (the kernel drops it).
483
555
  # Another run's sweep sees the lock and does not delete the file.
484
556
  f.flock(File::LOCK_EX) rescue nil # rubocop:disable Style/RescueModifier
485
- f.write(payload.fetch("code"))
557
+ # JSON delivered Unicode. Write the encoding the comment names (#346).
558
+ f.binmode
559
+ f.write(SourceBytes.for_file(payload.fetch("code")))
486
560
  f.flush
487
- ConcernBlocks.load_mutant(payload.fetch("source_file"), f.path)
561
+ ConcernBlocks.load_mutant(payload.fetch("source_file"), f.path, hook_def: hook)
488
562
  end
489
- hook = payload["hook_def"] or return
490
-
491
- ConcernBlocks.place_hook_def(source_file: payload.fetch("source_file"), line: hook.fetch("line"),
492
- name: hook.fetch("name").to_sym, singleton: hook.fetch("singleton"))
493
563
  end
494
564
 
495
565
  # Load the covering test files and run them; 0 = all passed (survived),
496
566
  # 1 = a failure/error (killed), 2 = the first failure was an in-test
497
567
  # database error (error, #296). Minitest only; rspec is not yet on this path.
498
568
  # {MinitestIntegration.run} is the method the daemon's clean check uses,
499
- # so each mutant fork gets the same seed and class list (#282). It loads
569
+ # so each mutant fork gets the same seed and, within each file, the same
570
+ # class order as the clean check (#282). It loads
500
571
  # only stdlib, so the daemon still needs no Prism. The fork requires
501
572
  # nothing else first: the test files' `require "test_helper"` loads
502
573
  # rails/test_help after the class list is reset, as in the clean check.