A former prosecutor in the US state of Pennsylvania has said his decision not to charge actor Bill Cosby with sex crimes in 2005 is "binding".
In December, a new prosecutor reversed course and charged Mr Cosby with sexually assaulting a woman in 2004.
Former prosecutor Bruce Castor testified on Tuesday for Mr Cosby's defence team who argued that the criminal case should be dismissed.
Dozens of women have said the actor assaulted them over a span of decades.
Mr Cosby has said the encounters were consensual and has denied any wrongdoing.
Also on Tuesday, Mr Cosby was ordered give a deposition in a California civil case.
Mr Cosby is accused of drugging and fondling a former Temple University employee, Andrea Constand, in 2004. He could go to prison for 10 years if convicted.
Mr Castor said he found flaws in the case in 2005, making the decision not to press charges for the state, to last in perpetuity.
He also testified that he made an agreement with Mr Cosby not prosecute, but the current prosecutor, Kevin Steele, said there is no record of the deal.
Mr Cosby testified in a lawsuit brought on by Andrea Constand after the agreement was made.
"Cosby would've had to have been nuts to say those things if there was any chance he could've been prosecuted," Mr Castor said .
"I did not believe it was just to go forward with the criminal prosecution, but I wanted there to be some measure of justice" for Ms Constand, he said.
Documents from her lawsuit against the star were sealed until last summer, when damaging testimony was revealed as more women came forward.
The 12-year statute of limitations was only days away from expiring in January.
Mr Steele said Mr Cosby needs an immunity agreement for the case to be tossed out.
Mr Castor said he did not have a formal agreement with Mr Cosby's former lawyer Walter Phillips, who is now dead, that Mr Cosby would testify in Ms Constand's case.
"Mr Phillips never agreed to do anything in exchange for Mr Cosby not being prosecuted," Mr Castor said. "I thought making Mr Cosby pay money was the best I was going to be able to set the stage for."
Also on Tuesday, a California judge ordered Mr Cosby to give a second deposition in a case of a woman who has accused him of sexually assaulting her at the Playboy Mansion in 1974 when she was 15.
He had given a deposition in that case last October but the woman's lawyer has said he did not answer all questions.