Skip to content

Validator: skip request environments the policy scope rules out - #2609

Open
dcosson wants to merge 6 commits into
cedar-policy:mainfrom
dcosson:validator-pruning
Open

dcosson wants to merge 6 commits into
cedar-policy:mainfrom
dcosson:validator-pruning

Conversation

@dcosson

@dcosson dcosson commented Oct 13, 2026 •

Copy link
Copy Markdown

Description of changes

typecheck_policy typechecks the full policy condition in every request environment of the schema, that is every (principal type, action, resource type) the schema allows. A policy scoped to one action and one principal type still pays for all of them. A template slot constrained by ==, in or is ... in is also linked to every candidate type of its variable, and each link is typechecked in every environment again. On a large schema, validation time grows with actions × principal types × resource types × links, nearly all of it spent on environments the policy can never apply to.

This PR removes that work in two steps.

  1. Skip environments the scope rules out. Before typechecking a policy in an environment, check its scope against the schema hierarchy, as admitsAll in the companion Lean proof does. If the scope cannot match the environment's principal type, action or resource type, skip it. A template slot is linked once, to its variable's own type.
  2. Cache the check by type. The principal check depends only on the principal type, the action check only on the action, and the resource check only on the resource type. Each result is computed once and reused, and whole groups of environments are skipped at once.

typecheck_by_request_env and the other per-environment entry points are unchanged. There is no API change.

Why the pruning is exact

The companion cedar-spec PR (cedar-policy/cedar-spec#1046) proves this against the Lean validator, with no sorry:

  • filtered_environments_exact_of_false: dropping any set of environments in which the policy types as false returns exactly the same ValidationResult. action_filter_exact, principal_filter_exact and resource_filter_exact instantiate it for each scope constraint, and admitted_environments_exact applies all three together.
  • The Lean spec doesn't model template validation: it validates a template only after linking it. The cedar-spec PR adds a Lean template validator that does what Rust's does, typechecking the template in each environment once per candidate slot type. template_filter_exact shows that any set of slot-type candidates which keeps each environment's own principal and resource types gives the same outcome. rust_candidates_exact (today's possible_slot_links) and one_link_exact (this change) are instances, and one_link_admitted_exact covers one-link together with environment filtering.
  • template_type_error_iff and template_sound: a template has a type error exactly when some valid link has one in some environment.

Benchmark

Synthetic schemas with P principal types, R resource types and A actions (P × R × A environments), with narrowly scoped static policies and templates, strict mode, median of repeated runs on one machine:

Environments Policies + templates Before After
250 50 + 10 5.27 ms 0.32 ms
2,500 100 + 20 81.8 ms 1.30 ms
20,000 200 + 40 1.24 s 13.7 ms
80,000 200 + 40 4.93 s 56.8 ms

With 20 unscoped policies (nothing to prune): 68.1 → 62.9 ms at 2,500 environments and 547 → 505 ms at 20,000.

Testing

  • cedar-policy-core/src/validator/typecheck/test/pruning.rs typechecks about 12,000 generated policies and templates with and without pruning in strict, permissive and partial modes, and requires identical results. The corpus covers every slot constraint kind, unknown entity and action literals and mismatched scopes. It also checks that the corpus makes each scope constraint fail, type as false and type as something else.
  • Mutants of the pruning code that also skip an environment when the principal, action or resource constraint fails to typecheck are caught by those tests.
  • DRT: validation-drt, validation-drt-type-directed and level-validation-drt ran 31 minutes each against this branch, about 1.9M–2.4M runs per target, with no mismatches.
  • cargo test -p cedar-policy-core validator, cargo test -p cedar-policy, cargo clippy --all-features and cargo fmt --check pass.

Gap: DRT compares strict-mode pass/fail only. Permissive and partial modes, and the exact error lists, are covered by the differential tests above, not by DRT. DRT also skips the "Rust rejects, Lean accepts" direction whenever the input has templates (cedar-policy/cedar-spec#945), so template pruning rests on the Lean proof and the unit corpus.

Issue #, if available

Closes #2608

Checklist for requesting a review

The change in this PR is (choose one, and delete the other options):

  • A change "invisible" to users (e.g., documentation, changes to "internal" crates like cedar-policy-core, cedar-validator, etc.)

I confirm that this PR (choose one, and delete the other options):

  • Updates the "Unreleased" section of the CHANGELOG with a description of my change (required for major/minor version bumps).

I confirm that cedar-spec (choose one, and delete the other options):

I confirm that docs.cedarpolicy.com (choose one, and delete the other options):

  • Does not require updates because my change does not impact the Cedar language specification.

typecheck_policy typechecked the full condition in every linked request
environment. A policy scoped to one action and one principal type still
paid for every action x principal x resource environment in the schema,
and a template slot constrained by in or is-in was linked to every
ancestor type of its variable.

Before typechecking the condition in an environment, typecheck the scope
constraints alone. When they type as false with no errors, the full
condition does too, because && short-circuits on a false operand, so the
environment cannot change the errors, the return value or the
impossible-policy warning. Slots constrained by ==, in or is-in now link
once, to the variable's own type: the slot occurs only in its scope
constraint, which types the same way for every candidate link.

typecheck_by_request_env and the other per-environment entry points are
unchanged. The new pruning tests typecheck about 12,000 policies and
templates (every slot constraint kind, unknown entity and action literals,
mismatched scopes) with and without pruning in strict, permissive and
partial modes and require identical results.

Signed-off-by: Danny Cosson <danny@zocomputer.com>
The first pruning pass still typechecked the scope prefix in every
request environment, so validation stayed linear in actions x principal
types x resource types with a large constant: only 2.3x faster than
before on an 80,000-environment schema.

A scope constraint sets no capabilities, and its type depends only on
its own variable's type (and a slot's link, which is now fixed by that
type). So typecheck the principal constraint once per principal type,
the action constraint once per action and the resource constraint once
per resource type, and fold them in the order the condition's && chain
evaluates them. The schema's environments are grouped by action and
then principal, so a group the action or principal rules out is skipped
without linking or typechecking any of its environments.

Templates whose slots would need more than one link (a slot under a
bare is, which the grammar does not allow) take the plain path.

Benchmark, 200 narrow policies and 40 templates, strict mode:
  80,000 envs: 22.1 s before, 314 ms after
  20,000 envs:  5.5 s before,  78 ms after
Twenty unscoped policies, nothing to prune: no slowdown.

The pruning test now also checks the corpus makes each scope constraint
fail, type as false and type as something else, and mutants that skip
on a failing principal, action or resource constraint are caught.

Signed-off-by: Danny Cosson <danny@zocomputer.com>
Signed-off-by: Danny Cosson <danny@zocomputer.com>
@john-h-kastner-aws

Copy link
Copy Markdown
Contributor

Looks promising, thanks!

I've thought about a similar idea in the past but didn't follow up on it because I didn't expect pruning to provide much benefit over what the existing short-circuiting does. Surely it saves some work, but we would already avoid typechecking inside deep policies if the action condition allows us to short-circuit.

Can you explain what is is that makes my intuition wrong here?

@github-actions

Copy link
Copy Markdown
Contributor

Coverage Report

Head Commit: cd75b0b82d45d13e93c5d32335f36ab700c4c43f

Base Commit: 56c9706aeb342d748170d5787e142f79480f972d

Download the full coverage report.

Coverage of Added or Modified Lines of Rust Code

Required coverage: 80.00%

Actual coverage: 96.18%

Status: PASSED ✅

Details
File Status Covered Coverage Missed Lines
cedar-policy-core/src/validator/typecheck.rs 🟢 126/131 96.18% 251, 460, 462, 518, 547

Coverage of All Lines of Rust Code

Required coverage: 80.00%

Actual coverage: 88.26%

Status: PASSED ✅

Details
Package Status Covered Coverage Base Coverage
cedar-language-server 🟢 4734/5122 92.42% 92.42%
cedar-policy 🟢 4873/6069 80.29% 80.29%
cedar-policy-cli 🟡 1317/1697 77.61% 77.61%
cedar-policy-core 🟢 25158/28396 88.60% 88.54%
cedar-policy-formatter 🟢 914/1088 84.01% 84.01%
cedar-policy-symcc 🟢 7134/7590 93.99% 93.99%
cedar-wasm 🔴 0/40 0.00% 0.00%

@dcosson

dcosson commented Oct 13, 2026 •

Copy link
Copy Markdown
Author

Thanks.

I think the intuition here is mostly the combinatorial size. For P principal types, A actions, R resource types there are P x A x R environments to loop through and single_env_typechecking gets called for each one. Even though that check itself short-circuits internally it still checks one or more of the p, a, or r conditions plus it does some allocations for each environment

We can instead prune the environments first with P + A + R checks and then we only have to do full checks on the remaining environments. And even better each of these pruning checks is faster than the full typecheck too because it skips the body and also only checks one of the 3 scope expressions. In many cases this could just leave 1 remaining environment, if the scope is pretty specific. In the worst case where nothing can be pruned it means we're still running P+A+R + PxAxR checks, but the additive ones are fast and at larger values this approaches just PxAxR so I can't really measure any slowdown from this even on contrived examples.

@dcosson

dcosson commented Oct 13, 2026

Copy link
Copy Markdown
Author

And then there's a further benefit for template policies, because each environment we prune doesn't just skip one full typecheck, it skips one for each possible slot link.

And this PR also includes an additional optimization here where for the remaining environments in a template policy, we only do the linking once instead of linking again for each possible slot type.

@dcosson

dcosson commented Oct 19, 2026

Copy link
Copy Markdown
Author

@john-h-kastner-aws just checking in, any more thoughts on this?

@dcosson

dcosson commented Nov 3, 2026

Copy link
Copy Markdown
Author

@john-h-kastner-aws hey John, what can I do to get this reviewed and merged in?

This is a pretty massive improvement to perf. It passes all the tests, it adds to the proofs to ensure it's sound. Anything else that is missing from your perspective?

@dcosson

dcosson commented Nov 5, 2026

Copy link
Copy Markdown
Author

Hey team, would really appreciate some feedback on what the holdup is. Happy to make any changes needed to get this over the finish line.

@victornicolet victornicolet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for the contribution.

I haven't finished reviewing this PR, but I'm noting that the structure between Lean and Rust looks quite different. I wonder if it could be addressed before diving deeper.

request_env: &RequestEnv<'_>,
policy_id: &PolicyID,
constraint: &Expr,
) -> ScopePart {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The rust scope_part and the Lean counterpart (action_scope_admits and friends) seem quite different. Here, the scope_part calls the typechecker, whereas the Lean implementation is more syntactic. I think the proofs and Rust implementation should match better.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed, fixed. scope_part is gone and the filter no longer calls the typechecker. Each Lean definition now has one Rust function with the same name in snake case, such as scopeAdmitsType → scope_admits_type.

.or_insert_with(|| self.scope_part(&first_env, policy_id, &scope.principal));
let action = match principal {
ScopePart::False => continue,
ScopePart::Fails => ScopePart::Fails,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The Lean impl doesn't have the failure case, why was this needed? I have difficulty mapping the Lean spec with the Rust implementation, the structure seems quite different.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It was for undeclared scope literals like Nope::"x", which Lean rejects in checkEntities before filtering. It's replaced by one per-policy check, scope_entities_valid, that turns pruning off for these.

Review-Ref: concierge-sea, 20261106-152523-02a84fc8
Review-Round: 1
Review-Disposition: 5/5 incorporated, 0 deferred
@dcosson

dcosson commented Nov 7, 2026 •

Copy link
Copy Markdown
Author

Thanks for the review, the Rust filter should now be a closer translation to the Lean.

I added new measurement numbers as well, I lost the previous test script I had used so rewrote it this time with what seem to be smaller example policies so both main & the branch are much faster. This PR branch is still about the same factor faster than main though, nearly 100x for the large 80k environments scenario.

Move the CHANGELOG entry into the current Unreleased section, since the
section it was in has been released. Handle the new
ValidatorEntityTypeKind::External variant in scope_entities_valid the same
way as Standard, matching the literal checks in rbac.rs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Danny Cosson <danny@zocomputer.com>

This branch has not been deployed

No deployments
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.

Validation time grows with every request environment, even for narrowly scoped policies

3 participants