House Speaker Nancy Pelosi (D-Calif.) speaks on the Hill June 27. (Alex Brandon/AP)

For nearly six months, House Speaker Nancy Pelosi (D-Calif.) kept the lid on her ideologically combustible caucus — tamping down a few small rebellions and trying to focus on the ­kitchen-table agenda that helped sweep Democrats into power in 2018.

But that carefully crafted plan hit a roadblock Thursday after a centrist revolt against the House version of a border crisis bill, leaving Pelosi cornered for the first time in her second go-round as House speaker.

“We will reluctantly pass the Senate bill,” she wrote to her Democratic colleagues Thursday afternoon.

Hours later, the House approved the Senate version of the border-funding plan, minus some of the carefully crafted protections that House liberals had demanded for migrant children held in security facilities. The legislation won approval but mostly with Republican votes. More than 40 percent of Pelosi’s caucus broke ranks against her, leaving Democrats divided as Congress heads home for a 10-day break around the July 4 holiday.

Almost every junior member of Pelosi’s leadership team opposed the legislation, along with several powerful committee chairmen. Those lower-level Democratic leaders are all eyeing the eventual departure of Pelosi, 79; House Majority Leader Steny H. Hoyer, 80; and House Majority Whip James E. Clyburn, 78, seeking to appeal to liberals in the inevitable succession fights.

Many House Democrats also pointed fingers across the Capitol at Senate Democrats for crafting what they viewed as a devil’s bargain with Senate Majority Leader Mitch McConnell (R-Ky.) on the legislation, which is headed for President Trump’s signature.

By midday, liberals took their outrage public — openly frustrated by Pelosi’s efforts to cater to the interests of swing-district Democrats in her bid to hold or expand the House majority next year.

Rep. Mark Pocan (D-Wis.) accused centrists of creating the “Child Abuse Caucus” in a tweet, after they forced Pelosi’s hand early Thursday by announcing their support for the Senate bill. That prompted several centrists to angrily confront Pocan during late-afternoon votes on the House floor, but the co-chairman of the Congressional Progressive Caucus refused to back down from his “child abuse” language.

Pelosi’s longtime allies defended her and argued that Thursday marked a small setback in an otherwise storied career. “Nancy is a very principled and skillful leader,” said Rep. Zoe Lofgren (D-Calif.). “It’s easier when you control both chambers.”

The Democratic retreat came almost five months to the day after one of Pelosi’s crowning achievements, when Trump surrendered on his border wall demands and agreed to reopen the full government after a 35-day shutdown. That episode framed the next several months of Washington power dynamics — exemplified by the image of Pelosi giving a mock clap toward Trump at the State of the Union — and gave Pelosi a store of political capital in her second run as speaker.

Pelosi blocked liberal requests to begin impeachment proceedings against Trump and left sweeping liberal proposals on health and energy bottled up in committee rooms.

Now, Pelosi heads into the second half of the year with new questions about her sway over her increasingly restive caucus.

Just before Pelosi formally abandoned the House bill, Pocan told reporters that “caving without consultation for any reason” would have ramifications on legislation in the months ahead, pointing to the National Defense Authorization Act as one bill that could be imperiled.

“NDAA already is going to be a tough lift. I can imagine how much tougher, potentially, it could be,” Pocan said.

Other major battles lie ahead, from a high-stakes budget fight over government spending levels to a potentially explosive appearance by former special counsel Robert S. Mueller III that could increase pressure on Pelosi to start impeachment proceedings against Trump.

The past week unfolded in many ways like the previous eight years, when the GOP ran the House and the previous speakers, John A. Boehner (Ohio) and Paul D. Ryan (Wis.), flailed in their efforts to pass compromise legislation because of insurrections from their right flank.

For months, many Republicans watched with admiration — if not a little resentment — as Pelosi’s grip seemed tightly wrapped around her caucus.

“Speaker Pelosi is now finding the reality that Speaker Ryan and Speaker Boehner had to deal with: You’ve got tight margins and a variety of opinions,” said Rep. Richard Hudson (R-N.C.), who personally thanked McConnell for sticking it to Pelosi on Thursday morning.

Just as the House GOP fought earlier this decade, Democrats broke into warring factions Thursday. Pocan’s tweet set the tone, accusing the bipartisan Problem Solvers Caucus of undermining the House bill. Rep. Max Rose (D-N.Y.), a freshman who won a GOP-leaning district last year, angrily confronted Pocan during votes but he refused to apologize for his “child abuse caucus” remark.

“I think that’s a pretty fair characterization,” Pocan told reporters afterward, suggesting the Senate bill will not protect migrant children. He said Rose complained about what family members would think of such an accusation.

“I’ll tell your mother you’re not a child abuser,” Pocan replied. “How’s that?”

Rose, who actually backed the progressives’ demands, accused Pocan of regularly trying to get attention, not working toward legislating. “I think that Mark probably got a couple more Twitter followers, and that that is an arbiter of success for him,” he told reporters.

As House Republicans once did, House Democrats accused their Senate counterparts of selling them out after all but six Senate Democrats voted Wednesday for that chamber’s version of the bill.

“Senate Democrats did us a huge disservice,” said Rep. Pramila Jayapal (D-Wash.), a co-chair of the progressive caucus. Privately, some leadership officials blamed Jayapal for frequent liberal demands that slowed their negotiationg.

Senior Democrats believed Pelosi and her leadership team lost their leverage as soon as they lost Senate Democrats.

With little leverage in hand, some House Democratic leaders in a Thursday morning caucus were careful not to criticize the Senate bill, believing it was probably the only path to sending billions of dollars to help the border crisis.

“I want to make that clear: The Senate bill is a good bill. But this is a better bill.” House Appropriations Committee Chairwoman Nita M. Lowey (D-N.Y.) told reporters leaving that huddle.It was around that time that Blue Dog Democrats and members of the centrist Problem Solvers Caucus began whipping votes against the House bill, quickly rounding up enough votes to sink the House proposal they knew would die in the Senate anyway.

Moderate Democrats said Pelosi’s whip team has worked them much harder in the past to stick together, but they appeared to see the writing on the wall and changed course.

Pelosi made one final push to reach a new deal in a late-morning call to Vice President Pence, hoping to get some legislative guarantees of regulations that would protect migrant children.

According to officials familiar with that conversation, Pelosi, an ardent Catholic, spoke for an hour about God and morality to Pence, an ardent evangelical. Pence told her to pass the Senate bill, but ultimately Pence agreed to several demands that Pelosi had wanted to tuck into the legislation, including that Congress would be notified within 24 hours after a child dies at the border.

After a tense two-hour leadership meeting, Pelosi gave up and sent the “Dear Colleague” message announcing the Senate bill would get a vote.

It tore the caucus apart, but some veteran Democrats said never to bet against Pelosi over the long haul.

“She knows what she’s doing,” Rep. Henry Cuellar (D-Tex.) said. “She knows.”