Repository navigation
Conversation
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>
|
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? |
Coverage ReportHead Commit: Base Commit: Download the full coverage report. Coverage of Added or Modified Lines of Rust CodeRequired coverage: 80.00% Actual coverage: 96.18% Status: PASSED ✅ Details
Coverage of All Lines of Rust CodeRequired coverage: 80.00% Actual coverage: 88.26% Status: PASSED ✅ Details
|
|
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. |
|
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. |
|
@john-h-kastner-aws just checking in, any more thoughts on this? |
|
@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? |
|
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
left a comment
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
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>
Description of changes
typecheck_policytypechecks 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==,inoris ... inis 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.
admitsAllin 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.typecheck_by_request_envand 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 asfalsereturns exactly the sameValidationResult.action_filter_exact,principal_filter_exactandresource_filter_exactinstantiate it for each scope constraint, andadmitted_environments_exactapplies all three together.template_filter_exactshows 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'spossible_slot_links) andone_link_exact(this change) are instances, andone_link_admitted_exactcovers one-link together with environment filtering.template_type_error_iffandtemplate_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:
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.rstypechecks 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 asfalseand type as something else.validation-drt,validation-drt-type-directedandlevel-validation-drtran 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-featuresandcargo fmt --checkpass.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):
cedar-policy-core,cedar-validator, etc.)I confirm that this PR (choose one, and delete the other options):
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):