(a) Terminates in a conviction upon a plea of guilty; or
(b) Proceeds to the trial stage and a jury has been impaneled and sworn or, in the case of a trial by the court without a jury, a witness is sworn. 2. Despite the occurrence of proceedings specified in subdivision one, a person is not deemed to have been prosecuted for an offense, within the meaning of section 40.20, when:
(a) Such prosecution occurred in a court which lacked jurisdiction over the defendant or the offense; or
(b) Such prosecution was for a lesser offense than could have been charged under the facts of the case, and the prosecution was procured by the defendant, without the knowledge of the appropriate prosecutor, for the purpose of avoiding prosecution for a greater offense. 3. Despite the occurrence of proceedings specified in subdivision one, if such proceedings are subsequently nullified by a court order which restores the action to its pre-pleading status or which directs a new trial of the same accusatory instrument, the nullified proceedings do not bar further prosecution of such offense under the same accusatory instrument. 4. Despite the occurrence of proceedings specified in subdivision one, if such proceedings are subsequently nullified by a court order which dismisses the accusatory instrument but authorizes the people to obtain a new accusatory instrument charging the same offense or an offense based upon the same conduct, the nullified proceedings do not bar further prosecution of such offense under any new accusatory instrument obtained pursuant to such court order or authorization.