With just hours to go before the US government is due to shut down, the House of Representatives has agreed to a short-term funding deal.
A bill that keeps the government funded until mid-November but includes no new aid for Ukraine was approved in the chamber by a vote of 335 to 91.
The measure has to be approved by the upper congressional chamber, the Senate, but is expected to pass.
Once signed into law, it will avert a disruption of federal services.
A shutdown, which would place tens of thousands of federal employees on furlough without pay and suspend various government services, was slated to begin at 00:01 ET (04:01 GMT) on Sunday.
But in a dramatic turnaround on Saturday afternoon, House Republicans scrambled to pass a temporary funding measure that would keep the government open for 45 more days and make no major concessions on spending levels.
It was backed by more Democrats than Republicans, with as many as 90 Republicans voting against it.
The move was a blow to a small group of right-wing Republicans who have held up negotiations in the chamber with unyielding demands for spending cuts.
However, with a majority of lawmakers keen to avert a shutdown, one of the faction’s key demands – no more US funding for Ukraine’s defence against its invasion by Russia – is reflected in the bill.
Shutdowns happen when both chambers of Congress are unable to agree on the roughly 30% of federal spending they must approve before the start of each fiscal year on 1 October.
With Republicans holding a slim majority in the House and Democrats holding the Senate by a single seat, any funding measure needs buy-in from both parties.
Repeated efforts to pass spending bills in the House have been thwarted in recent weeks by rebel right-wingers.
The group has opposed short-term spending measures and pushed for making cuts by passing long-term spending bills with agency-specific savings, even though such bills stand little chance of advancing through the Senate.
Speaker Kevin McCarthy had been extremely reluctant to rely on Democratic votes to pass the House’s bill until the last minute, given this would anger these hard-line conservative members of his party.
But Democrats did not get everything they wanted. In order to avoid a shutdown, they had to abandon their hopes of providing further military aid to Ukraine.
They – and Republicans who also support more Ukraine money – will keep pressing for more funding, but officials in President Joe Biden’s administration have warned that, in the short term, there could be disruption to the Ukrainian war effort.
“We fully expect Speaker McCarthy, who has stated his support for funding to support Ukraine in its fight against Russia’s illegal and unjustified war of aggression, will bring a separate bill to the floor shortly,” a White House official told the BBC’s US partner CBS News.
Chaos had engulfed Capitol Hill ahead of the vote as members of both parties sought clarity on the best path forward to avert a shutdown.
As House Democrats complained that they were unable to read Republicans’ latest offer before voting on it, one – Jamaal Bowman of New York – appeared to have pulled a fire alarm in one building to buy more time.
Pulling fire alarms when there is no fire emergency is a criminal offence punishable by prison time in Washington.
A spokesperson from Mr Bowman’s office said he “did not realise he would trigger a building alarm as he was rushing to make an urgent vote. The Congressman regrets any confusion”.
Mr McCarthy slammed the move as “a new low” for the House at a news conference in which he took credit for the successful House vote.
His counterpart, Democratic leader Hakeem Jeffries, argued his party had bailed out Republicans following “a complete and total surrender to right-wing extremists who throughout the year have tried to hijack the Congress”.
Lawmakers in the Senate are expected to quickly take up the bill and attempt to pass it within hours on Saturday, so it can be rushed to President Biden’s desk for his signature.
Assuming the Senate follows the House’s lead and quickly approves its short-term bill, the government will stay open – at least for now.
But this drama is likely to be repeated again in 45 days, as fundamental disagreements over government spending levels and policies between Republicans and Democrats, and among Republicans themselves, have not been resolved.
In the meantime, Florida Congressman Matt Gaetz and hard-line conservatives in the House have a decision to make.
Mr McCarthy’s decision to rely on Democratic votes to pass the short-term bill was supposedly a red line that, if crossed, would prompt a right-wing rebellion in the House and an attempt to remove Mr McCarthy from his leadership position, by triggering a so-called motion to vacate.
At his Saturday news conference, Mr McCarthy challenged those who oppose him. “If somebody wants to bring a motion against me, bring it,” he said. “There has to be an adult in the room.”
The days ahead will reveal whether Mr Gaetz and company were serious about their threat – or just bluffing.