Reloading
By default an application uses the rules compiled into it: bree::rules! in Rust and bree-gen in Go embed the rule sources in the binary. The rules change when the application is rebuilt and deployed, through the same review and CI as any other change. For most applications, that is the right way to run.
An application can instead load its rules from a directory and pick up changes while it runs. Use this in development, to see a rule change without restarting, or in production if rules must change faster than you deploy.
What is picked up
The engine hashes the contents of the rules directory every interval (1 second by default). When the hash changes, it loads the directory as it would at startup, and uses the new rules only if:
- every rule, module and test type checks;
- every test passes;
- every rule the application was built with is still there, with the same
InputandOutputtypes (the fingerprint check).
If any of these fails, the change is refused and the previous rules keep serving. The engine does not check the same refused files again; the next edit triggers a new attempt. Evaluations in flight are never interrupted, and each one uses either the old rules or the new ones, never a mix.
A new rule can be added while the application runs, but code that uses it through generated types needs a rebuild. It can be evaluated by name straight away.
Seeing what happened
A refused change would otherwise go unnoticed, because the application keeps working, so every reload and every refusal is reported. In Laravel Rust and Laravel Go, they go to the application's log:
level=WARN msg="bree: kept the previous rules because the rules have 1 problem:\n rules/shipment_eligibility.test.luau:42: test \"express is capped at 20 kg, below the carrier's 30 kg\" failed\n expected { eligible = false, reason = \"overweight\" }\n got { eligible = true, reason = \"eligible\" }"
level=INFO msg="bree: loaded shipment_eligibility" rule=shipment_eligibility digest=sha256:e2cbe0d589f37319dd989b3e012dd23f238cd2349850cb665cdb4c8c14392fadThat is the Laravel Go example, running, while the express cap in its rule was raised from 20 to 25 kg. The first edit broke a test, so it was refused and the app kept deciding with the 20 kg cap. Updating the test too was accepted.
bree serve prints the same events to standard error. In Rust, pass a hook to Builder::on_reload, and read Engine::last_reload_error (also in bree serve's /v1/health) to see whether the latest change was refused.
Turning it on
| Rules from a directory | Interval | |
|---|---|---|
| Laravel Rust | BREE_RULES=rules |
BREE_RELOAD_INTERVAL_MS, default 1000 |
| Laravel Go | BREE_RULES=rules |
BREE_RELOAD_INTERVAL_MS, default 1000 |
| Rust | Engine::builder().bundle(&rules::RULES).dir("rules") |
.reload_every(Duration::from_secs(1)) |
| Go | bree.Open(rules.Rules, bree.WithDir("rules")) |
bree.WithReload(time.Second) |
bree serve |
--rules rules |
--reload-ms 1000 |
An interval of 0 loads the directory once, without watching it. In Laravel, leave BREE_RULES empty (the default) to use the rules compiled into the binary.
use std::time::Duration;
let engine = bree::Engine::builder()
.bundle(&rules::RULES)
.dir("rules")
.reload_every(Duration::from_secs(1))
.on_reload(|event| match event {
bree::ReloadEvent::Loaded { changed, removed } => { /* log them */ }
bree::ReloadEvent::Refused(error) => eprintln!("kept the previous rules because {error}"),
})
.build()?;Engine::reload() checks for changes immediately and returns Ok(true) if new rules were loaded. It retries a refused change, so it is also the way to try again after fixing something outside the rules directory.
Cost
Checking for changes hashes the directory: about 0.14 ms for the example rules, on a background thread. With the directory checked every 10 ms, evaluations took 0.148 ms at the median against 0.144 ms without; see Cost. Loading a change costs what startup does, about 23 ms for the example, also off the request path.