Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
11a4c77
rego: test that a root read through the item's key and then a key fro…
jbpros Dec 5, 2026
ee90813
rego: read a root through the item's key before any item whatever key…
jbpros Dec 5, 2026
133137c
rego: test that a key from $params in front of the item's key keeps t…
jbpros Dec 5, 2026
1c0ac52
rego: read a root before any item when a key from $params sits in fro…
jbpros Dec 5, 2026
6213dda
rust: test that a root read through a key from $params and then the i…
jbpros Dec 5, 2026
b264611
rust: read a root before any item when a key from $params sits in fro…
jbpros Dec 5, 2026
9861717
docs: list the fixes and keep the inputs the two engines leave out in…
jbpros Dec 5, 2026
9e032c1
rego: test that a bound line alias in front of the item's key reads t…
jbpros Dec 5, 2026
e776f69
rego: read a root before any item through a bound line alias in front…
jbpros Dec 5, 2026
dd9ccd9
rust: test that a root read through the item's key and then an unboun…
jbpros Dec 5, 2026
99810a3
rust: read no root before any item when a later key reads an unbound …
jbpros Dec 5, 2026
99e0c1b
test the causes of a root read through a key from $params and the ite…
jbpros Dec 5, 2026
7ce5848
docs: record the line alias forms in the changelogs and reword the in…
jbpros Dec 5, 2026
3fbe8cc
spec: add cases for a root read through the line alias and the item's…
jbpros Dec 5, 2026
a1ca8b9
docs: say the cases for the line alias and the item's key copy the Ru…
jbpros Dec 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This file is about `ergo.rego` only. The Rust engine's changes are in [engines/r

### Fixed

- A root read through the item's `named by` key is read once, before any item, whatever key comes before or after the item's key, as [tally.walk](spec/semantics.md#counts-and-sums-in-sentences) and [for.roots](spec/semantics.md#for) say. With no `m` in the params, the params' `k` holding `"a"` and `tests` holding `[]`, `count of tests whose v equals $params.m named by key named by $params.k is 0` and `count of tests whose v equals $params.m named by $params.k named by key is 0` used to pass, and now fail as `absent` with `$params.m is missing`. The same goes for a `sum of`, and for a key from the subject beside the key from `$params`, like `$params.m named by pr.key named by $params.k named by key`. Changes the report of such a check. Under `for: every xs, if any, as x`, a key that reads the line's alias in front of the item's key, like `$params.m named by x named by key`, reads the root with each item of `xs`: with `xs` holding `["a"]` it used to pass and now fails as `absent` with `item 1 of xs: $params.m is missing`, and with `xs` holding `[]` it still passes.
- A `count of` or `sum of` under a `for` line no longer fails an empty list with `, if any,` on an item its `that` or `whose` leaves out, or on an item when the `that` or `whose` reads the line's alias, like `count of zw.k that exists is at most x.n` or `count of tests.v that equals x.m named by key is 0` ([for.roots](spec/semantics.md#for), [for.if_any](spec/semantics.md#for)). With `zw` holding `[{"k": 1}, {"v": 2}]`, `count of zw.k that exists is at most x.n` also passes when `xs` holds `[{"v": 1, "n": 3}]`, where it failed as `absent` with `item 2 of zw: k is missing`. The items a `that` reading the line's alias can't decide are said under each item of the line, like `item 1 of xs: item 2 of zw: k is missing`, and no longer up front as `item 2 of zw: k is missing`. A sentence that reads no alias of the line, like `count of zw.k that is 1 is at most 5`, says such an item once, where it said `item 2 of zw: k is missing; item 2 of zw: k is missing`. Changes the report of such checks.
- A key that starts with a `$`, read on each item of a walk, is the item's key and not a name, as [name.keys](spec/syntax.md#naming-a-path) says. `every tests."$x" is 1` with every test holding `"$x": 1` used to fail as `absent` with `$x is missing`, and now passes, and the message of an item that fails quotes the key, like `item 2 of tests: "$x" is 2`.
- Under a second `for` line, an earlier line's alias of the wrong type in the `whose` of a `count of` or the `that` of a `sum of` fails the check for each item of the first list, even when the second list is empty, as [for.roots](spec/semantics.md#for) says. With `ys` holding `[{"rv": "s"}]`, `xs` holding `[]` and `zw` holding `[{"k": 1}, {"k": 2}]`, `for: every ys as y, every xs, if any, as x` with `count of zw whose k is at most y.rv is at most x.n` fails as `unusable` with `item 1 of ys: y.rv is "s", which isn't a number`, where it passed. With items in `xs`, the message no longer puts `item 1 of xs` in front of it. Changes the report of such a check.
Expand Down
11 changes: 11 additions & 0 deletions engines/rego/src/lines/roots.rego
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,17 @@ _keyed_read_before(step) if {
is_string(object.get(_fb({}, step), _fk(step), null))
}

_keyed_read_before(step) if {
_is_ref(step)
_ref_state(step.ref) == "value"
is_string(_ref_read(step.ref))
}

_keyed_read_before(step) if {
_name_step(step)
is_string(object.get(object.get(input, "ergo/names", {}), step.named_by, null))
}

_keyed_plain_end(p) := min({i | some i, step in p; i > 0; not is_string(step)})

_keyed_off_start(p) if _part_start(p)
Expand Down
5 changes: 0 additions & 5 deletions engines/rego/src/messages/walks.rego
Original file line number Diff line number Diff line change
Expand Up @@ -640,11 +640,6 @@ _m_keyed_fault_paths(subj, whose) := {p |
_whose_reads_keyed_root(whose) if {
some c in whose
some p in _keyed_cond_paths(c)
is_array(p)
count(p) > 2
last := p[count(p) - 1]
is_object(last)
"named_by_field" in object.keys(last)
_keyed_root_path(p)
}

Expand Down
37 changes: 37 additions & 0 deletions engines/rego/tests/lines/roots_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,43 @@ test_keyed_root_fault_names_a_missing_root_from_input_or_params_by_its_whole_pat
ergo._keyed_root_fault({}, ["$$input", "d", "m", {"named_by_field": "key"}]) == {"cause": "null", "text": "$input.d is null"} with data.ergo_document as {"d": null}
}

test_a_key_from_params_in_front_of_the_items_key_keeps_the_root_read_before_any_item if {
pr := {"key": "a", "tests": []}
every p in [
["$$params", "m", {"ref": ["$$params", "k"]}, {"named_by_field": "key"}],
["$$params", "m", {"ref": ["$$params", "k"]}, {"named_by_field": "\u0003pr\u0003key"}, {"named_by_field": "key"}],
["$$params", "m", {"named_by_field": "\u0003pr\u0003key"}, {"ref": ["$$params", "k"]}, {"named_by_field": "key"}],
] {
ergo._keyed_root_path(p) with input as {"ergo/names": {"ergo/subject": pr}} with data.ergo_params as {"k": "a"}
ergo._keyed_root_fault(pr, p) == {"cause": "absent", "text": "$params.m is missing"} with input as {"ergo/names": {"ergo/subject": pr}} with data.ergo_params as {"k": "a"}
}
}

test_a_bound_line_alias_in_front_of_the_items_key_reads_the_root_before_any_item_of_the_walked_list if {
pr := {"key": "a", "tests": []}
every p in [
["$$params", "m", {"named_by": "x"}, {"named_by_field": "key"}],
["$$params", "m", {"named_by": "x"}, {"ref": ["$$params", "k"]}, {"named_by_field": "key"}],
["$$params", "m", {"ref": ["$$params", "k"]}, {"named_by": "x"}, {"named_by_field": "key"}],
] {
ergo._keyed_root_path(p) with input as {"ergo/names": {"ergo/subject": pr, "x": "a"}} with data.ergo_params as {"k": "a"}
ergo._keyed_root_fault(pr, p) == {"cause": "absent", "text": "$params.m is missing"} with input as {"ergo/names": {"ergo/subject": pr, "x": "a"}} with data.ergo_params as {"k": "a"}
not ergo._keyed_root_path(p) with input as {"ergo/names": {"ergo/subject": pr}} with data.ergo_params as {"k": "a"}
}
}

test_a_root_read_through_a_key_from_params_and_the_items_key_takes_the_roots_cause_and_leaves_a_key_that_cant_be_read_to_the_key if {
pr := {"key": "a", "tests": []}
p := ["$$params", "m", {"ref": ["$$params", "k"]}, {"named_by_field": "key"}]
names := {"ergo/names": {"ergo/subject": pr}}
ergo._keyed_root_fault(pr, p) == {"cause": "null", "text": "$params.m is null"} with input as names with data.ergo_params as {"k": "a", "m": null}
ergo._keyed_root_fault(pr, p) == {"cause": "unusable", "text": "$params.m is 3, which isn't an object"} with input as names with data.ergo_params as {"k": "a", "m": 3}
ergo._keyed_root_fault(pr, p) == {"cause": "absent", "text": "$params.m is missing"} with input as names with data.ergo_params as {"k": ""}
every params in [{}, {"k": null}, {"k": 3}] {
not ergo._keyed_root_path(p) with input as names with data.ergo_params as params
}
}

test_a_root_read_through_two_subject_keys_that_both_fail_takes_the_worst_cause if {
p := ["$$params", "m", {"named_by_field": "\u0003pr\u0003key"}, {"named_by_field": "\u0003pr\u0003j"}]
every row in [
Expand Down
8 changes: 8 additions & 0 deletions engines/rego/tests/messages/walks_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ test_a_root_read_through_the_items_key_in_a_whose_is_read_before_any_item_whatev
}
}

test_a_root_read_through_the_items_key_and_then_a_key_from_params_is_read_before_any_item if {
c := {"op": "compare", "cmp": "eq", "left": ["v"], "right": ["$$params", "m", {"named_by_field": "key"}, {"ref": ["$$params", "k"]}]}
every pair in [[{"k": "a"}, {"cause": "absent", "text": "$params.m is missing"}], [{"k": "a", "m": null}, {"cause": "null", "text": "$params.m is null"}], [{"k": "a", "m": 3}, {"cause": "unusable", "text": "$params.m is 3, which isn't an object"}]] {
ergo._m_tally_keyed_base({"tests": []}, {"count_of": ["tests"], "whose": [c]}).faults == [pair[1]] with data.ergo_params as pair[0]
}
not ergo._m_tally_keyed_base({"tests": []}, {"count_of": ["tests"], "whose": [c]}) with data.ergo_params as {"k": "a", "m": {}}
}

test_a_count_shows_every_item_it_cant_read_whatever_its_cause_but_not_a_root_or_a_params_item_of_another_cause if {
item := {"item": {"i": ["xs", 0]}}
ergo._m_record_shown(["absent", "item 1 of xs: v is missing", item], "unusable")
Expand Down
1 change: 0 additions & 1 deletion engines/rego/tests/spec_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ known_differences := {
"sentences / a root from $input or the subject itself read through a named by key from the subject, in a count of or a sum of over items, is said once on the assertion, and each item the clause can't read is said after it / puts the root on the assertion and gives an item entry only to the item the clause can't read",
"sentences / a root from $params read through a named by key from $params and then the item's key is read once, before any item, so a root it doesn't have fails the check on an empty list too / fails as absent naming the root when the params have no m and tests is empty",
"sentences / a root from $params read through a named by key from the subject and then the item's key is read once, before any item, so a root that has no key for the subject's key fails the check on an empty list too / fails as absent naming the root through the subject's key when the params' m has no such key and tests is empty",
"sentences / a root from $params read through the item's key and then a named by key from $params is read once, before any item, so a root it doesn't have fails the check on an empty list too / fails as absent naming the root when the params have no m and tests is empty",
"sentences / a root from $params read through the item's own key, in a one-line every with if any, is not an object, so it fails as unusable on an empty list / fails as unusable naming $params.m when tests is empty",
"sentences / a root from $params read through the item's own key, in a one-line every with if any, is null, so it fails as null on an empty list / fails as null naming $params.m when tests is empty",
"sentences / a root read through the item's own key, in a one-line every with if any, is read once, before any item, so it fails on an empty list when it is missing, null or not an object / fails as absent naming $params.m and $input.m when neither is there and tests is empty",
Expand Down
1 change: 1 addition & 0 deletions engines/rust/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Each release names the version of the spec it follows. The spec's own changes ar

### Fixed

- A root read through a `named by` key from `$params` and then the item's `named by` key is read once, before any item, as [tally.walk](../../spec/semantics.md#counts-and-sums-in-sentences) and [for.roots](../../spec/semantics.md#for) say. With no `m` in the params, the params' `k` holding `"a"` and `tests` holding `[]`, `count of tests whose v equals $params.m named by $params.k named by key is 0` used to pass, and now fails as `absent` with `$params.m is missing`. The same goes for a key from the subject beside it, like `$params.m named by pr.key named by $params.k named by key`. Changes the report of such a check. Under `for: every xs, if any, as x`, a root read through the item's key and then the line's alias, like `$params.m named by key named by x`, is no longer read when `xs` holds `[]`, so the check passes there, as [for.roots](../../spec/semantics.md#for) says, where it used to fail as `absent` with `$params.m is missing`.
- A sentence under a `for` line that reads the line's alias only as the `named by` key of `$params` or `$input` on the right of `is in`, like `count of zw whose k is in $params.m named by x is at most 5`, is left out on an empty list with `, if any,`, as it is in `ergo.rego`, so the check passes. It used to run before any item and fail on an item it couldn't decide. A count that never ran is no longer among the row's `inputs`. The part before such a key, like `$params.m` in `$params.m named by x`, is read with each item too, and not before any item, so it no longer fails the check on an empty list when it is missing or `null`.

- A `named by` key from the subject in a `whose … exists`, like `pr.key` in `count of tests whose $params.m named by pr.key exists is 0`, is read once, before any item, as with the other phrases ([tally.whose](../../spec/semantics.md#counts-and-sums-in-sentences), [for.roots](../../spec/semantics.md#for)). A key that is missing, `null` or isn't text fails the check, like `pr.key is {}, which isn't text`, even when the list is empty, with or without `, if any,`. It passed before. This changes the report of a policy that has one.
Expand Down
13 changes: 11 additions & 2 deletions engines/rust/engine/src/ops/key_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,18 @@ pub fn base_of(path: &Path, item_keys: bool, ctx: &Ctx) -> Option<(String, Strin
if !super::walk::reads_outside(path) && path.start != path::Start::Params {
return None;
}
if path.steps[at..].iter().any(|step| {
matches!(step, Step::Ref(key) | Step::Field(key) if super::for_line::path_reads_unbound_alias(key, ctx))
}) {
return None;
}
match &path.steps[at] {
Step::Ref(key) => {
if super::for_line::path_reads_unbound_alias(key, ctx) {
if key_flaw(key, ctx).is_none()
&& !(item_keys && subject_keys_then_an_items_key(&path.steps[at..], ctx))
{
return None;
}
key_flaw(key, ctx)?;
}
Step::Field(key)
if !item_keys
Expand Down Expand Up @@ -262,6 +268,9 @@ fn subject_keys_then_an_items_key(steps: &[Step], ctx: &Ctx) -> bool {
path::names_the_subject(&key.start)
&& matches!(path::resolve(step, ctx), Ok(path::Resolved::Key(_)))
}
Step::Ref(key) => {
!super::for_line::path_reads_unbound_alias(key, ctx) && key_flaw(key, ctx).is_none()
}
_ => false,
})
}
Expand Down
109 changes: 109 additions & 0 deletions engines/rust/engine/src/ops/key_base/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,112 @@ fn a_counted_item_clause_is_kept_even_when_its_text_is_a_base_problem() {
"w is 2; o is 5, which isn't an object; o is 5, which isn't an object; $params.k is missing"
);
}

#[test]
fn a_root_read_through_a_key_from_params_and_then_the_items_key_has_its_base_read_before_any_item()
{
let subject = json!({"tests": []});
let mut path = keyed(Start::Params, &["m"], "k");
path.steps.push(Step::Field(Box::new(Path {
start: Start::Own,
steps: vec![Step::Key("key".to_string())],
})));
let params = json!({"k": "a"});
let (name, text, cause) = base_of(&path, true, &ctx(&subject, &params, &[])).unwrap();
assert_eq!(
(name.as_str(), text.as_str(), cause),
("$params.m", "$params.m is missing", Cause::Absent)
);
let params = json!({"k": "a", "m": {}});
assert!(base_of(&path, true, &ctx(&subject, &params, &[])).is_none());
}

fn chain(steps: Vec<Step>) -> Path {
let mut all = vec![Step::Key("m".to_string())];
all.extend(steps);
Path {
start: Start::Params,
steps: all,
}
}

fn items_key() -> Step {
Step::Field(Box::new(Path {
start: Start::Own,
steps: vec![Step::Key("key".to_string())],
}))
}

fn alias_key() -> Step {
Step::Ref(Box::new(Path {
start: Start::Alias("x".to_string()),
steps: vec![],
}))
}

fn params_key() -> Step {
Step::Ref(Box::new(Path {
start: Start::Params,
steps: vec![Step::Key("k".to_string())],
}))
}

#[test]
fn a_root_read_through_the_items_key_and_then_an_unbound_line_alias_is_not_read_before_any_item() {
let subject = json!({"key": "a", "tests": []});
let params = json!({"k": "a"});
for steps in [
vec![items_key(), alias_key()],
vec![items_key(), params_key(), alias_key()],
vec![params_key(), items_key(), alias_key()],
] {
assert!(base_of(&chain(steps), true, &ctx(&subject, &params, &[])).is_none());
}
}

#[test]
fn a_root_read_through_a_key_from_params_and_the_items_key_takes_the_roots_cause_whatever_the_key()
{
let subject = json!({"key": "a", "tests": []});
let path = chain(vec![params_key(), items_key()]);
for (params, text, cause) in [
(
json!({"k": "a", "m": null}),
"$params.m is null",
Cause::Null,
),
(
json!({"k": "a", "m": 3}),
"$params.m is 3, which isn't an object",
Cause::Unusable,
),
(json!({"k": ""}), "$params.m is missing", Cause::Absent),
(json!({}), "$params.m is missing", Cause::Absent),
(json!({"k": null}), "$params.m is missing", Cause::Absent),
(json!({"k": 3}), "$params.m is missing", Cause::Absent),
] {
let (_, found, found_cause) = base_of(&path, true, &ctx(&subject, &params, &[])).unwrap();
assert_eq!((found.as_str(), found_cause), (text, cause));
}
}

#[test]
fn a_bound_line_alias_in_front_of_the_items_key_reads_the_root_before_any_item() {
let subject = json!({"key": "a", "tests": []});
let params = json!({"k": "a"});
let x = json!("a");
for steps in [
vec![alias_key(), items_key()],
vec![alias_key(), params_key(), items_key()],
vec![params_key(), alias_key(), items_key()],
vec![items_key(), alias_key()],
] {
let path = chain(steps.clone());
let (_, text, cause) = base_of(&path, true, &ctx(&subject, &params, &[("x", &x)])).unwrap();
assert_eq!(
(text.as_str(), cause),
("$params.m is missing", Cause::Absent)
);
assert!(base_of(&chain(steps), true, &ctx(&subject, &params, &[])).is_none());
}
}
12 changes: 12 additions & 0 deletions engines/rust/sentences/tests/oracle/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ pub const CHECK_OK: &[(&str, &str, &[&str], &[&str])] = &[
&[],
&[],
),
(
"count of tests whose v equals $params.m named by key named by x is 0",
"count of tests whose v equals $params.m named by key named by x is 0",
&[],
&[],
),
(
"count of tests whose v equals $params.m named by x named by key is 0",
"count of tests whose v equals $params.m named by x named by key is 0",
&[],
&[],
),
(
"every tests whose $input.m named by key is 1, if any, exists",
"every tests whose $input.m named by key is 1, if any, exists",
Expand Down
Loading
Loading