Checking how many agents are working

How this works
Contact
← Blog
Two dark mirrored boards with matching olive patterns and one cracked line between them, a small teal checkmark and the AMARBARO mark centered.
✎post de blog

mojo-port: checking that a rewrite does the same thing

The short version

mojo-port compares a Mojo rewrite with its Python original on stdout and exit status, and a mutant gate shows that the comparison can fail.

Part 6 of 20 in Tools we built and why. Previous: mokei: counting the lines and tokens in your code · Next: ctxd: keeping long AI sessions short

What this is about

Sometimes you rewrite a working program in a new language. You want it to be faster, or smaller, or to fit into a different project. The risk is plain: the new version may do something slightly different, and you may not notice until it matters.

mojo-port is our tool for this. It takes a small Python command-line program, called the oracle, and the Mojo version you wrote, called the port. It runs both on the same inputs and compares what they print and how they exit. Then it breaks the port on purpose, to check that the comparison really can fail.

Why it matters

A test that has never failed tells you very little. If you only run the cases you thought of, a green result means you thought of the right ones. It does not say you thought of all of them. The same is true of a rewrite: it may match on the ten inputs you tried and differ on the eleventh.

Two ideas fix this. The first is to compare against a trusted original, not against your memory of what the program should do. The second is to plant a bug in the new code and see whether any test notices. If no test notices a planted bug, the tests are too weak, and the green result means less than it seems to.

How it works

A tool folder holds three things: a small tool.toml that names the program, the Python original, and a file tests/cases.txt with one line of arguments per test case. Your Mojo port sits beside them.

The tool has two main checks.

  1. verify builds your port, then runs every case through the Python original and through the Mojo binary. It compares what each prints and its exit status. It does not compare error text. A folder needs at least three cases. The Python results are cached, an unchanged port is not rebuilt, and cases run in parallel.
  2. mutants flips one operator on each of up to eight lines of your port, one change per copy of the folder. For each broken copy it runs verify. If no case notices the change, the run fails. A green verify is then not only the cases you thought of.

The tool itself is one Mojo file of about 600 lines, and the checks are plain programs that never trust the Mojo.

The demo

The repository ships a demo called wcl, a line and word counter. It has the Python original, a Mojo port, nine cases and a few small files.

$ MOJO_PORT_ROOT=examples .bin/mojo-port verify wcl
wcl : 9 / 9 cases match

Now break the port on purpose. The exit status for an unreadable file is status = 1. Change it to status = 0 and run again. Verify fails and prints the two cases that read a missing file. It shows the Python result and the Mojo result side by side, and the summary reads 7 / 9 cases match.

Then restore the file and run the mutant gate:

$ MOJO_PORT_ROOT=examples .bin/mojo-port mutants wcl
wcl : mutants 8 / 8 killed (8 by a case, 0 did not compile )

Eight planted bugs, eight caught by a case.

How to do it, step by step

  1. Install the needs: Mojo 1.2, Python 3.11 or newer for the oracle, a POSIX shell and GNU coreutils on Linux.
  2. Clone the repository and build the tool: mkdir -p .bin && mojo build mojo-port.mojo -o .bin/mojo-port.
  3. Run the wcl demo to see a pass.
  4. Break the port with sed -i 's/status = 1/status = 0/' examples/wcl/wcl.mojo and run verify again to see a fail. Restore with git checkout examples/wcl/wcl.mojo.
  5. Run mutants wcl.
  6. Make your own folder: your Python program, an empty Mojo file, and at least three cases.

Other commands cover the rest of the job. cases grows your case list with a model until every line of the Python oracle runs. auto writes a brief and asks a model for the port, up to five tries, running verify after each. cert writes a certificate of verify, mutants and coverage. flip switches a tool from Python to Mojo only after a fresh verify.

What it does not prove

  • It compares stdout and exit status, not error text or timing.
  • It checks only programs whose original is a Python program using only the standard library. A program that needs a third-party import is tagged wrap, not port.
  • The cases are yours. Mutants help, but they cannot invent a behavior you never tested.
  • An equivalent mutant, one that changes the code but not the behavior, can survive. A line can be marked # mutant-ok: <why>, which skips it with a reason on record.

Words you may not know

  • Oracle: the trusted original we compare against.
  • Port: the rewrite in a new language.
  • Exit status: the number a program returns when it ends. Zero means success.
  • Stdout: the text a program prints.
  • Mutant: a copy of code with one small change planted on purpose.
  • Killed mutant: a mutant that a test caught.
  • Coverage: how many lines of a program the tests run.

Try it

Comentarii

Niciun comentariu încă.

Conectare pentru a comenta.