There's one crucial flaw. What is and is not covered by immunity can only be decided by a court, after the fact.
Intimidating the court into ruling in your favor with guns to their heads is an official act until the court says it isn't official, but the court can't say it isn't offical, because they've got guns to their heads.
That crucial flaw is the fundamental structure of our legal system. It's why case law is a valued and necessary part of our legal doctrine. Humans are bad at predicting the future, and attempts to write prescient laws often end with significant loopholes that must be corrected after the fact.
That this system fails to work when it's axioms are ignored (i.e. in the stated case of a coup) cannot be construed as a failure of common law or an indictment on its 950+ years of success. Such act would be a failure of the Executive solely.
The founder's stated intent for resolving such a situation is why we have the 2nd amendment.
Intimidating the court into ruling in your favor with guns to their heads is an official act until the court says it isn't official, but the court can't say it isn't offical, because they've got guns to their heads.