Repository navigation
fix(rego): ignore default rules beneath dynamically keyed rules - #964
Open
snehabandla wants to merge 2 commits into
Open
snehabandla wants to merge 2 commits into
snehabandla wants to merge 2 commits into
Conversation
This was referenced Nov 18, 2026
snehabandla
force-pushed
the
fix/default-under-dynamic-key
branch
from
November 18, 2026 19:57
79a098a to
c5fe0c9
Compare
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved shadowed-default handling can omit valid producers, return incorrect results, trigger spurious conflicts, and expand rule references without enforcing the configured memory ceiling.
8 open issues
Prevent placeholder paths from conflicting with dynamic owner outputs · New Include compatible dynamic owners when resolving exact paths · New Resolve paths using compatible producers across ancestor rules · New Enforce memory limits while expanding shadowed producer lists · New Validate shadowed default values before skipping evaluation · New Use the full rule head for dynamic-default ownership · New Treat empty shadowed producer lists as bodyless rules · New Reuse cached results for shadowed paths with identical producers · New
What changed in this PR
This PR aligns Regorus’s Rego default-rule handling more closely with OPA by ignoring defaults shadowed by dynamically keyed rules in the interpreter and RVM. It preserves reference resolution and documents the breaking fallback behavior.
Changes:
- Track dynamic-rule ownership and the rules that can produce shadowed paths.
- Update interpreter resolution and RVM compilation while preserving explanation spans.
- Add 53 differential cases, a provenance regression test, and migration guidance.
| File | Description |
|---|---|
tests/rvm/rego/mod.rs |
Registers the new differential suite. |
tests/rvm/rego/default_dynamic_key_differential.rs |
Covers shadowed defaults across four evaluation paths. |
src/utils.rs |
Converts path components into typed keys. |
src/languages/rego/compiler/rules.rs |
Compiles shadowed rule paths and entry points. |
src/languages/rego/compiler/references.rs |
Routes shadowed references through producing rules. |
src/languages/rego/compiler/program.rs |
Adds literal-key indexing with span support. |
src/interpreter.rs |
Tracks ownership, suppresses defaults, and resolves affected reads. |
src/explain/provenance/tests.rs |
Checks that shadowed reads retain their provenance paths. |
src/compiled_policy.rs |
Shares ownership and producer metadata between engines. |
CHANGELOG.md |
Documents the breaking fix and fallback migration. |
Review effort: Max
A rule whose ref head has a dynamic key, such as `p[k] := 1` or `p[k].s := 1`, defines the whole document at its ground prefix `p`. OPA therefore ignores a `default` rule beneath that prefix (`default p.r := 0`): `p.r` is defined only when `p[k]` produces `r`. Regorus applied the default instead, so `p.r` was 0 even where OPA gives Undefined or the value from `p[k]`, and the interpreter also added it to `p`. Match OPA in the interpreter (`eval_rule` and `eval_query`) and in the RVM compiler (both execution modes): - A default beneath such a prefix is ignored. It no longer appears in `p` or in any document above it. - The default's path stays a valid reference and entry point. It is read from the rules that can produce it: the dynamically keyed rule, or a deeper rule path such as `p.q` for `p.q[j].t := 2`. If no rule can produce it, it is Undefined. - A name that only such a default introduced still resolves through the `data` document, e.g. `sub` for `default sub.p.r := 0` when `p[k] := 1` is in the subpackage `sub`. A read evaluates only the rules that can produce the path it reads. - Literal keys (strings, numbers including negated ones such as `p[- 1]`, booleans and null) are not dynamic, so a default beside them still applies. So does a default beside a deeper dynamic rule, e.g. `default p.t := 2` beside `p.q[k].s := 1`. - A default exactly at the prefix (`default p := 0`), which OPA rejects as a conflict, is ignored rather than rejected. Add a differential test whose expected values come from OPA 1.2.0. Each case runs in both interpreter entry points and both RVM modes. Most cases are rerun with their shadowed defaults removed and must give the same results, so the defaults are shown to be ignored. Known engine divergences that exist without the defaults too are pinned per engine. BREAKING CHANGE: a `default` rule beneath the path of a rule with a dynamic key is now ignored, as in OPA, so its value is Undefined unless a rule produces it. To keep a fallback, give it a name outside the dynamic rule's document, e.g. `default r := 0` with `r := p.r`. Co-authored-by: Copilot <[email protected]>
- Check a shadowed default's value before skipping it, so the
interpreter rejects e.g. `default p.r := input.x` beside `p[k] := 1`
as it does without the dynamic rule (and as OPA does). A bracket
default registered at the owner (`default p["r"] := input.x`) with
an invalid value is no longer unregistered, so evaluating it reports
the error as before.
- In the RVM, a shadowed default path that no rule head can produce is
compiled as a rule with no definitions, which is Undefined, instead of
falling back to the whole owner. The fallback could read a value
(e.g. 1 for `p.r.u` beside `p[x].t := {"r": {"u": 1}}`) that OPA does
not produce.
- Check the memory limit after recording each shadowed default path,
since a path stores its producer rules and the registration checks
have already run.
- In the RVM, an expression read of a shadowed default path whose
producers are all of its base's rules calls the base's rule, so reads
of `p`, `p.a` and `p.b` share one compiled rule and its cached value.
- CHANGELOG: note that an at-owner bracket default from a parent
package still applies and that invalid default values are still
rejected.
Add differential cases shadowed_default_invalid_value,
shadowed_default_invalid_value_bracket,
no_rule_produces_path_nested_value (with a no-default twin) and
reads_share_owner_rule, and the test
shadowed_default_reads_share_the_owner_rule.
Co-authored-by: Copilot <[email protected]>
snehabandla
force-pushed
the
fix/default-under-dynamic-key
branch
from
November 23, 2026 20:02
c5fe0c9 to
e732c5f
Compare
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Critical regressions cause register overflow with many entry points, break argument-bearing default-function lookup, and fail evaluation of explicitly overridden shadowed paths.
5 open issues
Skip producer lookup for explicitly overridden shadowed paths · New Preserve argument-bearing default function registrations · New Avoid allocating a second register for fresh indexed results · New Index producers by head and check deadlines during scans · New Check memory limits while building the rule-head map · New
8 resolved since last review
Enforce memory limits while expanding shadowed producer lists Resolve paths using compatible producers across ancestor rules Include compatible dynamic owners when resolving exact paths Prevent placeholder paths from conflicting with dynamic owner outputs Reuse cached results for shadowed paths with identical producers Treat empty shadowed producer lists as bodyless rules Use the full rule head for dynamic-default ownership Validate shadowed default values before skipping evaluation
Review effort: Max
| self.check_execution_time()?; | ||
| let mut matched = false; | ||
| if let Some(rules) = self.compiled_policy.rules.get(&path) { | ||
| if let Some(rules) = self.compiled_policy.rules_for(&path) { |
Comment on lines
+4778
to
+4780
| let Some(owner_len) = self.compiled_policy.dynamic_rule_owner_len(&components) else { | ||
| continue; | ||
| }; |
| span: Option<&Span>, | ||
| ) -> Result<super::Register> { | ||
| for key in keys { | ||
| let dest = self.alloc_register()?; |
Comment on lines
+4798
to
+4802
| let rules: Vec<_> = base_rules | ||
| .iter() | ||
| .filter(|rule| { | ||
| let head = heads.get(*rule).map(|head| head.get(base_len..)); | ||
| let head = head.flatten().unwrap_or_default(); |
| for rule in module.policy.iter() { | ||
| let mut head = package.clone(); | ||
| Self::push_head_keys(Self::get_rule_refr(rule), &mut head); | ||
| heads.insert(rule.clone(), head); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
A rule whose ref head has a dynamic key, such as
p[k] := 1orp[k].s := 1, defines the whole document at its ground prefix (p). OPA ignores adefaultrule beneath that prefix, so indata.pkg.p.ris Undefined for{"ks": []}and{"ks": ["a"]}, and1for{"ks": ["r"]}. Regorus returned0for all three, in both engines, and the interpreter also added the default todata.pkg.p({"r":0}instead of{}).The interpreter (
eval_ruleandeval_query) and the RVM (run-to-completion and suspendable) now ignore such defaults, as OPA does.Details
por to any document above it (e.g.data.pkg).p.qforp.q[j].t := 2. If none can, it is Undefined.datadocument: inpackage pkg,default sub.p.r := 0besidep[k] := 1inpackage pkg.substill letssubandsub.p.rresolve. A read evaluates only the rules that can produce the path it reads.p[- 1], booleans and null) are not dynamic, so a default beside them still applies. A default beside a deeper dynamic rule (default p.t := 2besidep.q[k].s := 1) also still applies.default p := 0besidep[k]) as a conflict; regorus ignores it instead of rejecting it.p.q[k].s, and accepts only rule paths as entry points.Compatibility
Breaking: a policy that relied on such a default now gets Undefined, as in OPA. To keep a fallback, give it a name outside the dynamic rule's document:
No public API or serialization changes.
Testing
tests/rvm/rego/default_dynamic_key_differential.rs: 53 cases with OPA 1.2.0 expected values, each checked ineval_rule,eval_queryand both RVM execution modes. Most cases are rerun with their shadoweddefaultlines removed (lines marked# keptstay) and must give the same results; this compares regorus with itself, since OPA rejects some of those variants. Engine divergences from OPA are pinned per engine.explain::provenancetest: a read of a shadowed default keeps its path.cargo xtask pre-push(fmt, clippy, build,cargo testwith default features andrego-extensions, doc tests, no_std, ACI and Kata suites, OPA conformance suite: 2869 passed);cargo test --all-features --test mod(420 passed) and--lib(1039 passed).