Bill Cosby had his sexual assault conviction thrown out Wednesday — in a stunning ruling by Pennsylvania’s highest court that found a prosecutor’s decades-old agreement should have shielded him from criminal charges. The fallen funnyman, 83, is set to walk out of a Philadelphia-area prison a free man after serving more than two years of a three- to 10-year sentence. In its 79-page ruling, the State Supreme court found that an agreement with then-Montgomery County District Attorney Bruce Castor Jr. prevented Cosby from being charged for allegedly drugging and molesting Andrea Constand in 2004. Constand first reported the alleged encounter in 2015 to Castor, who decided not to pursue charges in the case. There was no evidence that the deal — which opened the door for Cosby to testify in Constand’s civil suit — was ever put in writing. That damning testimony was used, in part, to help convict Cosby. In 2019, Castor’s successor, Kevin Steele, opted to file charges against Cosby — just days before the 12-year statute of limitations expired. Wednesday’s ruling said the prosecution was obligated to uphold Castor’s promise not to charge the comedian.