Former White House national security adviser Michael Flynn leaves federal court in Washington on Tuesday after a judge berated him for his contacts with a Russian ambassador, saying, “Arguably, you sold your country out.” (Carolyn Kaster/AP)

The deal was done, and former national security adviser Michael Flynn was cruising toward a sentence of no prison time after admitting he lied to the FBI when interviewed at the White House about his contacts with the Russian ambassador to the United States.

But then, after special counsel Robert S. Mueller III recommended Flynn receive a sentence of probation as a reward for his cooperation with investigators, Flynn and his lawyers did something they came to regret: They made a filing to the court suggesting the FBI set him up.

U.S. District Judge Emmet G. Sullivan showed his deep displeasure with Flynn at a hearing Tuesday, at one point asking a prosecutor whether the retired general could be charged with treason. And Mueller, who has largely been in control of his investigation’s calendar into whether any Trump associates conspired with the Kremlin to interfere in the 2016 election, found a key part of his schedule suddenly upended by a defendant with cold feet and a judge with a hot temper.

Now, Flynn will wait another three months or more to find out whether he has to go to prison. To legal experts, Flynn has no one to blame but himself for his newly precarious position.

“He was in the catbird seat; he had prosecutors who thought he was truthful, prosecutors who thought he provided substantial assistance, and prosecutors who had recommended no jail,” said James Walden, a former federal prosecutor now in private practice. “And to go pick a fight over whether the FBI duped a guy of his age and his experience was unwise, to say the least. ”

In a pre-sentencing filing, Flynn’s lawyers said that he should receive leniency in part because the FBI did not warn him it was a crime to lie to FBI agents and did not question or challenge Flynn’s account of his conversation in late 2016 with Sergey Kislyak, then the Russian ambassador to the United States.

That filing fueled arguments in conspiracy theory circles — and in the White House — that Flynn was railroaded into pleading guilty and that Sullivan might nullify the plea deal.

Instead, the gambit seems to have increased the odds the former three-star general will end up wearing a prison jumpsuit.

In response to the filing, Sullivan ordered the Justice Department to produce the FBI records of Flynn’s interviews, as well as two related documents. What those materials show is that, contrary to the claims of Flynn’s most devoted defenders, Flynn was given a heads-up by a senior FBI official that the agents were coming to question him, the general knew what the questioning would be about, and that Flynn made statements that were provably false.

“They’re arguing he was treated unfairly. You know how many of my clients have gotten a heads-up from the deputy director of the FBI that agents were coming by to interview them, and what the interview would be about, and talk about whether a lawyer should be there? Zero,” said Patrick Cotter, a defense lawyer in Chicago.

The FBI documents were provided in the days leading up to Flynn’s sentencing hearing Tuesday.

Sullivan, a judge known to be blisteringly tough on lawyers and defendants when he thinks they have misled him or shaded the truth, tore into Flynn as the hearing began, saying that his actions arguably undermine “everything this flag over here stands for.”

“Arguably, you sold your country out,” he told Flynn.

With the threat of a possible prison sentence in the air, Flynn’s lawyers decided to ask the judge instead for a three-month delay in his sentencing, in the hopes the judge might see more tangible evidence of the importance of Flynn’s cooperation to Mueller’s investigation.

Flynn could become an important government witness in a case unsealed Monday against his former business partner, who was charged with illegally lobbying the U.S. government on Turkey’s behalf.

But even that possibility is in jeopardy now, Walden said.

“Whenever Flynn testifies, an experienced defense attorney is going to ram that filing so far down Flynn’s throat that it will be difficult for a jury to believe him,” Walden said.

That scenario highlights an odd pattern in Mueller’s handling of those who pleaded guilty and flipped to provide evidence against others — he has decided to let them be sentenced early, before they might testify in open court. Standard Justice Department practice is to delay sentencing for such cooperators until after all the investigation and prosecution work is finished, to ensure the guilty witness does not recant or water down their testimony.

It is not unprecedented for a cooperating witness to backtrack before sentencing, or even abandon their cooperation altogether. But the way in which Flynn did so — through his lawyers’ filing to the court, in an investigation involving the president — stunned some legal experts.

“It is highly unusual for a sentencing hearing to fly so far off the rails,” said Robert Mintz, another former federal prosecutor now in private practice. “The hearing showed that Mr. Flynn’s attempts to walk the line with the government between cooperation and criticism . . . plainly backfired.”

At Tuesday’s hearing, Flynn was specifically asked if he was guilty and wanted to continue with the case, and he said yes to both questions. His lawyer Robert Kelner urged the judge not to punish his client for a court document his lawyers wrote.

Even as Flynn and his lawyers retreated from suggestions he was set up, White House officials forcefully insisted he was.

White House press secretary Sarah Sanders said recent statements by former FBI director James B. Comey showed the FBI treated the Trump administration differently from previous administrations.

“FBI broke standard protocol in the way that they came in and ambushed General Flynn, and in the way that they questioned him, and in the way they encouraged him not to have White House counsel present,” Sanders said at a White House news briefing shortly after the court hearing.

Sanders was referring to Comey’s recent comments about his decision to send FBI agents to interview Flynn in January 2017, just days after Trump was sworn into office.

At a public appearance this month, Comey said that generally, when FBI agents want to question a senior White House official, such an interview would be arranged through the White House Counsel’s Office and the terms of the interview and personnel involved would be negotiated beforehand. In the Flynn case, Comey said it was so early in the administration that he decided, “Let’s just send a couple guys over.”