Congressional negotiators announced an “agreement in principle” Monday night on a broad spending bill they hope will satisfy President Donald Trump’s demands for additional border barriers and avert another government shutdown at the end of this week.

The compromise represents a remarkable turnaround for negotiators tasked with staving off another shutdown, just hours after lawmakers on both sides said the talks were on the brink of falling apart.

The tentative deal includes $1.375 billion for physical barriers — a type of fencing that resembles the “steel slats” that Trump has specifically called for, according to a congressional aide briefed on the talks. It includes a total of 55 miles, which is just 9 miles shy of Trump’s last budget request.

In exchange, Democrats agreed to drop their demand to restrict the number of people who can be detained by Immigration and Custom Enforcement at a time. Negotiators agreed to fund a total of 40,520 detention beds for ICE, a roughly 17 percent reduction from current levels, the aide said.

But one Republican source was quick to dispute Democrats’ account, saying the $1.375 billion in barrier funding can be used for “new miles of border wall.” The same Republican source suggested that the deal had enough flexibility to actually reach the president’s requested level of 52,000 beds, far above the negotiated level.

Details of the final deal may not be released until Wednesday, which has both parties aggressively attempting to spin the proposal as a win.

Shelby and House Appropriations Committee Chairman Nita Lowey (D-N.Y.) said they had approval from congressional leaders, namely Speaker Nancy Pelosi (D-Calif.) and Senate Majority Leader Mitch McConnell (R-Ky.). Shelby said he was also hopeful Trump would back the agreement but would not say directly if the president had promised to do so.

“The president told me, straight up, when I was with him… He told me more than once that if you can work out a legislative solution to this, do it. That’s what we’re trying to do,” said Shelby, who met with Trump last week. “Considering everything, this would be a good deal.”

The announcement follows a breakneck series of meetings between the top four congressional appropriators – Shelby, Lowey, Sen. Patrick Leahy (D-Vt.) and Rep. Kay Granger (R-Texas) – Monday aimed at trying to break the impasse after talks stalled over the weekend.

“If the four of us couldn’t get it together, this Congress never could. And we have gotten it together,” Leahy said Monday after the deal was announced.

Democrats moved quickly to sell the deal as a crackdown on the Trump administration’s immigration policies — likely an attempt to win over the party’s progressive flank. One source described the proposal as “putting a critical check on the Trump administration’s mass deportation policy that is tearing communities apart.”

But it’s unclear if the pitch will be enough to win over party’s liberal firebrands, who had called on Democratic leaders to not only reduce funding for homeland security but nix ICE funding all together.

The package would include all seven remaining funding bills, including agencies still reeling from a 35-day shutdown, like the IRS, the FDA and TSA. Lawmakers will not attach a massive disaster package, as Democrats initially sought.

Final text of the deal is unlikely until Tuesday, multiple lawmakers and aides said, with Lowey telling reporters she expected bill language on Wednesday.

It’s still unclear whether the House or Senate would move first. But both chambers must approve a deal by Friday to avert another partial shutdown, a challenge that’s made trickier by plans for former Rep. John Dingell’s funeral this week. Many lawmakers will travel to Michigan on Tuesday for the service.

The movement of bipartisan comity comes after negotiations over border security and avoiding a second government shutdown collapsed over the weekend, as Republicans rejected Democratic efforts to cap the number of detention beds that Immigration and Customs Enforcement can use for enforcement in interior regions of the U.S. At issue were detention resources for undocumented immigrants already in the United States, which Democrats wanted to impose new restrictions on to prioritize enforcement on violent criminals.

The deal comes as nearly everyone on Capitol Hill and in the Trump administration wanted to avoid a repeat of the record-setting shutdown last month, which lasted for 35 days. If Monday’s meetings had produced no progress, Congress and the White House would have had to turn to fallback options to avoid another shutdown, including a stopgap spending bill or unilateral action by Trump to fund his border wall.

The final deal is only slightly higher than Democrats’ offer of $1.3 billion for border fencing and falls far short of Trump’s demand for $5.7 billion in border wall funding that led to the government shutdown in December. And if Trump doesn’t agree to sign the bill, negotiators could be back at square one, staring down another potential shutdown later this week.

Source: Sarah Ferris, Heather Caygle and Burgess Everett, Politico