Skip to content

fix(rvm): match OPA for undefined rules and subpackages in packages - #967

Merged
Anand Krishnamoorthi (anakrish) merged 1 commit into
microsoft:mainfrom
snehabandla:fix/rvm-package-value-undefined-rules
Nov 20, 2026
Merged

Anand Krishnamoorthi (anakrish) merged 1 commit into
microsoft:mainfrom
snehabandla:fix/rvm-package-value-undefined-rules

Conversation

@snehabandla

Copy link
Copy Markdown
Collaborator

Summary

Reading a package as a value in the RVM (result := data.probe.sub) included an Undefined entry for each undefined rule of the package, and left out subpackages that contain no rules. OPA and the interpreter omit undefined rules and show rule-less subpackages as {}.

package probe
result := data.probe.sub
package probe.sub
import rego.v1
defined := 1
undefined_body if input.x == 1
undefined_value := input.missing

With input {}, OPA and the interpreter return {"defined": 1}. Before this change, both RVM execution modes returned {"defined": 1, "undefined_body": Undefined, "undefined_value": Undefined}.

package a
x := 1
package a.b
f(v) := v
package a.z

OPA and the interpreter read data.a as {"b": {}, "x": 1, "z": {}} and data.a.b / data.a.z as {}. Before this change, the RVM returned {"x": 1} and left data.a.b and data.a.z undefined.

Details

  • VirtualDataDocumentLookup builds a package's value by walking the rule tree and evaluating each rule (traverse_rule_tree_subobject in src/rvm/vm/virtual_data.rs). It stored every rule result, including Undefined. It now skips Undefined results and creates an object for every subpackage it visits, so a subpackage whose rules are all undefined still reads as {}. Defined rules are kept, including rules whose value is false, default values and empty partial sets or objects.
  • The rule tree only had nodes for rules, so packages with only functions or with no rules had none, and reads of such paths compiled to plain data access. compile_data_chain in src/languages/rego/compiler/references.rs now gives every package path under the read path an empty rule tree node and uses the virtual document lookup when the read path is a prefix of a package path (dynamic components such as data.a[k] match any package name). Package paths are computed once per compile. Programs that do not read such a path compile exactly as before.
  • Packages without rules leave empty objects in the rule tree. RegoVM::set_data only reports a conflict for non-object base data when the rule tree below that path contains a rule (contains_rule in src/rvm/program/rule_tree.rs), and the lookup keeps non-object base data instead of replacing it with a package object.

Compatibility

Package values that used to contain Undefined entries no longer do. As a result, count, object.keys, iteration and equality on them change, and json.marshal no longer emits "<undefined>" strings. Rule-less subpackages now appear as {}. The new results match OPA and the interpreter. Packages without rules never conflict with base data: a non-object base data value at or above a package path is returned as is (with {"a": {"b": "str"}} and an empty package a.b, data.a.b is "str"), and object values gain the missing subpackages as {}, as in OPA. A package with only functions over non-object base data behaves the same way; OPA rejects that policy at compile time (conflicting rule for data path a/b/f found). Package values whose rules are all defined and that have no rule-less subpackages are unchanged.

Merge note

The ### Fixed heading added under ## [Unreleased] in CHANGELOG.md may conflict with #964, which adds the same heading. Resolve by keeping one ### Fixed heading with both entries.

Testing

  • New tests/rvm/rego/package_value_differential.rs (5 tests): evaluates each query in the interpreter and in both RVM execution modes (run-to-completion and suspendable), building the engine once per test and setting the input per case. Expected values were checked against OPA 1.2.0. It covers the package value, count, object.keys, some k, _ in iteration, ==, json.marshal and membership of an undefined key, with inputs where rules are undefined, partly defined, and all defined (including non-empty partial sets and objects). It also covers a parent package that contains a subpackage, an empty subpackage, a package whose rules are all undefined, subpackages with only functions or no rules (read directly and through a dynamic key, data.a[input.k]), and packages without rules over object and non-object base data. Two base-data cases ({"a": "str"} with package a.b, and {"a": {"b": "str"}} with package a.b.c) skip the interpreter, which fails on them with make: not an object on the base commit as well. All 5 tests fail on the unchanged source (RVM only) and pass with this change.
  • cargo fmt --check
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo clippy --no-default-features -- -D warnings
  • cargo test --all-features --test mod (424 passed)
  • cargo test --all-features --lib (1038 passed, 2 ignored)
  • cargo xtask pre-push (OPA conformance: 2869 passed, 0 failed, 6 skipped)

@anakrish

Copy link
Copy Markdown
Collaborator

snehabandla Rebase to latest main to fix the dependency audit errors.

Reading a package as a value (`data.pkg`) in the RVM inserted an
`Undefined` entry for every rule in the package that was undefined, so
`count`, `object.keys`, iteration, equality and `json.marshal` saw keys
that OPA and the interpreter omit. The virtual document traversal now
skips undefined rule results and keeps subpackages as objects, so a
subpackage whose rules are all undefined reads as `{}`, as in OPA.

Subpackages that contain no rules (only functions, or nothing) were
missing from package values, and reading such a package directly was
undefined. The compiler now gives each package under a read path an
empty rule tree node and reads the path through the virtual document
lookup, so these subpackages read as `{}` as well. Package paths are
computed once per compile. Programs that do not read such a path
compile as before.

Packages without rules never conflict with base data: a non-object
base data value at or above a package path is returned as is, and
object values gain the missing subpackages as `{}`, as in OPA. A
package with only functions over non-object base data behaves the same
way; OPA rejects that policy at compile time.

Co-authored-by: Copilot <[email protected]>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

check(
&[CONSUMERS, SUB, INNER],
&[
("data.probe.result", EMPTY, Some(SUB_EMPTY)),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

snehabandla I wonder if the yaml testing framework can be extended to run multiple such cases without having to write rust based tests.

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

Undefined package slots cause inconsistent data reads, while discarded allocator errors can let compilation continue with an incomplete rule tree.

2 open issues
What changed in this PR

Aligns RVM package-value reads with OPA and the interpreter: undefined rules are omitted, and subpackages without rules appear as empty objects.

Changes:

  • Filters undefined rule results and materializes empty subpackages during virtual document lookup.
  • Tracks package paths during compilation and adjusts base-data conflict handling for rule-less packages.
  • Adds differential coverage for the interpreter and both RVM execution modes, plus a changelog entry.
File Description
tests/​rvm/​rego/​package_value_differential.rs Tests package values, derived operations, dynamic reads, and base-data merging.
tests/​rvm/​rego/​mod.rs Registers the new differential tests.
src/​rvm/​vm/​virtual_data.rs Omits undefined rules and creates empty package objects during lookup.
src/​rvm/​program/​rule_tree.rs Allows non-object base data beneath rule-less package paths.
src/​languages/​rego/​compiler/​references.rs Uses virtual lookup for matching package paths, including dynamic reads.
src/​languages/​rego/​compiler/​mod.rs Computes and stores package paths once per compilation.
CHANGELOG.md Documents the corrected RVM package-value behavior.

Review effort: Max

Comment on lines +218 to +224
// Errors are ignored: a rule already defined on this path keeps
// its leaf (OPA rejects such policies).
if let Ok(node @ &mut Value::Undefined) =
self.program.rule_tree.make_or_get_value_mut(&package_path)
{
*node = Value::new_object();
}
let Value::Object(ref mut map) = *node else {
return Ok(());
};
node = crate::Rc::make_mut(map).get_or_insert_with(key.clone(), Value::new_object);

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

Memory-limit errors are silently discarded during package-path collection and node insertion, allowing compilation to continue with incomplete package visibility or an invalid rule tree.

3 open issues

Review effort: Max

.inner
.modules
.iter()
.filter_map(|module| get_rule_path_components(&module.package.refr).ok())
@anakrish
Anand Krishnamoorthi (anakrish) merged commit cec3adb into microsoft:main Nov 20, 2026
61 checks passed
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.

3 participants