The secret 2005 deal that sunk Bill Cosby’s sexual assault conviction this week was never put in writing. Its true intent is known only to one man — former Montgomery County District Attorney Bruce L. Castor Jr.

He didn’t tell his staff about it. Cosby’s lawyer at the time has since died. And lawyers for Cosby’s accuser, Andrea Constand, say he never informed them, either.

As the former top prosecutor explains it now, he agreed 16 years ago that Cosby would never face charges in connection with the alleged 2004 assault if he testified in a civil case she filed against him. And in its decision this week to free Cosby from prison, the Pennsylvania Supreme Court took Castor at his word.

But Castor’s own descriptions of the agreement — what it meant and whether it forever protected Cosby from prosecution — have shifted significantly over time.

In fact, just months before the Montgomery County District Attorney’s Office filed the case that was tossed on Wednesday, Castor said he was open to prosecuting Cosby himself.

“There is absolutely nothing wrong with the DA’s office reviewing the Cosby case based on new evidence,” he said in a 2015 interview with The Inquirer, a decade after the deal he now says barred such a move.

That statement came amid a contentious 2015 election battle pitting Castor, vying for his old seat against Kevin R. Steele, who would later be elected to the job.

At the time, dozens of women were coming forward for the first time to accuse Cosby of decades-old sexual misconduct, and Steele criticized Castor’s previous handling of the Cosby case.

“I have said repeatedly and for months” Castor said, “that if I ever get the opportunity where I get the power to review the investigation into Cosby, I would do so.”

2005: Castor opts not to charge Cosby

In 2005, Constand, a young Temple University sports administrator, had reported that she had been drugged and assaulted by the entertainer.

Castor, in opting not to pursue a case, announced his decision in a news release that cited “insufficient, credible and admissible evidence.”

That release — the only written record of Castor’s decision — did not mention a deal with Cosby or his lawyer, nor did it mention Cosby’s potential civil testimony.

And he concluded it with a warning: “District Attorney Castor cautions all parties to this matter that he will reconsider this decision should the need arise.”

After Constand sued Cosby in civil court, the comedian sat for depositions in which he testified about his past use of drugs in sexual encounters with women and eventually settled with Constand for $3.4 million.

2015: Castor says he could ‘revisit’ the issue of prosecution

As the 2015 campaign heated up, Castor said he’d like to review Cosby’s testimony in that civil suit to see if he could use it to prosecute the entertainer himself.

“I have been wrestling in my mind on ways to try to figure out how to use the new info about the deposition to create a favorable atmosphere for a prosecution,” he told The Inquirer at the time. He made no mention, as he would in the years to come, that Cosby was forever shielded from prosecution.

In September 2015, The Inquirer reported that Castor’s successor, Risa Vetri Ferman, had reopened the Constand investigation. And in response, Castor referenced the line in his 2005 press release that he might ‘reconsider’ the issue.

“I put in there that if any evidence surfaced that was admissible then I would revisit the issue, and that evidently is what the DA is doing,” he said.

2015: Castor says ‘a prosecution is not precluded’

But as Ferman’s reopened investigation of Cosby proceeded, Castor privately tried to warn her off the case.

In a series of September 2015 emails, he mentioned for the first time an explicit arrangement he said he’d struck with Cosby and his lawyers that he said would preclude using any of the comedian’s deposition testimony against him.

“With the agreement of the defense lawyer and Andrea’s lawyers,” Castor wrote, “I intentionally and specifically bound the Commonwealth that there would be no state prosecution of Cosby in order to remove from him the ability to claim his Fifth Amendment protection against self incrimination thus forcing him to sit for a deposition [in Constand’s civil suit] under oath.”

Ferman, who had been Castor’s first assistant in 2005 and the point of contact for Constand and her lawyers in the Cosby investigation, replied that she did not recall any such agreement. She said neither Cosby’s nor Constand’s attorneys had evidence of an explicit agreement, either.

» READ MORE: Bill Cosby is already plotting his post-prison comeback. But is America ready to welcome him?

Castor’s emails, however, didn’t argue that his actions had barred prosecution altogether.

“Naturally, If a prosecution could be made out without using what Cosby said [in his deposition], I believe and continue to believe that a prosecution is not precluded,” he wrote on Sept. 25.

About 10 minutes later, he followed up with another note: “I never agreed we would not prosecute Cosby. I only agreed … that anything he said would not be used to advance a prosecution.”

In the months that followed, Steele defeated Castor in the District Attorney’s race and Ferman, in the final weeks of her term in office, charged Cosby with sexually assaulting Constand more than a decade after she first came forward.

2016: Castor says ‘Mr. Cosby was not getting prosecuted at all — ever’

By the time of Cosby’s first court appearances, Castor’s description of the 2005 deal had shifted yet again.

The first public reference to the deal constituting a permanent and binding “non-prosecution” agreement emerged in a filing from Cosby’s defense weeks after his arrest. And, at a hearing on the issue, the defense called Castor as its star witness.

The former district attorney testified in February 2016 — much as he’d told Ferman the year before — that when he opted not to charge Cosby in 2005, he hoped to compel him to sit for his civil deposition. But this time, he maintained the decision he’d reached a decade ago would bar the current case against Cosby from moving forward.

Contrary to what he’d said previously, Castor asserted that the line in his 2005 press release saying he would “reconsider this decision should the need arise” did not refer to his choice not to file a case — but rather his decision not to speak further about the reasons behind that decision.

» READ MORE: Bill Cosby’s release from prison again shines a light on Bruce Castor. And Castor feels ‘vindicated.’

“Mr. Cosby was not getting prosecuted at all — ever — as far as I was concerned,” he testified.

He described the the arrangement as legally binding on both Cosby and future prosecutors — even though he acknowledged that he made the decision on his own and that the comedian and his lawyers had “never agreed to anything.”

Steele, leading the prosecution team, scoffed, calling Castor’s new assessment of the deal’s parameters “revisionist history” and an eleventh-hour attempt by his political rival to shut down the case.

“A secret agreement that permits a wealthy defendant to buy his way out of a criminal case isn’t right,” Steele said.

Ultimately, Montgomery County Court Judge Steven T. O’Neill found Castor’s statements on the matter inconsistent and determined that he was not a credible witness — an astounding assessment by a court of a former district attorney.

Cosby’s prosecution was allowed to proceed. O’Neill later permitted portions of Cosby’s deposition to be used in two trials — one that ended in a hung jury and mistrial in 2017, and a retrial 10 months later that ended in Cosby’s conviction and three-to-10-year prison sentence.

2021: Pa. Supreme Court notes Castor’s ‘odd and ever-shifting explanations’

In deciding to overturn Cosby’s conviction Wednesday, six of the state Supreme Court’s seven justices agreed that whatever arrangement Castor had struck with Cosby in 2005 barred prosecutors from using the comedian’s deposition testimony against him at trial.

But Castor’s conflicting statements over the years sowed division even within that majority.

Four of the justices — led by Justice David N. Wecht, who wrote the court’s opinion — took Castor at his word. They concluded that no matter how dubious the former district attorney’s legal reasoning might have been, what mattered was what Cosby believed at the time he gave up his Fifth Amendment protections and agreed to testify in Constand’s civil case.

And that, they said, meant Steele’s office had to stick to Castor’s last description of a “non-prosecution” deal — forever closing off the possibility of a retrial.

Justices Kevin R. Dougherty and Max Baer dissented.

“We should not use Castor’s ‘blunder’ to place Cosby in a better position than he otherwise would have been in by forever barring his prosecution,” Dougherty wrote, warning that the ruling could lead to a host of prosecutorial abuses in future cases.

He added in a footnote: “One might reasonably wonder if such abuses were at work in this case, particularly given Castor’s odd and ever-shifting explanations for his actions.”

Castor, for his part, claimed vindication in the ruling, saying: “I feel pride at the fact that the Supreme Court agreed with my analysis.”

He still maintains he’s been entirely consistent.