Skip to content

fix: condition CNF explosion for templates with nested condition references - #4455

Merged
kddejong merged 1 commit into
aws-cloudformation:mainfrom
kddejong:fix/issue/4406
Apr 10, 2026
Merged

kddejong merged 1 commit into
aws-cloudformation:mainfrom
kddejong:fix/issue/4406

Conversation

@kddejong

Copy link
Copy Markdown
Contributor

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 10 Fn::Equals each plus DeployLambdas = 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 Condition class, when encountering Condition: G1, recursively expanded G1's full boolean expression into the CNF. For DeployLambdas = Not(Or(G1, G2, G3, G4, G5)) where each Gi has 10 And(account, region) pairs, the resulting expression had 100 symbols and converting Not(Or(And(...), ...)) to CNF is exponential.

The same issue existed in the template-level Conditions class where build_true_cnf/build_false_cnf expanded the full expression on every call.

Fix

Give each condition name its own Symbol and express relationships as implication constraints:

  1. context/conditions/_condition.py: Condition: references use Symbol(name) as CNF instead of expanding
  2. context/conditions/_conditions.py: Switch to EncodedCNF with Equivalent(Symbol, expr) constraints added once at init. evolve() adds/negates simple Symbols
  3. conditions/_condition.py: ConditionNamed.build_cnf() returns the Symbol when available in params
  4. conditions/conditions.py: _build_cnf() creates condition-name Symbols with Implies constraints, excluding self-references to avoid circularity

Performance

Reproduction template from #4406: 20+ seconds (timeout) → 0.33 seconds

Fixes #4406

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
@codecov

codecov Bot commented Apr 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.23810% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.45%. Comparing base (0bbabc9) to head (7a21543).
⚠️ Report is 70 commits behind head on main.

Files with missing lines Patch % Lines
src/cfnlint/conditions/_condition.py 71.42% 1 Missing and 1 partial ⚠️
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     
Flag Coverage Δ
unittests 93.45% <95.23%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kddejong kddejong changed the title Fix condition CNF explosion for templates with nested condition references fix: condition CNF explosion for templates with nested condition references Apr 10, 2026
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.

cfn-lint performance issues on E1001

2 participants