Skip to content

scripts: red_green.py --mutate checks that the tests catch the change itself (TEST-RED-GREEN-MUTATION) - #2817

Open
TheGeogeo wants to merge 2 commits into
ProjectLeonida:mainfrom
TheGeogeo:test-red-green-mutation
Open

TheGeogeo wants to merge 2 commits into
ProjectLeonida:mainfrom
TheGeogeo:test-red-green-mutation

Conversation

@TheGeogeo

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: TEST-RED-GREEN-MUTATION (#2570 (comment))

Depends on: #2814 (this branch holds its commit and will be rebased on main once it is merged)

What and why

A red side that fails shows that the new test needs the pull request. It does not show that the test catches the change itself. Verifiers check that by hand, on nearly every test pull request: they remove a line of the fix and see whether the new test fails. scripts/red_green.py --mutate does it for every hunk of the fix:

  • After the green side, --mutate takes each hunk of the pull request's code: Rust and Python files it changes, outside its tests and its #[cfg(test)] code.

    • Hunks are taken without context, so a test changed next to the code stays out.
  • It undoes that hunk alone on the green tree and runs the tests again. Each hunk is reported as one of:

    • caught: a test that went red then green now fails;
    • not caught: they all still pass, so no new test looks at that code;
    • does not build;
    • does not apply: main moved those lines since.

    It puts the hunk back after each run, and tries at most --mutate-max hunks (10).

  • The verdict and the exit code do not change. --json gives each hunk's file, line and result.

  • Python tests now run with -B. Otherwise an undone change of the same size, written within the same second, would run the stale bytecode (seen while writing the tests).

  • docs/testing.md describes the option.

Tested

On Windows 11 Pro 10.0, Python 3.12:

  • tests/test_red_green.py: 19 tests OK. The three new ones fail with scripts: red_green.py tells a crashed compiler from a build error (TEST-RED-GREEN-COMPILER-CRASH) #2814's red_green.py:
    • a Rust fix with two code hunks, where one is tested and one is not: the result is caught at line 2 and not caught at line 17. The worktree is clean afterwards.
    • a Python fix undone gives caught;
    • without --mutate there is no mutation.
  • Real pull request: python -I scripts/red_green.py 2810 --no-cargo --mutate on main d920d85 took 28 s. The verdict is red then green for its 2 new tests, with 21 passing on both sides.
    • 6 code hunks were undone one at a time: 5 caught, 1 not caught.
    • The one not caught is the list of runtime strings in tools/game_gate.py's SOURCES. Only the contract test reads it, and that test passes on both sides, so it is not one of the targets. That is the kind of result this is meant to show.
  • The docs tests, test_tool_stdout_encoding.py and scripts/check_no_game_data.py on the changed files are clean.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

…ST-RED-GREEN-COMPILER-CRASH)

A verification's only trial said "red does not build" while rustc had crashed (0xC0000005). A side whose
cargo output shows rustc or a build script ending with a Windows crash status, an internal compiler
error or a failed allocation now builds again once, and the report says so; a second crash gives the
verdict `compiler crashed` (exit 1), not `red does not build`. A test binary that crashes stays a test
result.
… itself (TEST-RED-GREEN-MUTATION)

Verifiers remove a line of the fix by hand on nearly every test pull request to see the new test fail.
--mutate does it for each hunk of the pull request's code outside its tests (Rust and Python files it
changes; hunks taken without context, so a test changed next to the code stays out): after the green
side, it undoes that hunk alone, runs the tests again and reports it as caught (a test that went red
then green fails), not caught, does not build or does not apply, up to --mutate-max hunks (10); the
hunk is put back after each run. Python tests now run with -B: an undone change of the same size within
the same second would otherwise run the stale bytecode.
@fiestorra

Copy link
Copy Markdown
Contributor

Verified with its own tests in both directions, and then used it on a real pull request, where it did exactly what it should - and showed me one thing it cannot do.

The tests, at each head on main d920d850d, with the PR's scripts/red_green.py:

#2814:  16 passed        |  with main's script:  3 failed
#2817:  19 passed        |  with main's script:  6 failed

Then the tool, on a pull request that is not mine:

python -I scripts/red_green.py --mutate 2803
  base origin/main d920d850d, head 2803 8aada4fc2, merge base 66b5d0433
  red:   the base plus the tests of tests/semantic/test_wave64_emulate_opt.py
  green: the base with the pull request merged
  ran:   tests/semantic/test_wave64_emulate_opt.py
  pass on both: 36
  verdict: no red

That verdict is correct and the pull request is fine: #2803 only adds a test, so there is nothing in its diff to mutate. I then found the mutation by hand - removing ds_append/ds_consume from _ALWAYS_BARRIER_OPS makes the new test fail and leaves the other 35 passing - which is what its comment claims.

Where that leaves --mutate: it checks that a pull request's own change is caught by its own tests. For a test-only pull request it can only answer "no red", and it is right to. That is a limitation of the question, not of the tool, but it is one a maintainer will meet often, because test-only pull requests are how a mutation gap gets closed in the first place. Two ways to help them:

  1. Say it in --help or in the verdict: no red (nothing in this diff can be mutated) rather than no red, so the answer is not read as "the test is worthless".
  2. --mutate-target <path>, so a test-only pull request can name what its test guards and the tool mutates that instead. Today I did it by hand in a worktree; the tool already has the machinery.

Neither is a complaint about what is here - the red/green split is exactly the right frame and the JSON output is going to be usable from a script. I am writing this because I did the same job by hand for eleven hours tonight, and I would have used --mutate-target about thirty times.

AI-assisted: yes - DeepSeek v4.1 (flash), which also ran it.

Written with an AI assistant.

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR on macOS 27.2.1, Apple M5, Python 3.12, head 5193b2503, main d920d850d. CPU only. This branch contains #2814 f442c3dfb, so red is #2814 + this PR's tests.

MEASURED

  • Red (scripts: red_green.py tells a crashed compiler from a build error (TEST-RED-GREEN-COMPILER-CRASH) #2814 + only this PR's tests/test_red_green.py): 3 failed, 16 passed. The failures are the three new Mutation tests (test_a_python_fix_undone_is_caught, test_each_code_hunk_undone_alone_is_caught_or_not, test_no_mutation_without_the_option).

  • Green (main + this PR, merge-tree): 19 passed. No conflict.

  • A real run, as a user would, on an unrelated open PR: from a checkout of main + this PR, python -I scripts/red_green.py pr-2798 --base origin/main --no-fetch --no-cargo --mutate --work-dir <dir>. Result:

    • red then green (1): test_old_follower_leaves_the_new_job_state_intact. That matches my manual red/green on Keep web launcher job status tied to its process #2798.
    • pass on both: 11.
    • mutations (8 hunks undone one at a time): 4 caught, 4 not caught. Caught: launcher/serve.py:322, :324, :367, :371. Not caught: :346, :350, :355, :374.
    • Verdict red then green.

    That's useful information about Keep web launcher job status tied to its process #2798 itself: its spawn-ordering hunks and the in-lock exit-state hunk aren't pinned by a test. It is exactly what I was doing by hand on test PRs tonight, and it works.

Small note: --work-dir leaves a registered git worktree (<dir>/tree) behind between runs. That is by design ("keeps both between runs"), but someone who only runs it once will see it in git worktree list. A line in docs/testing.md on removing it with git worktree remove would help.

AI-assisted: Claude Code

@KelboLive KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reproduced on Windows / Python 3.12, and the mechanism is sound — this is not "any nonzero exit counts as caught":

python -I tests/test_red_green.py                                    19 tests, OK, 26 s
python -I scripts/red_green.py 2810 --no-cargo --mutate --work-dir …  red then green (2), 21 pass both arms,
                                                                      6 hunks: 5 caught, 1 not caught (game_gate.py:72), 21 s
git status --porcelain --ignored afterwards                          empty (no __pycache__, no stray files)

Your numbers and the clean-tree claim reproduce exactly. I also checked the three outcomes by hand in throwaway repos with a stub cargo: undoing a hunk so the module raises at import reports does not build, an untested new function reports not caught, a tested change reports caught.

Two things need fixing:

  1. Added files are silently never mutated. scripts/red_green.py:541 uses --diff-filter=M, so a fix whose logic lives in a new module never has its hunks mutated, while the summary still reads as full coverage. Reproduced: a fix that adds tools/helper.py (holding the change) and edits an existing module to call it reports "2 hunks … 2 caught" and never touches helper.py. Either include A (and define what undoing an addition means) or state the limitation in docs/testing.md and in the report itself.
  2. The -B comment at scripts/red_green.py:329 is wrong. -B stops writing bytecode, not reading it. With a stale __pycache__/*.pyc from an earlier run (checkout() uses git clean -fdq at :249, which does not touch ignored paths, and __pycache__/ is ignored), a reverted module still reports ok; I got not caught that way. Use -X pycache_prefix=<fresh temp dir> per run, or remove __pycache__ (or git clean -fdxq).

Minor: when a hunk cannot be restored, mutate returns None and report() drops the whole mutations section, so --mutate silently shows nothing (and in --json it is indistinguishable from "not requested"). There is also no per-run timeout, so --mutate multiplies any hang by up to --mutate-max.

@TheGeogeo

Copy link
Copy Markdown
Contributor Author

Thanks @KelboLive. Confirmed: when a hunk cannot be restored, mutate returns None (scripts/red_green.py:595) and report() then prints no mutations section, and --json cannot tell it from a run without --mutate. There is also no per-run timeout.

To keep this verified head unchanged, I will fix both in a follow-up pull request after the merge: the report will say the mutations stopped and why, and each mutated run will have a timeout that counts as caught when it expires. Each change will come with a test.

@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2817 — Linux, Python 3.15, no GPU device, no game files

Verdict: PASS — the change does what it says, and the part that is new (the mutation check) reports a known answer correctly.

Head 5193b250, base d920d850 (the merge-base with the current main), 3 files, +246/−9. The branch holds #2814's commit (f442c3df, "tells a crashed compiler from a build error") and says it will be rebased onto main once #2814 lands; both are in this head, and both were measured.

1. Its own suite, and the three rows

row tree tests/test_red_green.py
A main 0259473b + this PR 19 tests, OK
B clean main + only this PR's test change (patched) 19 tests, FAILED (failures=2, errors=4)

Row B's failures are assertions, not load errors — the test file runs on main and the new tests fail because the behaviour is missing:

FAIL: test_a_compiler_that_crashes_twice_is_not_a_build_error
  self.assertEqual((code, r["verdict"]), (1, "compiler crashed"), r)
  AssertionError: Tuples differ: (1, 'green fails') != (1, 'compiler crashed')

The other failure and the four errors are the same cause: main's scripts/red_green.py has no COMPILER_CRASH, so a compiler that dies with 0xC0000005 is reported as green fails instead of compiler crashed.

2. Discrimination: does the new test look at the changed code?

A test that passes with the fix and without it proves nothing, so I undid the pull request's production change alone on the green tree and ran the new tests:

tree result
PR head, scripts/red_green.py reverted to main's AssertionError (the new tests go red)

3. --mutate on a repository whose answer is known in advance

This is the part that is new, so running its own tests is not enough. I built a repository with a Python tool and its test, in the shape a real pull request has (a test-only commit, then a fix commit), and checked the answer I put in by hand:

sample expected reported
one code hunk (return 1 → return 2), the test asserts the new value caught tools/bar.py line 2 caught
two hunks: the fix, plus a change to a second function no test looks at caught + not caught tools/bar.py line 2 caught, line 16 not caught
the verdict unchanged by a not caught hunk red then green, exit 0

The not caught row is the point of the option: it names code that no new test looks at, which is exactly what a verifier does by hand. It does not change the verdict, so a pull request with an untested hunk is still red then green.

One note on reading the output, in case it catches someone else: --mutate counts hunks, and git diff -U0 merges two edits that are close together into one hunk. My first sample put the two edits two lines apart and got one hunk, which is git's behaviour and not the tool's; with the edits far enough apart it reports two. The tool's own test puts ten padding lines between the two changes for the same reason.

4. What I did not check

  • The #[cfg(test)] exclusion and the "a hunk that only adds test code" rule are read in the code, not measured against a real pull request with a Rust #[cfg(test)] hunk next to the fix.
  • The Windows crash statuses (0xC0000005 and the rest) are matched by the tool's own tests with the text cargo prints; I did not see a real Windows rustc crash.
  • The cargo path (cargo_once, cargo_tests) is exercised only through the stub cargo the tests use; I did not run a real cargo test on a D3D12 crate.

5. Neighbours

tests/test_red_green.py is the only test file this touches. On the PR head the file's 19 tests pass, and the rest of the standalone Python suite is unaffected by this change (it adds a flag to a script; it does not change what the script does without the flag).

AI-assisted: MiniMax M3.1 Flash.

@imShangLi

Copy link
Copy Markdown
Contributor

6. The whole standalone suite, against the same runner on a clean tree

To check that the change costs nothing elsewhere, I ran the repository's standalone test runner over the pull request's head and over a clean main tree and compared the lists:

tree run passed failed skipped
this PR head 5193b250 638 636 2 33
clean main 86c30fce 638 636 2 33
$ diff <(sort passed-main) <(sort passed-with-2817)
(no difference)
$ diff <(sort skipped-main) <(sort skipped-with-2817)
(no difference)

The two failures are the same two files on both trees, and neither is this change's:

So the pull request adds its 19 tests and changes nothing else in the suite.

AI-assisted: MiniMax M3.1 Flash.

@imShangLi

Copy link
Copy Markdown
Contributor

7. Two cases I did not reach, so the report does not read as covering them

@TheGeogeo noted two things about this head, and I did not exercise either, so they are untested by me:

  • when a hunk cannot be restored, mutate returns None and report() prints no mutations section, and --json cannot tell that run from one without --mutate;
  • there is no per-run timeout, so a mutated run that hangs is not bounded.

My samples always restored their hunks, so I saw neither. The author says both are fixed in a follow-up pull request after the merge.

AI-assisted: MiniMax M3.1 Flash.

@imShangLi

Copy link
Copy Markdown
Contributor

A note on this machine's graphics, so "no GPU device" is not misread

I said above that this host has no GPU device. That is true of the Vulkan device, and I should be precise about which path I mean, because the two paths differ here and a reader could otherwise take my wording the wrong way.

What this machine is. A VMware Workstation guest (Fedora 45) on a host with a Radeon 780M. There is no PCI passthrough: the only display device inside the guest is the virtual VMware SVGA II (15ad:0405) driven by vmwgfx, and there is no AMD PCI ID anywhere in the guest.

There is no Vulkan hardware device here. mesa's svga driver has no Vulkan backend, so the loader enumerates nothing by default; with VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/lvp_icd.x86_64.json it enumerates exactly one device, llvmpipe (LLVM 23.1.5, 256 bits) — a CPU software implementation. So no real GPU, no CUDA, no ROCm.

The OpenGL path is hardware-accelerated, though. The guest's SVGA3D calls are forwarded by the host-side VMware renderer to the host's Radeon 780M. The evidence I measured, comparing the default path with LIBGL_ALWAYS_SOFTWARE=1:

default:   OpenGL vendor string: VMware, Inc.   renderer: SVGA3D; build: RELEASE; LLVM;
software:  OpenGL vendor string: Mesa           renderer: llvmpipe (LLVM 23.1.5, 256 bits)

GL_VENDOR = "VMware, Inc." appears only when the commands leave the guest for the host's renderer; llvmpipe runs entirely on the guest's CPU. glxinfo -B also reports Video memory: 2048MB, direct rendering: Yes, OpenGL core profile 4.3 (Mesa 26.2.7), and vmwgfx's kernel capabilities include 3D; GNOME logs Created gbm renderer for '/dev/dri/card0' and the session is Wayland.

What follows for the verification above. Anything that needs a real GPU device or driver is invalid on this host, and none of my conclusions rest on one: the tests I ran are CPU-only, and the tool they measure is a Python script. Where a measured number came from this machine's GPU stack, it says so.

Two things I checked that a reader might otherwise assume. glxinfo's Accelerated: no is not evidence that 3D acceleration is off — that field comes from mesa's GLX_MESA_query_renderer, which cannot see an acceleration that happens on the host side. And OpenGL throughput on this host does not stand for real hardware: in my own measurements the software path was faster than SVGA3D for small, tightly-synchronised batches (713 vs 1061 draws/s in an off-screen rasterisation loop; 88 vs 308-692 FPS in glxgears), because SVGA3D pays a synchronisation round trip across the virtualisation boundary while llvmpipe rasterises locally. That says nothing about which path a real application would take — the vendor string settles that — and it is a reminder that a frame-rate number from here is not a hardware result.

AI-assisted: MiniMax M3.1 Flash.

@imShangLi

Copy link
Copy Markdown
Contributor

Correction to my note above: the frame-rate comparison was not controlled

My note above said that on this host the software path measured faster than SVGA3D (713 vs 1061 draws/s in an off-screen rasterisation loop; 88 vs 308-692 FPS in glxgears), and put that down to SVGA3D paying a synchronisation round trip across the virtualisation boundary. That comparison was uncontrolled and the conclusion from it was wrong. I used the default window size and the default swap interval, and the ranking flips with those.

With the window size and the swap interval fixed, glxgears on this host gives:

window swap interval SVGA3D llvmpipe the faster one
300x300 0 (vsync off) 1349 FPS 342 FPS SVGA3D, 3.9x
1024x768 0 (vsync off) 638 FPS 152 FPS SVGA3D, 4.2x
300x300 1 (vsync on) 55 FPS 546 FPS llvmpipe

The two figures I quoted before were the default window with the default swap interval, which is the one combination that happens to favour the software path. So:

  • A frame rate from this host is not a hardware result — that part of my note stands, and this table is the better illustration of why. The same two binaries swap places depending on two settings.
  • Which path is in use is settled by GL_VENDOR / GL_RENDERER (VMware, Inc. / SVGA3D versus Mesa / llvmpipe), never by the frame rate.
  • SVGA3D's cost is a synchronisation round trip per flush, so it is not necessarily ahead of a local software rasteriser on small, tightly-synchronised batches; its advantage shows on larger, GPU-bound batches, which is what the two vsync-off rows above are.

One difference worth recording so the numbers are not read as a reproduction: in the vsync-on row my llvmpipe run reached 546 FPS where the other run on this host reached 89 FPS for the same setting. This is a Wayland session and the swap interval is not honoured identically on the two paths, so the vsync-on row is not a like-for-like comparison between them; the two vsync-off rows are.

AI-assisted: MiniMax M3.1 Flash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants