Testing and verification

Performance

The benchmark times the cases of OPA's v1 conformance suite that expect a result: a set of solutions (want_result). The benchmark README explains how to run it.

The benchmark times each case on three engines:

The benchmark interleaves the engines over the same span of time. It repeats each case, for statistical confidence.

In the last full run (1135 cases, 4 passes), these were the geometric means of the time, as a ratio of native OPA's time:

That run was before commit 4e0921c (2026-11-30). This commit made rego4k approximately 2.5 times slower: it built the rule index again for each evaluation.

On 2026-12-02, after the fixes, three runs of benchGate gave a ratio of 0.23 to 0.25 for rego4k on HotSpot (rego4k 3.4 µs to 3.6 µs, native OPA 13.6 µs to 15.7 µs): rego4k is about 4 times as fast. Before the fixes, the ratio was 0.67. See "Finding" in HANDOFF.md.

These policies are small (microseconds for each evaluation), so the fixed cost of each evaluation is the largest part of the time. The results do not show the performance of large policies.

See verification/benchmark/README.md for the method.

Verification

Coding agents wrote most of the engine code of rego4k. If you doubt code that an agent wrote, we agree: do not trust it because it passes its own tests.

Trust it only as far as independent evidence shows. The oracle for each layer below is the OPA 1.23.0 binary, or a published specification, not a test that the same agent wrote.

The layers

  1. OPA's conformance suite. All 2538 cases of OPA's v1 suite pass, with OPA's expected results and error codes. check runs the suite again with a tracer on each evaluation. It also runs the suite with each case shaken to only its reachable rules and builtins. The results must not change.

  2. Cases from the OPA binary. About 2400 more cases in 72 YAML files, in the format of OPA's suite. Each expected result or error comes from a run of the OPA binary, not from rego4k. They cover each fault that the other layers found. check also runs them with the RE2 engine.

  3. Differential fuzzing against the OPA binary. The fuzzer generates programs from the types of the builtins, with adversarial values. It runs each program in rego4k and in OPA. It also runs each program after transformations that must not change its result:

    • the same program with different line breaks,
    • the with modifiers in the opposite order,
    • the program in the body of a rule,
    • the program with strict builtin errors.

    It compares the raw JSON, the solutions as a multiset, the error codes and the messages.

    • The fuzz gate (4500 programs, about 25000 cases) runs before each commit. It fails for a new difference, and also when a known difference disappears. Each of the 18 known differences has a reason: the exact arithmetic of rego4k, or a fault in OPA (see Divergences from OPA).
    • The gate uses Re2Engine, the regex engine of OPA.
    • Each night, a run of 50000 programs uses a new seed.
    • A separate mode (-Pmode=index) shows which rules each engine evaluates, to compare the rule index with OPA's.
  4. Ports compared on many inputs. When rego4k copies a Go library (for example gqlparser, jwx, crypto/x509, encoding/json or gojsonschema), the port is compared with the OPA binary on many inputs. For example, about 4000 GraphQL schemas, and 193 JSON texts for json.unmarshal.

  5. Old against new. A large refactor of the parser, the compiler or the evaluator must give the same results as the code before it. The comparison uses tens of thousands of inputs, including inputs that fail. For example, the trampolined evaluator ran about 68000 fuzz cases on both versions, and the parser changes ran up to 192000 inputs.

  6. External tools. OPA loads, runs and verifies the bundles that rego4k writes and signs. cosign, the signing tool of the Sigstore project, verifies the decisions that rego4k-signing-signer signs.

  7. Long and large inputs. Tests run these inputs on a 512 KB stack:

    • bodies of 20000 expressions,
    • chains of 10000 rules,
    • 10000 nested brackets,
    • collections of 100000 elements.

    A test fails if the time of size n is more than 30 times the time of size n / 10. Thus a quadratic or exponential step fails it on any machine.

  8. Mutation testing. PIT, a mutation testing tool for the JVM, changes the code on purpose. Then it checks that a test finds each change. The survivors show the gaps in the tests.

  9. Benchmark thresholds. rego4k must not get much slower than native OPA, measured in the same run.

  10. Static checks. check fails for each of these faults:

    • a warning of the compiler,
    • a declaration that nothing uses,
    • an unused import,
    • a package with more than 7 files,
    • a dependency between packages that the layers do not allow (ArchUnit, a library that tests the architecture of Java code).

    These checks keep the code small enough for a person to read.

Each divergence from OPA that is on purpose is in Divergences from OPA, with its reason.

Humans design the API

Agents built the engine: the parser, the compiler, the evaluator and the ports of the builtins. Their correctness comes from the layers above.

A person designs the API that a user calls, because only a person can judge if an API is easy for a person to use. Examples of these decisions:

When the layers run

rego4k must give the same results as OPA, and it must not get much slower without a decision. These layers run:

Layer Command When
The unit tests, the 2538 cases of OPA's conformance suite and the cases of rego4k ./gradlew check each commit (pre-commit hook), each push
The fuzz gate: 4500 generated programs, compared with native OPA ./gradlew fuzzGate each commit (pre-commit hook), each push
The benchmark thresholds on every 10th case ./gradlew benchGate each push
The benchmark thresholds on all the cases ./gradlew benchNightly each night
The long fuzz run: 50000 programs with the date as the seed ./gradlew fuzzNightly each night
The mutation testing of core and stdlib, against the tests and the fuzz gate ./gradlew pitest each night
The tests and the fuzz gate against the latest release of OPA, if it is not the pinned version the workflow opa-latest each day
The soak run: new seeds against opa eval and opa run --server, and PIT on every package of the library ./gradlew soak -Phours=48 by hand, on a machine with no other work

Benchmark thresholds

The thresholds in verification/benchmark/thresholds.txt give the maximum ratio of the time of rego4k to the time of native OPA. One threshold is for all the cases, and one is for each group of categories, for example strings or crypto.

The two engines run interleaved on the same machine. Thus the ratio changes much less than the times when the machine is slower.

The margins find large changes only: on a machine with a low load, benchGate fails if rego4k becomes approximately 1.6 times slower. See "Thresholds" in verification/benchmark/README.md.

Mutation testing

./gradlew pitest runs PIT on the main code of rego4k-core and rego4k-builtins-stdlib-pure.

PIT makes small changes (mutants) to the bytecode, for example a changed operator or a removed call. Then it runs the tests against each mutant. A mutant that no test finds (a survivor) shows a gap in the tests.

The report is in verification/mutation/build/reports/pitest/. -Pmutate=rego4k.values.* mutates only the classes of one package. check does not run PIT, because it is slow.

All the classes take too long for one night. rego4k.values has 322 mutants and 2% of the bytecode, and PIT took 2 h 27 min for it with 2 threads.

Thus -Pshard=k/50 divides the classes, sorted by name, into 50 shards with about the same size of bytecode. Each night mutates one shard, by the day of the year. Thus the nightly build mutates all the classes in 50 nights.

The first run, on rego4k.values, killed 292 of the 322 mutants (91%). These are the scores of the runs of 2026-12-03 (PIT counts a mutant that it stops at the time limit as killed), and the scores after the tests of the gaps:

package mutants killed killed after the new tests
rego4k.values 322 303 (94%) 305 (95%)
rego4k.caches 61 51 (84%) 55 (90%)
rego4k.builtins.contexts 16 8 (50%) 13 (81%)

All the mutants that stay are equivalent: the change does not change the behaviour. Most of them remove a null check of the Kotlin compiler on a value that is never null. HANDOFF.md gives the reason for each mutant.

PIT runs these layers against each mutant:

PIT runs the tests that cover a mutant from the fastest to the slowest, and it stops at the first test that fails. The fuzz gate takes about 30 s, so it runs only for the mutants that the faster tests do not find.

A JUnit test is the simplest way to add this layer: PIT gives it this order with no plugin. The module verification/mutation holds the test, so that check does not need the opa binary.

PIT does not run the benchmark against a mutant, for these reasons:

Thus a mutant that makes rego4k slower but not wrong, and that stays in this time limit, is not found. benchNightly finds such a change when somebody commits it.

PIT has no support for Kotlin of its own. The Kotlin plugin of Arcmutate, the commercial extensions of PIT, needs a paid licence. Thus the build uses plain PIT.

PIT removes some mutants in the code that the Kotlin compiler generates, but not all. Thus some survivors are in generated code, for example in the equals of a data class.

The GitHub Actions build

These workflows run on GitHub Actions:

The jobs get the tools with ./gradlew fetchBenchTools (pinned versions, with checksums). They keep the Gradle caches and .bench-tools between runs.

To see the results, do these steps:

  1. Open the "Actions" tab of the repository on GitHub, and select the workflow build, nightly or opa-latest.
  2. Select a run. The page shows each job with a mark for success or failure.
  3. Select a job to see its log. The log of benchGate and benchNightly has the table of the ratios and the maximums.
  4. Download the files from the "Artifacts" part at the bottom of the page of the run:
    • benchmark: report.html and the results of the benchmark.
    • fuzz-nightly: the report of the long fuzz run, with one YAML file for each group of differences.
    • mutation-report: the report of PIT. Open index.html.
    • conformance-report: the result of each conformance case. The check job keeps it only if it fails.
    • opa-latest: the page and the reports of the checks against the latest release of OPA.

The latest release of OPA

The build uses the pinned version of OPA (opaVersion in gradle.properties). A new release of OPA can add cases to the conformance suite, change a builtin or add a builtin.

.github/workflows/opa-latest.yml runs each day at 04:00 UTC, and when you start it. It does not change the pinned version. It has two jobs:

  1. latest gets the tag of the latest release from the GitHub API. If the tag is the pinned version, the job summary says so, and the workflow passes.
  2. compatibility runs only if the latest release is not the pinned version. It does these steps:
    1. fetchOpa -PopaVersion=<tag> downloads the opa binary of the release, and examines its published checksum. OPA_BIN is this binary in all the later steps.

    2. tests: ./gradlew test -PopaVersion=<tag> runs the conformance suite of the release, and all the other tests. These tests also compare rego4k with the new binary, for example:

      • OpaContractTest (opa run --server),
      • the contract tests of verification/capabilities,
      • OpaFormatTest (opa fmt),
      • ParseModuleMessagesTest,
      • BuiltinDescriptionsTest (opa capabilities),
      • HttpSendOpaTest.
    3. fuzz-gate: the fuzz gate with the new binary. It compares the differences with known-differences.txt of the pinned version. A known difference does not fail it. A new difference, a changed difference and a difference that is gone fail it.

    4. fuzz: fuzzNightly, with the date as the seed. A difference does not fail it.

    5. report: verification/opa-latest/report.py finds each difference of the release from the pinned version. Each difference has a stable key, for example:

      • builtin:<name> for a new builtin,
      • conformance:<file>#<note> for a failed conformance case,
      • test:<class>#<test> for another failed test,
      • fuzz-gate:<case> for a new, changed or gone difference of the fuzz gate.

      The script writes the line of the release in verification/opa-latest/tested-versions.tsv, and the page The latest release of OPA. The page gives each difference with its detail and the command that shows it. The page is also the job summary.

    6. commit: if the record or the page changed, the step commits them to main as github-actions[bot]. Then it starts the workflow site, which publishes the page. The comment of the step explains why this cannot make a loop.

    7. verdict: the job fails if a difference is not in verification/opa-latest/documented-differences.tsv, or if a key of that file does not occur.

Thus the workflow fails each day until each difference of the release is fixed or documented.

A documented difference has a line <key><tab><reason>. A documented difference that does not occur also fails the workflow, so that the list stays correct.

To fix the differences, give the page to a coding agent. See verification/opa-latest/README.md.

The artifact opa-latest holds the page, the conformance report, the JUnit results, the output of the fuzz gate and the report of the fuzz run.

To run the same checks locally against a release, do these steps:

  1. Download the binary of the release into a directory of your own, and examine its checksum.
  2. Set OPA_BIN to the binary.
  3. Run ./gradlew test fuzzGate -PopaVersion=<tag> --continue.

Update the pinned version of OPA

When the workflow opa-latest fails for a new release, do these steps:

  1. Set opaVersion in gradle.properties to the tag of the release.
  2. Set OPA_VERSION in .devcontainer/Dockerfile to the same tag. Then build the devcontainer again.
  3. Run ./gradlew fetchOpa, or set OPA_BIN to the new binary. The repository has no checksum of the opa binary to change: fetchOpa and the Dockerfile examine the checksum that OPA publishes with each release. The cache key of .bench-tools holds gradle.properties, so the GitHub Actions build gets the new binary.
  4. Run ./gradlew check. Correct rego4k for each conformance case and each test that fails.
  5. Run ./gradlew fuzzGate. Examine each new, changed and gone difference. Correct rego4k, or document the difference in Divergences from OPA. Then run ./gradlew fuzzGate -Pupdate.
  6. Run ./gradlew benchGate. A new release of OPA can be faster or slower.
  7. Change the version in the text of README.md, the pages and the home page of site, and HANDOFF.md, for example "2538 cases (OPA v1.23.0)".
  8. Remove the lines of verification/opa-latest/documented-differences.tsv. Each key there compares a release with the old pinned version. The record of the tested versions stays.
  9. Add a note about the update to the status in HANDOFF.md: the version, and what changed for rego4k.