user47827: What is a sane Miniscript? How does it differ to a valid Miniscript?
This was asked during a Bitcoin Core PR review club on Miniscript hosted by stickies-v on May 18th 2022.

user47827: A valid Miniscript is any correctly typed Miniscript.
A sane Miniscript needs to be valid, consensus and
standardness-compliant (e.g. number of operations and script size),
have non-malleable solutions, not mix different timelock units on a particular branch of the script and not have duplicate keys.
From miniscript.h:
//! Whether the apparent policy of this node matches its script semantics. Doesn't guarantee it is a safe script on its own.
bool IsSaneSubexpression() const { return ValidSatisfactions() && IsNonMalleable() && CheckTimeLocksMix() && CheckDuplicateKey(); }

//! Check whether this node is safe as a script on its own.
bool IsSane() const { return IsValidTopLevel() && IsSaneSubexpression() && NeedsSignature(); }

A Miniscript is sane if it is of the form X OR Y with X containing a block check and Y containing a time check but X AND Y would be unspendable. e.g. (A signs) OR ((B signs) AND (timelock) AND (heightlock)) would not be considered sane. Even though it can be spent by A, its apparent policy doesn't match the actual script (whose actual policy is just "A signs", because the other branch is unusable)
The Node::IsSane() method has the docstring "Whether the apparent policy of this node matches its script semantics."
Thanks to participants for answering this at the Bitcoin Core PR review club.

47827: <p>What is a <strong>sane</strong> Miniscript? How does it differ to a <strong>valid</strong> Miniscript?</p>
<p>This was asked during a Bitcoin Core PR <a href="https://bitcoincore.reviews/24148" rel="nofollow noreferrer">review club</a> on Miniscript hosted by stickies-v on May 18th 2022.</p>
 47827: <p>A <strong>valid</strong> Miniscript is any correctly typed Miniscript.</p>
<p>A <strong>sane</strong> Miniscript needs to be <strong>valid</strong>, consensus and
standardness-compliant (e.g. number of operations and script size),
have non-malleable solutions, not mix different timelock units on a particular branch of the script and not have duplicate keys.</p>
<p>From <a href="https://github.com/bitcoin/bitcoin/blob/master/src/script/miniscript.h#L866" rel="nofollow noreferrer">miniscript.h</a>:</p>
<pre><code>//! Whether the apparent policy of this node matches its script semantics. Doesn't guarantee it is a safe script on its own.
bool IsSaneSubexpression() const { return ValidSatisfactions() &amp;&amp; IsNonMalleable() &amp;&amp; CheckTimeLocksMix() &amp;&amp; CheckDuplicateKey(); }

//! Check whether this node is safe as a script on its own.
bool IsSane() const { return IsValidTopLevel() &amp;&amp; IsSaneSubexpression() &amp;&amp; NeedsSignature(); }
</code></pre>
<p>A Miniscript is sane if it is of the form X OR Y with X containing a block check and Y containing a time check but X AND Y would be unspendable. e.g. <code>(A signs) OR ((B signs) AND (timelock) AND (heightlock))</code> would not be considered sane. Even though it can be spent by A, its apparent policy doesn't match the actual script (whose actual policy is just &quot;A signs&quot;, because the other branch is unusable)</p>
<p>The <code>Node::IsSane()</code> method has the docstring &quot;Whether the apparent policy of this node matches its script semantics.&quot;</p>
<p>Thanks to participants for answering this at the <a href="https://bitcoincore.reviews/24148" rel="nofollow noreferrer">Bitcoin Core PR review club</a>.</p>
