Skip to content

fix(rego): ignore default rules beneath dynamically keyed rules - #964

Open
snehabandla wants to merge 2 commits into
microsoft:mainfrom
snehabandla:fix/default-under-dynamic-key
Open

snehabandla wants to merge 2 commits into
microsoft:mainfrom
snehabandla:fix/default-under-dynamic-key

Conversation

@snehabandla

Copy link
Copy Markdown
Collaborator

Summary

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 ignores a default rule beneath that prefix, so in

package pkg

p[k] := 1 if { some k in input.ks }
default p.r := 0

data.pkg.p.r is Undefined for {"ks": []} and {"ks": ["a"]}, and 1 for {"ks": ["r"]}. Regorus returned 0 for all three, in both engines, and the interpreter also added the default to data.pkg.p ({"r":0} instead of {}).

The interpreter (eval_rule and eval_query) and the RVM (run-to-completion and suspendable) now ignore such defaults, as OPA does.

Details

  • A default beneath the ground prefix of a dynamically keyed rule no longer contributes to p or to any document above it (e.g. data.pkg).
  • The default's path stays a valid reference and RVM entry point. It is read from the rules whose heads can produce it: the dynamically keyed rule itself, or a deeper rule path, e.g. p.q for p.q[j].t := 2. If none can, it is Undefined.
  • A name that only such a default introduced still resolves through the data document: in package pkg, default sub.p.r := 0 beside p[k] := 1 in package pkg.sub still lets sub and sub.p.r resolve. 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. A default beside a deeper dynamic rule (default p.t := 2 beside p.q[k].s := 1) also still applies.
  • OPA rejects a default exactly at the prefix (default p := 0 beside p[k]) as a conflict; regorus ignores it instead of rejecting it.
  • Rule gathering records the ground prefixes of dynamically keyed rules, unregisters each shadowed default at and above its prefix, and records, for each path beneath the prefix, the rules that can produce it. The interpreter and the RVM compiler share this data; the RVM reads a shadowed path by calling those rules and indexing the result, keeping the read's span for explanations.
  • Remaining known differences, present with or without the default and pinned in the test: e.g. the RVM does not support nested dynamic keys such as 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:

default r := 0
r := p.r

No public API or serialization changes.

Testing

  • New tests/rvm/rego/default_dynamic_key_differential.rs: 53 cases with OPA 1.2.0 expected values, each checked in eval_rule, eval_query and both RVM execution modes. Most cases are rerun with their shadowed default lines removed (lines marked # kept stay) 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.
  • New explain::provenance test: a read of a shadowed default keeps its path.
  • cargo xtask pre-push (fmt, clippy, build, cargo test with default features and rego-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).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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
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

Comment thread src/interpreter.rs Resolved
Comment thread src/interpreter.rs Resolved
Comment thread src/interpreter.rs Resolved
Comment thread src/interpreter.rs Resolved
Comment thread src/interpreter.rs Resolved
Comment thread src/interpreter.rs Resolved
Comment thread src/languages/rego/compiler/rules.rs Resolved
Comment thread src/languages/rego/compiler/rules.rs Resolved
snehabandla and others added 2 commits November 23, 2026 19:05
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]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/interpreter.rs
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 thread src/interpreter.rs
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 thread src/interpreter.rs
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();
Comment thread src/interpreter.rs
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants