Repository navigation
fix: condition CNF explosion for templates with nested condition references - #4455
Merged
Merged
Conversation
When conditions reference other conditions via !Condition, the CNF expression was recursively expanded into the full boolean tree. For templates with many conditions (e.g., 5 groups of 10 Fn::Equals each plus a DeployLambdas = Not(Or(G1..G5))), this created massive expressions where sympy's CNF conversion (_or()) exploded exponentially. Fix: Give each condition name its own Symbol and express relationships as implication constraints added once to the EncodedCNF. Nested !Condition references now resolve to the simple Symbol instead of expanding the full expression. The SAT solver handles implications through the constraints. Both the template-level (conditions/conditions.py) and context-level (context/conditions/_conditions.py) classes are updated. Performance on reproduction template: 20+ seconds -> 0.33 seconds. Fixes aws-cloudformation#4406
kddejong
force-pushed
the
fix/issue/4406
branch
from
April 10, 2026 17:36
6bff194 to
7a21543
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4455 +/- ##
==========================================
- Coverage 93.54% 93.45% -0.09%
==========================================
Files 418 449 +31
Lines 14145 14844 +699
Branches 2817 2889 +72
==========================================
+ Hits 13232 13873 +641
- Misses 567 594 +27
- Partials 346 377 +31
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
satyakigh
approved these changes
Apr 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When conditions reference other conditions via
!Condition, the CNF expression was recursively expanded into the full boolean tree. For templates with many conditions (e.g., 5 groups of 10Fn::Equalseach plusDeployLambdas = Not(Or(G1..G5))), this created massive expressions where sympy's CNF conversion (_or()) exploded exponentially — consuming 85%+ of runtime and causing cfn-lint to hang indefinitely.Root Cause
The context-level
Conditionclass, when encounteringCondition: G1, recursively expanded G1's full boolean expression into the CNF. ForDeployLambdas = Not(Or(G1, G2, G3, G4, G5))where each Gi has 10And(account, region)pairs, the resulting expression had 100 symbols and convertingNot(Or(And(...), ...))to CNF is exponential.The same issue existed in the template-level
Conditionsclass wherebuild_true_cnf/build_false_cnfexpanded the full expression on every call.Fix
Give each condition name its own Symbol and express relationships as implication constraints:
context/conditions/_condition.py:Condition:references useSymbol(name)as CNF instead of expandingcontext/conditions/_conditions.py: Switch toEncodedCNFwithEquivalent(Symbol, expr)constraints added once at init.evolve()adds/negates simple Symbolsconditions/_condition.py:ConditionNamed.build_cnf()returns the Symbol when available in paramsconditions/conditions.py:_build_cnf()creates condition-name Symbols withImpliesconstraints, excluding self-references to avoid circularityPerformance
Reproduction template from #4406: 20+ seconds (timeout) → 0.33 seconds
Fixes #4406