WASHINGTON – Congressional leaders reached a tentative $1.3 trillion spending deal Wednesday to keep government agencies operating through September, unveiling legislation that would make good on President Trump’s promises to increase military funding while blocking much of his immigration agenda.

The release of the 2,000-plus-page bill Wednesday evening, after a two-day delay, touched off a legislative sprint as lawmakers try to pass it before Friday night, the deadline to avoid a government shutdown. And with a key senator unwilling to say whether he would agree to accelerate the deal’s consideration, it remained uncertain whether they would be able to meet the challenge.

Speaker of the House Paul Ryan, R-Wis., meets with reporters on Tuesday. Associated Press/J. Scott Applewhite

There were other plot twists as the deal came together: As aides hashed out its final details on Wednesday afternoon, Trump’s support for the emerging compromise was suddenly cast into doubt, forcing House Speaker Paul Ryan, R-Wis., to rush to the White House early Wednesday afternoon to allay the president’s concerns.

After the meeting, White House press secretary Sarah Huckabee Sanders said in a statement that Trump had spoken to Ryan and Senate Majority Leader Mitch McConnell (R-Ky.) “about their shared priorities secured in the omnibus spending bill” and confirmed their mutual support for the legislation.

In the broadest strokes, the bill gives Republicans a major win by delivering a $78 billion increase in military spending over 2017 levels, while Democrats won a $52 billion increase for domestic programs. The haggling that delayed the legislation’s release concerned smaller-bore provisions sprinkled throughout the bill.

One hotly litigated matter concerned funding for Gateway, a major New York-area infrastructure project. At Trump’s behest, Republicans succeeded in eliminating some provisions favoring the $30 billion project that includes building a new rail tunnel under the Hudson River. But project backers said it would still be eligible for hundreds of billions of dollars in taxpayer funds.

The dickering played out for hours Wednesday, even after top congressional leaders left a morning meeting on a snowbound Capitol Hill declaring that a deal was at hand.

“We’re feeling very good about this,” Senate Minority Leader Chuck Schumer, D-N.Y., said. “We’ve accomplished many, many, many of our goals.”

Democrats pressed particularly hard to block Trump’s requests to fund a new wall along the U.S.-Mexico border and to beef up immigration enforcement capacity.

The bill includes $1.6 billion in funding for construction of a border wall, but that number is far short of the $25 billion in long-term funding that the administration sought. Democrats also won tight restrictions on how that money can be spent.

The scant border wall funding, aides said, accounted for Trump’s cold feet Wednesday. He pushed in recent days for much more extensive funding and expressed his willingness to cut a deal, in exchange, with Democrats to extend the Deferred Action for Childhood Arrivals program that Trump moved to cancel last year. But the talks went nowhere.

The spending bill faces opposition from many conservative Republicans, but they are unlikely to be able to derail the bill given its likely support among Democrats and more moderate Republicans.

Rep. Mark Meadows, R-N.C., the chairman of the Freedom Caucus and a Trump ally, was described by a White House official as one of the key influencers of the president’s position.

Meadows slammed the proposal during a Wednesday panel discussion on Capitol Hill, saying that “wins for conservatives will be few and far between.”

“Are we going to continue to fund sanctuary cities? Are we going to continue to fund Planned Parenthood? Are we going to continue to raise the debt to levels that quite frankly are unsustainable and bankrupt our country?” he said. “There is really no wall funding. People will try to spin it as there is wall funding, but the [$1.6 billion] has been in there for some time.”

One late-breaking deal surrounded gun laws. Democrats agreed to add bipartisan legislation to improve the National Instant Criminal Background Check System (NICS) for gun buyers, while Republicans agreed to add language making clear that federal funds can be spent on research into gun violence – clarifying a long-standing restriction that has been interpreted as preventing such research.

The package also includes a fix for a provision in the new tax law that favored farmer-owned cooperatives over traditional agriculture corporations, threatening the viability of some corporations by shifting sales to cooperatives. In exchange for agreeing to the fix sought by Republicans and farm groups, Democrats won an increase in a low-income housing tax credit.

Omitted was a health-care measure sought by Republican Sens. Susan Collins, Maine, and Lamar Alexander, Tenn., which would have allowed states to establish high-risk pools to help cover costly insurance claims, while restoring certain payments to insurers under the Affordable Care Act. Trump, who ended the “cost sharing reduction” payments last fall, supported the Collins-Alexander language. But Democrats opposed it because they claimed it included language expanding the existing prohibition on federal funding for abortions.

While a Democratic push to win provisions protecting Special Counsel Robert Mueller did not succeed, the bill does include hundreds of millions of dollars to combat potential interference from Russia or others in the November midterm elections. The federal Election Assistance Commission will receive $380 million to dole out to states to improve their election-related cybersecurity. And the FBI is set to receive $300 million in counterintelligence funding to combat Russian hacking.

Trump succeeded in partially blocking efforts to direct $900 million in planned seed funding to the Gateway project, which has been a key priority for lawmakers of both parties, including Schumer and House Appropriations Committee Chairman Rodney Frelinghuysen, R-N.J.

Trump, according to several officials familiar with his thinking, was determined not to hand Schumer a win while Democrats stood in the way of his administration’s priorities, and he maintained for weeks that he would veto any bill that included the project. Still, a Democratic aide said the project could still benefit from hundreds of millions of dollars in Transportation Department funding, though in some cases it would have to compete with other projects.

In the final wall compromise, strings are attached to the $1.6 billion that will be available for physical barriers along the Mexican border. Most of the funding, officials said, can be used only for repairs or for “secondary” barriers along border stretches where there is already a wall. The rest can be used for 33 miles of new barriers, but there are restrictions on the type: Only levees or existing “bollard” fencing can be built, rather than the concrete prototypes Trump appears to favor.

The bill also rejects a Trump administration request for more immigration enforcement officers and an increase in funding for detention facilities. Language in the bill, two officials said, holds the level of enforcement agents flat and does not allow the administration to add detention beds. However, a Republican official said the administration could still move money between accounts to fund more enforcement.

An effort to trade a much larger amount of border wall money for protections for certain young immigrants fell apart Tuesday. Trump continued to push for a last-minute deal as recently as Monday, but Democrats resisted the terms of the White House offer.

A House vote on the spending bill had been tentatively expected Thursday, but by Wednesday night that looked likely to slip into Friday morning. That would leave scant margin for error in the Senate, where unanimous consent from all members would be needed to waive procedural rules and set up votes before the Friday midnight deadline.

That means any one senator could delay the proceedings and force a brief shutdown, much as Sen. Rand Paul, R-Ky., did in February, when he held up consideration of a previous budget bill.

Paul said Wednesday that he had not decided how he would handle the new bill, telling reporters that he would wait to read it first. But he made clear that he was unlikely to be pleased by its contents.

“I think it is safe to say that there are many voices in the Senate, including many Republicans, who are not real happy about having a thousand-page bill crammed down our throat at the last minute without time to read it,” he said. “It’s a really terrible, rotten, no-good way to run your government.”