Skip to content

Aggregate semantics in Policy Compiler - #1408

Open
TristonianJones wants to merge 3 commits into
cel-expr:masterfrom
TristonianJones:aggregate-policy
Open

Aggregate semantics in Policy Compiler#1408
TristonianJones wants to merge 3 commits into
cel-expr:masterfrom
TristonianJones:aggregate-policy

Conversation

@TristonianJones

@TristonianJones TristonianJones commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

Add aggregate semantics into Policy Compiler.

The CEL policy compiler initially started as first-match policy compilation, ensuring at
most one result would be produced from a policy expression evaluation. With aggregate
semantics, multiple outputs can be produced on a single pass. For those familiar with K8s
validating admission policies, the effect is similar.

Ported from cel-expr/cel-java/pull/1052

====

Aggregate walks through all matching rules (including nested ones) and appends them into a list:

rule:
  aggregate:
    - condition: "true"
      output: "'FOO'"
    - condition: "true"
      output: "'BAR'"

# Output: ['FOO', 'BAR']

Few noteworthy design decisions below. All examples assume all conditions matched:

  1. For usability reasons, subrules under an aggregate ancestor will always have their lists flattened:
name: aggregate_flat_flattening_example
rule:
  aggregate:
    - rule:
        match:
          - condition: "resource.is_admin == true"
            output: "['GDPR_STANDARD', 'EU_B2C_NOTICE']"
    - condition: "true"
      output: "'FALLBACK'"
# Output: ['GDPR_STANDARD', 'EU_B2C_NOTICE', 'FALLBACK']
  1. Base case of an aggregate rule is an empty list. Nested conditional rules within an aggregate rule which outputs optional.none() are pruned (except in cases where policy output explicitly emits an optional.none()):
name: optional_pruning_example
rule:
  aggregate:
    - rule:
        match:
          - condition: "1 == 2"
            output: "'EU_NOTICE'"
    - condition: "true"
      output "'ALWAYS'"

# Output: ['ALWAYS']
name: explicit_optional_none_example
rule:
  aggregate:
    - rule:
        match:
          - condition: "resource.is_b2c == true"
            output: "optional.none()"   # Explicitly authored by user

    - condition: "true"
      output: "optional.of('ALWAYS')"

# Output: [optional.none(), optional.of('ALWAYS')]

Note: nesting aggregate clauses is currently not allowed, and will result in a compilation error.

@TristonianJones
TristonianJones requested a review from l46kok August 15, 2026 00:31
Comment thread policy/composer.go Outdated
}
}

if output == nil {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@l46kok - This is a bit different from cel-java because output isn't initialized to a base step in order to avoid superfluous + [] appearing in the output expression. I believe you handle this with the constant folding and CSE in Java, but constant-folding isn't turned on by default for policy composition in order to preserve the round-trip to source metadata for coverage purposes.

Comment thread policy/compiler_test.go
}
}

func TestRuleComposerError(t *testing.T) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

These moved into composer_test.go

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.

1 participant