Bree

The runtime

Every integration, from the Rust API to Laravel Go to bree serve, evaluates through the same Rust engine. This page describes what one evaluation does, its limits, its errors and its cost.

One evaluation

  1. The input, as JSON, is checked against the rule's Input type. Every problem is collected, not just the first.
  2. A fresh Luau VM is created for this call, with the sandboxed globals and the rule's precompiled bytecode. Nothing is shared with any other call, so one evaluation cannot affect the next.
  3. The rule runs under the step, memory and time limits.
  4. The output is checked against the rule's Output type and converted back to JSON. Tables may nest up to 128 deep, so a table of a recursive type that contains itself is an error rather than an endless value.

The result is a decision:

{
  "rule": "free_shipping",
  "digest": "sha256:f7c9611f58d4f678cfc0ae1b74b66a9f69e51dc05aad015e1f54b18e5917855c",
  "output": { "free": true }
}

digest is a SHA-256 of the rule's source and the source of every module it requires, directly or not. Store it next to decisions you need to explain later: with the input and the digest, you can find the exact code and run the decision again.

A decision whose answer is no ({ "free": false }) is a successful evaluation. Errors are for evaluations that produced no decision.

Limits

Limit Default Error
Steps (roughly, loop iterations and calls) 1,000,000 execution_limit_exceeded
Memory allocated by the VM 16 MiB memory_limit_exceeded
Wall-clock time 1 second execution_limit_exceeded
{"error":{"code":"execution_limit_exceeded","message":"the rule ran past its limit of 1000000 steps"}}
{"error":{"code":"memory_limit_exceeded","message":"the rule tried to use more than 16777216 bytes of memory"}}

Business rules rarely get near these; they exist so a mistake such as a loop that never ends fails one call instead of hanging a request thread. Tests run under the same limits, so a test that sends a rule into an endless loop fails instead of hanging the build. Change them in Rust with Builder::limits and in Go with bree.WithLimits.

Errors

Every integration reports the same codes. bree serve and bree_laravel::error_response answer with the status in the last column.

Code Meaning Status
invalid_request The request is malformed, such as a body that is not JSON 400
rule_not_found No rule with that name is loaded 404
types_mismatch The caller was built against different Input or Output types than the loaded rule has 409
invalid_input The input does not match Input, or the rule called invalid 422
invalid_output The rule returned something its Output type does not allow 500
rule_error The rule raised an error, for example with error() 500
execution_limit_exceeded The rule ran past its step or time limit 500
memory_limit_exceeded The rule tried to use more memory than its limit 500

A 4xx means the caller sent something wrong. A 5xx means the rule failed, and is worth an alert.

invalid_input and invalid_output errors list their problems as violations. field is a dotted path such as service_limits.0.max_weight_grams, counting array positions from 0, and is empty for the value as a whole:

{"error":{"code":"invalid_input","message":"the input does not match the Input type of free_shipping","violations":[{"field":"order_total_cents","message":"must be a whole number"},{"field":"country","message":"must be one of \"CA\", \"US\""},{"field":"x","message":"is not a field of this type"}]}}

A rule_error message gives the file and line:

{"error":{"code":"rule_error","message":"boom.luau:5: boom"}}

Cost

Rules are type checked, tested and compiled to bytecode once, when they load. Each evaluation then validates, runs the bytecode in a fresh VM, and validates again.

cargo run --release -p bree --example latency measures it with the shipment_eligibility rule from examples/laravel-rust, through the same Engine every integration uses. On a 16-thread Windows desktop:

load: type check, run the tests, compile (once, at startup): 23.2713ms
evaluate: p50 143.9µs  p99 311.8µs  max 658.7µs
evaluate while the reload thread checks every 10 ms: p50 147.6µs  p99 357.1µs  max 704.6µs
one reload check with nothing changed (hash the directory): 137.462µs

So about 0.14 ms per decision, with no network hop, and no measurable cost from reloading in the background. An engine is safe to share between threads; evaluations run in parallel.

Edit this page on GitHub