Divergences from OPA

In these cases, rego4k gives a different result from OPA, on purpose.

Precision of numbers

OPA computes these numbers with a Go big.Float (arithmetic.go):

SetString gives this big.Float a mantissa of 64 bits, so OPA loses precision. rego4k computes these numbers exactly:

rego
# OPA: 12345678901234567889920   rego4k: 12345678901234567890123
round(12345678901234567890123)
# OPA: 1000000000000000000024696061952   rego4k: 10 to the power 30
floor(1e30 + 0.5)
# OPA: 9999999999999999999731564544   rego4k: 10 to the power 28
ceil(1e28)
# OPA: 10000000000000000000281880683947... (401 digits)
ceil(1e400)
                                # rego4k: exactly 10 to the power 400
OnPurposeTest.kt, lines 27 to 35, on GitHub

The two results differ only for a number that a mantissa of 64 bits cannot hold. Such a number has more than 19 significant digits, or an exponent of 28 or more.

Regex with java.util.regex

OPA uses Go's RE2 engine, the regex library of Google. JavaRegexEngine uses java.util.regex, because it has no dependencies. Its syntax, its semantics and its running time are different from RE2:

rego
# OPA: false   java.util.regex: true (a look-ahead)
regex.is_valid("(?=a)")
# OPA: true   java.util.regex: false (it reads the characters : a l p h)
regex.match("[[:alpha:]]", "b")
# OPA: false   java.util.regex: true ($ also matches before a final \n)
regex.match("a$", "a\n")
# x of 53 characters: OPA takes 10 ms, java.util.regex does not end in 60 s
regex.match("^(.*a){12}$", x)
OnPurposeTest.kt, lines 46 to 53, on GitHub

Builtins.Pure.stdlib(regex) needs a regex engine, so each application chooses one.

For OPA's behaviour, use Re2Engine of rego4k-builtins-re2: Rego(Builtins.Pure.stdlib(Re2Engine)). See its README.

A rule and base data at the same path

OPA rejects this at compile time. rego4k gets the data only when it evaluates a query, so it does not reject it. The base data wins:

rego
package p

# with the data {"p": {"x": 5}}: OPA gives a compile error,
# rego4k gives data.p.x == 5
x := 1
OnPurposeTest.kt, lines 67 to 71, on GitHub

If you want OPA's behaviour, do not give base data and rules at the same path.

The depth of nesting of JSON and YAML

OPA gives an error for JSON or YAML with more than 10000 levels of nesting (exceeded max depth). rego4k has no limit on the depth.

rego4k reads, compares and writes values without recursion, so a deep value does not overflow the stack.

ML-DSA signatures in a certificate chain

crypto.x509.parse_and_verify_certificates and crypto.x509.parse_and_verify_certificates_with_options use a port of Go's Certificate.Verify. The JDK does the RSA and Ed25519 signature math.

Java 21 has no ML-DSA, so rego4k does not accept a certificate that an ML-DSA key signed. OPA accepts it.

The order of a set term or an object term

When OPA first reads a set that a set term with non-constant items makes, it iterates the items in the order of the compiled terms. An object term with a non-constant key is the same: OPA iterates its keys in the order of the compiled key terms.

A cached read of the same rule, and every other set or object, iterates in the order of values. rego4k always iterates a set or an object in the order of values.

The solutions are the same. Only the order of errors can differ, for example which of two operand errors comes first in strict mode:

rego
x := "a"
# OPA: k = "b" first, because a string term comes before a var term
some k, v in {x: 1, "b": 2}
# strict: OPA: the error for "b"   rego4k: the error for "a"
to_number(k)
OnPurposeTest.kt, lines 82 to 86, on GitHub

To copy OPA, a set or an object value would need a second order that each cache and builtin removes.

opa test

rego4k test and TestRunner give the outcomes and the text of opa test, with these differences:

The README of rego4k-testing-core lists them, with the flags that are not supported, for example --coverage and --bench.

The conflict errors

OPA 1.23.0 lists the conflicting values of a conflict, with the location of each rule:

text
eval_conflict_error: rule data.p.x produced conflicting values:
  1 at policy.rego:3
  2 at policy.rego:4

rego4k gives the first line, without the : and the list. The conformance suite compares only the start of the message.

The trace and the explanations

The tracer gives the events of OPA's trace, and Explanation gives OPA's text of opa eval --explain (see rego4k-core).

OpaExplanationTest compares the forms full, notes and fails with OPA on its own policies. It also compares the form full on each case of OPA's conformance suite. In 2045 of the 2193 cases without an expected error, the text is the same.

explanations/differences.txt lists the other cases. These are the differences.

The numbers of the vars of the compiler

OPA's compiler gives each var that it makes the name __localN__, or __localqN__ in a query. rego4k's compiler gives other numbers, and the vars of the traced evaluation have numbers from 1000000.

The comparison renames the vars in the order of their first use.

rego
# OPA: __local0__ = input.xs; count(__local0__, __local1__); gt(__local1__, 1)
count(input.xs) > 1
                      # rego4k: __local1000000__ = input.xs;
                      # count(__local1000000__, __local1__); gt(__local1__, 1)
ExplanationsTest.kt, lines 35 to 38, on GitHub

Template strings

OPA's compiler makes a template string a call of internal.template_string, with a set comprehension for each placeholder. rego4k evaluates the template string as one term, so its trace has fewer steps.

rego
# OPA: Eval internal.template_string([__localq0__, " ", __localq2__], __localq4__)
x := $"{input.a} {input.b}"
                              # rego4k: Eval __localq8__ = $"{{...}} {{...}}"
ExplanationsTest.kt, lines 52 to 54, on GitHub

A collection with calls in the head of a rule

OPA keeps the collection in the head, and evaluates only the calls in the body. rego4k evaluates the collection in an expression of its own, so its trace has one more Eval and Redo.

rego
# rego4k: Eval __local4__ = [__local5__, __local6__]
p := [count(a), count(b)] if { a := "x"; b := "yz" }
ExplanationsTest.kt, lines 68 to 69, on GitHub

not with calls, and rego.metadata

For not with calls in its expression, OPA's compiler makes a body of not with the calls in it. rego4k keeps the expression.

OPA gives the value of rego.metadata.chain() and rego.metadata.rule() in an expression of its own. rego4k puts the value in the expression of the call.

rego
# OPA: Eval not {__local0__ = input.x; __local1__ = input.y; ...}
not plus(input.x, input.y) = input.z
ExplanationsTest.kt, lines 80 to 81, on GitHub

with that replaces a builtin or a function with a rule

rego4k evaluates the rule when it applies the with. OPA evaluates the rule when the expression calls the builtin. Thus the steps of the rule come at another place.

rego
# rego4k: the steps of f before q. OPA: the steps of f in q.
p if q with time.now_ns as f
ExplanationsTest.kt, lines 98 to 99, on GitHub

Rules at constant keys of a ref head

For a ref with a key into rules at constant keys, OPA looks up the rules of each key, and its Index has the key. rego4k looks up all the rules of the ref one time.

rego
q[1] := 1
q[2] := 1
# OPA: Index data.test.q[1] (matched 1 rule)
# rego4k: Index data.test.q (matched 2 rules)
p := count([x | q[x]])
ExplanationsTest.kt, lines 112 to 116, on GitHub

Unifications in the debug explanation

OPA gives a Unify event at each step of its unification, also for the keys and the values of a ref with vars. rego4k gives a Unify event for each unification of an expression (= and :=), and for the args of a function.

Thus the debug explanation of rego4k has fewer lines. The other forms have no Unify events.

rego
# OPA: Unify x = input.xs[i], Unify 0 = i, Unify 1 = x, ...
# rego4k: Unify x = input.xs[i]
x := input.xs[i]
ExplanationsTest.kt, lines 127 to 129, on GitHub

An error stops the trace

With strict-builtin-errors, OPA keeps an error of a builtin and continues the evaluation as if the call were undefined. At the end, it gives the first error. rego4k stops at the error, so its trace ends there.

After any error, opa eval evaluates the query a second time, and its explanation has the steps of both evaluations. The REST API gives no explanation with an error, as OPA's.

rego
# strict: OPA: Fail div(1, 0, __local0__), then more steps
# rego4k: the trace ends at the call
p if 1 / 0 == 1
ExplanationsTest.kt, lines 142 to 144, on GitHub

The JSON form of an explanation

Without pretty, the REST API gives the events as JSON objects with the AST of each node. These are the differences:

rego
# OPA: {"head": {"name": "allow", "ref": [allow], ...}}
# rego4k: the ref data.app.allow
allow if input.user == "alice"
ExplanationsTest.kt, lines 159 to 161, on GitHub

Partial evaluation

OpaPartialEvaluationTest compares the partial evaluation with OPA's. In 2168 of the 2193 cases of OPA's suite (with input unknown, and without an expected error), and in each of its own 10 policies, the JSON is the same. The comparison renames the vars that the evaluators make.

partial/differences.txt lists the other 25 cases:

The partial evaluator has no option for --disable-inlining, --shallow-inlining or --nondeterminstic-builtins.

It runs on a thread with a large stack, because it is a recursion of the JVM.

Faults in OPA

rego4k does not copy these faults of OPA 1.23.0:

A with changes the source of another with

A with on a part of a document that another with replaces changes the source of the replacement, and the change stays after the with. In rego4k, a with changes only its target.

rego
# OPA also changes data.p.base
x with data.d as data.p.base with data.d.a as {}
# after this query expression, data.d has the key a in OPA
t := 1 with input as data.d with input.a as 2
OpaFaultsTest.kt, lines 22 to 25, on GitHub

A ref in a with value stays a ref

In some places, OPA does not evaluate a ref in a with value, and the value holds the ref. rego4k evaluates the ref.

rego
# OPA prints {"a": data.p.r}
every v in [1] { v; x := data.d with data.d.a as data.p.r; print(x) }
OpaFaultsTest.kt, lines 40 to 41, on GitHub

If a builtin gets such a value, its result or its error is not correct. For example:

If OPA gives eval_internal_error, the fuzzer counts the case as unsupported. In other cases, the fuzzer finds a difference, for example the fuzz cases g13-888 and g15-5701.

A set term with equal items

A set term with two items that become equal gives each item when OPA iterates over it, but count gives 1. In rego4k, a set has no duplicates.

rego
# OPA: 2 solutions   rego4k: 1 solution
x = "p"; some y in {x, "p"}
OpaFaultsTest.kt, lines 54 to 55, on GitHub

A var that an object key of its own pattern binds

If OPA reads a var that an object key of its own pattern binds, OPA crashes.

rego
# rego4k: a solution without v; opa eval crashes when it writes v
v = {v: 1}
OpaFaultsTest.kt, lines 65 to 66, on GitHub

An object term with equal keys

An object term with two keys that become equal has one key and two values in OPA. In rego4k, the later key wins.

rego
x = "a"
# both engines: o == {"a": 2} and count(o) == 1
o := {"a": 1, x: 2}
# OPA: 1   rego4k: 2
o.a
# OPA: 1, two times   rego4k: 2, one time
some v in o
OpaFaultsTest.kt, lines 78 to 84, on GitHub

If an expression calls print and has a with value with a comprehension, a local var of the comprehension gets the argument of print in the result of the query. rego4k gives no binding.

rego
# OPA gives the binding y = ["p"]
print("p") with input.a as [x | y := 1]
OpaFaultsTest.kt, lines 96 to 97, on GitHub

OPA's query compiler does not rewrite a print call in the domain of every. Its rewritePrintCalls does not walk into the domain of an every, and only the rule compiler walks each nested body again (WalkBodies).

The same body in a rule has no error. rego4k gives the result of the rule.

rego
# OPA: "print: arity mismatch"
every m in [1 | print(input.x, 0)] { m }
# OPA: eval_internal_error
every m in [1 | print(1)] { m }
OpaFaultsTest.kt, lines 107 to 110, on GitHub

every in the domain of every in a query

OPA's query compiler does not rewrite the vars of an every in a comprehension in the domain of another every. Then the result of the query has bindings of these vars, with the values of other terms of the query. rego4k gives no binding.

rego
# OPA gives the binding i = [true]
every k in [1, 2] { k > 0 }; every e in [true | every i in [3] { i > 0 }] { e }
OpaFaultsTest.kt, lines 121 to 122, on GitHub

Outputs of a call that every gets

OPA's safety check keeps the outputs of a call that it cannot order yet in a buffer, and gives them to an every that it examines next (outputVarsForExpr). Thus a var can become safe without an expression that binds it.

rego4k gives "var _ is unsafe", as OPA does without the every.

rego
# OPA accepts it, then eval_internal_error
y[s] < sprintf(s, ["a"]); every z in ["p"] { y; z }; y = {"a": "a"}
OpaFaultsTest.kt, lines 132 to 133, on GitHub

every before the last call of a body

The same buffer of outputVarsForExpr can also make OPA reject a safe body. If a call with an output argument is the last expression, and an every before it reads that var, the buffer still holds the var.

OPA reads it as an output of the every ("closure-self"), so it never adds the every. rego4k accepts the body, as OPA does if another expression follows the call.

rego
# OPA: "var x is unsafe"   rego4k: x == 1
every y in [1] { x == y }; abs(1, x)
# both engines: x == 1
every y in [1] { x == y }; abs(1, x); true
OpaFaultsTest.kt, lines 144 to 147, on GitHub

If another expression reads the var between the every and the last call, OPA adds the every after that expression. rego4k adds the every after the call, as OPA does if a further expression follows the call.

Thus an error in the every can stop the query in rego4k, and the other expression can fail first in OPA:

rego
# OPA: no solution   rego4k: a conflict error
every v in [1] { not k; data.p.r == v }; k; is_string(1, k)
OpaFaultsTest.kt, lines 162 to 163, on GitHub

The name of an undeclared var of a template string in a query

If a var of a template string in a query is undeclared, OPA gives the name that its compiler gave to the var, for example __localq2__. OPA uses the names of the compiler of the modules there, so it cannot find the name of the source.

rego4k gives the name of the source. In a rule, the two engines give the same name.

rego
# OPA: "var __localq2__ is undeclared"   rego4k: "var x is undeclared"
some x in [input.b[_]]; y := $"{x[0]}"
OpaFaultsTest.kt, lines 174 to 175, on GitHub

An unsafe var with a closure

OPA's safety check does not find an unsafe var of an expression with a closure that reads a var of the body. rego4k gives "var unbound is unsafe".

rego
# OPA accepts it, then eval_internal_error
q contains x if { x = 1; {"a" | x} > unbound }
OpaFaultsTest.kt, lines 188 to 189, on GitHub

Faults in OPA that rego4k copies

rego4k copies a fault of OPA when the fault is deterministic and simple to copy, so that a policy gives the same result in both engines.

not and with on a call of rego.metadata.*

Before it evaluates a body, OPA replaces each call of rego.metadata.rule() and rego.metadata.chain() with its value. It then drops the not and the with modifiers of that expression. rego4k gives the same results.

rego
# true, although rego.metadata.rule() always has a value
not rego.metadata.rule()
# the with has no effect
rego.metadata.rule() with rego.metadata.chain as 1
CopiedFaultsTest.kt, lines 17 to 20, on GitHub

A policy has no reason to write not rego.metadata.rule(). With no # METADATA block, the value is {}, not undefined. In both engines, not rego.metadata.rule() is true even for a rule that has a block.

To find a rule with no block, write this:

rego
rego.metadata.rule() == {}
CopiedFaultsTest.kt, lines 34 to 34, on GitHub