Repository navigation
fix(conditions): avoid exponential CNF when registering conditions - #4742
Merged
kddejong merged 3 commits intoOct 20, 2026
Merged
Conversation
Both condition engines added `Symbol(name) <-> expression` to the EncodedCNF directly, and sympy converts that by distributing And over Or. An Fn::Or of n Fn::And pairs became 2^n clauses, so a 20-pair condition took over a minute to lint. Encode the equivalence with Tseitin-style auxiliary variables instead: each And/Or subterm gets its own variable defined by a full equivalence, keeping the clause count linear. The auxiliary variables are fully determined by the original ones, so satisfiability and implication queries over condition and equals symbols are unchanged. Fixes aws-cloudformation#4737
add_equivalence gave every And/Or subterm a fresh Dummy, including the outermost one, and then tied the condition symbol to it with a two clause Equivalent. For the common one-level condition (an Fn::And or Fn::Or of equals) that is one extra variable and two extra clauses per condition, which showed up as ~25% more time in build_scenarios on condition-heavy templates. Use the condition symbol itself as the variable of the outermost And/Or so such conditions encode to exactly the clauses Equivalent produced before.
kddejong
approved these changes
Oct 20, 2026
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4742 +/- ##
==========================================
- Coverage 94.72% 94.71% -0.01%
==========================================
Files 439 439
Lines 15858 15886 +28
Branches 3067 3076 +9
==========================================
+ Hits 15021 15046 +25
- Misses 457 458 +1
- Partials 380 382 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Oct 20, 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.
Issue #, if available: #4737
Description of changes:
Both condition engines register each named condition as
Symbol(name) <-> expressionby handing the whole equivalence toEncodedCNF.add_prop. sympy converts that to clauses by distributing And over Or, so anFn::Orof nFn::Andpairs becomes 2^n clauses. Every latercopy()/satisfiable()then works on that clause set, and since #4455 this happens at template load whether or not the condition is used.This adds
add_equivalence()incfnlint/conditions/_utils.pyand uses it in bothconditions/conditions.pyandcontext/conditions/_conditions.py. It Tseitin-encodes the expression, which keeps the clause count linear in the size of the expression:Why results are unchanged: auxiliary variables are fully determined by the original variables. Later
add_propcalls only reference condition symbols and equals hashes. No caller inspects models. So everysatisfiablequery behindbuild_scenarios,check_implies,build_scenerios_on_region,evolveandis_reachablereturns the same answer.Timings (cfn-lint wall clock):
@hoganbr-amzn independently tried the same per-node encoding and reported that the 421 KB template that surfaced the bug went from more than an hour to 12s, with the same findings.
Testing:
TestAddEquivalencecompares the encoding against directEquivalentover every assignment for a set of And/Or/Not/Implies/Equivalent/Xor expressions. It also bounds the clause count for an Or of 40 Ands and checks that a single-level And/Or adds no auxiliary variable.build_scenarios,check_implies,build_scenerios_on_region,satisfiable,evolveandis_reachable. Full cfn-lint output overtest/fixtures/templates/goodandbadwas identical.pytest testpasses. Snapshot and rule test time is unchanged.Out of scope: template
Rulesassertions still go throughadd_propdirectly, so an Or-of-And inside a rule assertion can still expand. That could be a follow-up.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.