You need to do a little home work. There was no formal written "agreement"... there were no negotiations... there was a press release by a prosecutor who decided not to pursue a case because it appeared weak. Another prosecutor decided that it was worth pursuing. That is about the extent of the situation. Just because one prosecutor decides not to pursue a case does not give anyone any where immunity from prosecution. That is ridiculous.
If Cosby was not a member of a protected class the Pennsylvania Supreme Court would have laughed their butts off at this "challenge". The Pennsylvania Supreme Court is a partisan body with five Democrats and one Republican. This decision was a travesty.
When the prosecutor publicly states that he had assured Cosby that he would not prosecute Cosby if he testified in the civil case proceedings, that is a contract. Whether it is written or not; it makes no difference.
Absent that public statement by the Prosecutor, Cosby and his attorney would have never agreed to testify or be deposed in the civil case.
If the Prosecutor had not made a public statement, Cosby and his attorney would have insisted on a written agreement to not prosecute.