Rego and its parts

Rego is the engine. It parses, compiles and evaluates Rego (v1 syntax).

Rego is an interface, and each of its parts is an interface: the parser, the compiler, the evaluator and the cache. Rego(...) gets the parts by constructor injection, with the defaults as default values. Thus you can replace or decorate each part, or Rego itself.

Make an engine

Rego.standard(Re2Engine) of rego4k-builtins-stdlib-pure gives a new engine with the pure builtins of OPA's standard library. For other builtins or parts, give named arguments to Rego(...):

Engines.kt
// Rego(Builtins.Pure.stdlib(Re2Engine))
val standard = Rego.standard(Re2Engine)
val reproducible = Rego(
    // and time.now_ns, time.date, opa.runtime ...
    Builtins.Pure.stdlib(Re2Engine) + Builtins.Provided.stdlib,
    clock = InstantSource.fixed(Instant.parse("2026-01-15T09:00:00Z")),
    localZone = ZoneId.of("Europe/London"),
    // reject each impure builtin
    pure = true,
)
Engines.kt, lines 23 to 32, on GitHub

The parts and the values of Rego(...):

Parameter The default What it does
builtins Builtins.NONE The builtins that the policies can call. Operators such as == and + are builtins too. See Builtins.
parser Parser(builtins) Parses the modules and the queries. It reads each # METADATA block with the yaml.unmarshal builtin.
compiler Compiler(...) Compiles the modules and the queries. It knows the types of the builtins.
evaluator Evaluator() Evaluates the queries. It has no state.
cache RegoCache.NONE Keeps the compiled policies. NONE keeps nothing.
clock, localZone, runtime the system clock, the zone of the JVM, {} The values of the provided builtins, for example time.now_ns.
pure false If it is true, the engine rejects each impure builtin.

Make one engine and compile the policies when the application starts. Use the same compiled policy for all the requests.

Many threads can use one engine and one policy at the same time.

Compile the modules

compile takes the paths of module files and directories, or the names and the texts of modules.

Below a directory, each .rego file is a module, and its name is its path, as in opa eval -d. The name of a module is in each error message, for example policies/authz.rego:3: rego_parse_error: ....

Engines.kt
// each .rego file below the directory
val fromFiles = rego.compile(policies)
val fromText = rego.compile(RegoFileName("authz.rego") to RegoFile(source))
Engines.kt, lines 41 to 43, on GitHub

Keep the compiled policies in a cache

A RegoCache keeps each compiled policy by a SHA-256 hash of its modules, with the parser and the compiler of the engine. An application that compiles the same modules again, for example on each reload of a bundle, then compiles them only one time.

RegoCache(maxBytes, maxEntries) evicts the policy with the earliest last use when it is over a limit.

Many engines can share a cache. Two engines share a policy only if their parsers are equal and their compilers are equal.

Engines.kt
val cache = RegoCache(maxBytes = 64L * 1024 * 1024, maxEntries = 100)
val rego = Rego.standard(Re2Engine, cache = cache)
// a miss: the engine compiles the modules
val policy = rego.compile(policies)
// a hit: the same compiled policy
val again = rego.compile(policies)
check(policy.compiled === again.compiled)
// hits, misses, evictions, entries and bytes
val statistics = cache.statistics
Engines.kt, lines 51 to 59, on GitHub

With the builtins of OPA and a policy of 20 lines, a compile takes 230 µs, and a hit takes 0.5 µs (2026-12-02). The README of rego4k-core explains how the cache estimates the size of a policy, and how it uses its lock.

Decorate a part

Each part is an interface, so a decorator of Kotlin (by) can wrap the default part. This engine records each module that its parser parses:

Engines.kt
val parsed = mutableListOf<String>()
val parser = Parser(Builtins.Pure.stdlib(Re2Engine))
val rego = Rego(
    Builtins.Pure.stdlib(Re2Engine),
    // a decorator of the default parser
    parser = object : Parser by parser {
        override fun parseModule(source: String, file: String) =
            parser.parseModule(source, file).also { parsed += file }
    },
)
rego.compile(policies)
Engines.kt, lines 67 to 77, on GitHub

A Rego is an interface too. A decorator of Rego can record each compile, or check each set of modules before it compiles them.

Replace a part

Each component of rego4k follows one pattern:

Give your implementation in place of the default, or wrap the default in a decorator (see above). The components and their defaults:

Interface Default Module
Rego Rego(...) rego4k-core
Parser Parser(builtins) rego4k-core
Compiler Compiler(builtins, generative) rego4k-core
TypeChecker (@Rego4kVolatile) TypeChecker(builtins), a part of RegoCompiler rego4k-core
Evaluator Evaluator(), the object RegoEvaluator rego4k-core
RegoCache RegoCache.NONE, or RegoCache(maxBytes, maxEntries) rego4k-core
ImpureCalls ImpureCalls.LIVE rego4k-core
PrintHook PrintHook.NONE rego4k-core
RegexEngine the object JavaRegexEngine, or Re2Engine of rego4k-builtins-re2 rego4k-builtins-stdlib-pure
WasmModules WasmModules.DROP rego4k-bundles-core
BundleVerifier, BundleSigner the objects JwtVerifier and JwtSigner rego4k-bundles-signing
Toolchain Toolchain.embedded(rego) of rego4k-opa-embedded rego4k-opa-core
Opa Opa.cli() of rego4k-opa-command-line rego4k-opa-core
TestRunner TestRunner(rego) rego4k-testing-core
SeenIds SeenIds(maxIds) rego4k-server-signing
DebuggerComponents DefaultComponents(), an open class rego4k-server-debugger
BundleCatalog no default in Debugger; ServerCatalog(bundles) in DebuggerModule rego4k-server-debugger

A part with @Rego4kVolatile can change in any release. To use it, opt in with @OptIn(Rego4kVolatile::class).

Rego

The engine. It compiles modules to a policy, and evaluates its queries. Rego(...) gets its parts by constructor injection. See the declaration on GitHub.

Parser

The parser of Rego (v1 syntax). It gives the AST of a module or of a query. Parser() gives the parser of rego4k. See the declaration on GitHub.

Compiler

The compiler. It compiles parsed modules to a CompiledPolicy, and queries to a CompiledQuery. Compiler(builtins, generative) gives RegoCompiler. See the declaration on GitHub.

TypeChecker

The type checker of the compiler, with @Rego4kVolatile. TypeChecker(builtins) gives the type checker of OPA. See the declaration on GitHub.

Evaluator

The evaluator of the compiled queries of a policy. Evaluator() gives RegoEvaluator. See the declaration on GitHub.

RegoCache

A cache of compiled policies. RegoCache(maxBytes, maxEntries) evicts the policy with the earliest last use, and RegoCache.NONE keeps nothing. See the declaration on GitHub.

The layers of core

The packages of rego4k-core are layers: rego4k (the engine) uses caches, evaluator, compiler and parser. The compiler gives a CompiledPolicy (compiler/compiled), and the evaluator reads only that.

The README of rego4k-core shows the graph, and a test checks it.

Core has no builtins, and its only dependencies are parser4k and tuples4k.