Rep. Nancy Pelosi’s opponents knew they had an opportunity. But what they really needed was an alternative.

This past summer, they thought they had identified the perfect candidate to replace Pelosi (D-Calif.) as the party’s House leader: Rep. Karen Bass, a respected Californian who once served as speaker of the state Assembly. At least two of Pelosi’s rivals suggested to Bass that she pursue the post, and they left encouraged from meetings with her.

But it was wishful thinking. “They had hoped that I would consider it,” Bass said Thursday. “I was clear: I would never challenge Pelosi.”

Pelosi, meanwhile, was solidifying the support she would need to reclaim the speaker’s gavel, working furiously through the summer to regain the House majority and meeting privately with the left’s ascending political star, Rep.-elect Alexandria Ocasio-Cortez (N.Y.). To nail down the votes, Pelosi deployed the same tactics she used multiple times to muscle hard-fought legislation through the House during her prior tenure as speaker — methodically undermining her opposition, tapping a vast network of allies and relying on a grab bag of political favors.

House Minority Leader Nancy Pelosi (D-Calif.) and Senate Minority Leader Charles E. Schumer (D-N.Y.) addressed reporters after meeting President Trump Dec. 11. (Reuters)

Pelosi, 78, clinched the votes she needed late Wednesday after announcing support for a term limits deal that would allow her to serve as speaker for another four years. Seven dissident Democrats immediately said they would support her, paving the way for her election Jan. 3.

Former House majority leader Richard A. Gephardt (D-Mo.) called it “a very typical performance by a really talented political figure.”

“The fact that she’s been speaker and will be speaker again is testimony to her enormous ability, her toughness, her off-the-charts work ethic, and her ability to interact with people and negotiate with people and come to a conclusion,” he said. “She works harder than any human being I’ve ever known.”

It was not, however, an outright defeat for Pelosi’s opponents: Her agreement to exit the stage in 2022 marks a significant concession for a leader who had vowed in recent weeks never to put an expiration date on her political career. If House Democrats vote early next year to institute the term limit proposal, it would mean the clock is also ticking for incoming House Majority Leader Steny H. Hoyer (D-Md.) and incoming Majority Whip James E. Clyburn (D-S.C.).

“A lot of people said that they would stay here as long as they could,” Rep. Seth Moulton (D-Mass.), one of the rebels, said Thursday. “If we can institutionalize this, it will be very significant. And we took a major step toward institutionalizing it by getting her support.”

But the rebels fell well short of their goal of ousting Pelosi, suffering repeated setbacks as they worked behind the scenes to capi­tal­ize on a sense of frustration with a top tier of septuagenarian leaders who had been in place for nearly 16 years.

When efforts by Moulton and Rep. Brian Higgins (D-N.Y.) to recruit Bass sputtered, they moved on to other younger stars of the Democratic caucus — including Reps. Joe Kennedy III (Mass.), Hakeem Jeffries (N.Y.) and Cheri Bustos (Ill.). But rather than take on Pelosi, they all chose to bide their time and pursue more-junior positions.

Ultimately, the rebels were forced into their last-ditch posture: trying to stick together as a bloc, deny Pelosi the votes she would need to be elected speaker and hope an alternative would emerge.

Moulton had drawn up list of 58 Democrats who he knew wanted a new leader. Most of those, he said he believed, would sign a letter expressing opposition to Pelosi — one that would demonstrate that Pelosi could not be elected speaker and prompt the shake-up they wanted.

But that never happened. Democrats won a majority in the Nov. 6 midterms, ultimately flipping 40 seats from Republicans, a showing that bolstered Pelosi’s strength. With no viable alternative stepping forward, members saw no reason to alienate Pelosi by opposing her — let alone challenging her in a messy floor battle.

Many who quietly wanted new leadership threw their support to Pelosi. “A lot of summer soldiers around here,” Moulton, a former Marine Corps officer, would say.

Instead of 35 names, the rebels ultimately released a letter Nov. 19 with only 16 names. A host of incoming freshmen who had pledged not to vote for Pelosi during their campaigns refused to sign — they didn’t see the point of the letter, even though they remained opposed. But it sent a message that there was disunity among the rebels. “The momentum was broken, no question about it,” Higgins said.

That perception was reality, and Pelosi took advantage. The rebellion saw a brief glimmer of hope in late November, when Rep. Marcia L. Fudge (D-Ohio), 66, a blunt Cleveland lawyer close to Moulton and Rep. Tim Ryan (D-Ohio), another longtime Pelosi critic, suddenly expressed public interest in a speaker bid.

But Fudge quickly bowed out after Pelosi met with her and walked through the enormous scope of the job — including huge fundraising demands and travel time — and offered commitments on Fudge’s policy priorities, including the opportunity to lead a panel on elections.

Two days after the letter, Pelosi also came to terms with Higgins, who had been nursing a grudge for months. He had blamed a senior Pelosi aide for derailing a health-care bill that he had sponsored and lashed out publicly at her leadership. But as the leadership dispute heated up, Pelosi called Higgins, offered to make amends and brokered a truce — one that gave her momentum ahead of a caucus vote the following week in which she ran unopposed.

Meanwhile, Pelosi’s allies were starting to hammer the opposition, and they tended to highlight one uncomfortable fact about the rebels: They tended to be whiter and more male than the Democratic caucus at large — especially after Fudge, a black woman, bowed out. A Twitter hashtag tweaking the opponents as “#fivewhiteguys” started trending — never mind they had women and minorities in their ranks.

Moulton emerged as a particular lightning rod, facing pro-Pelosi protesters at a November town hall and persistent calls for a primary challenge. When during a CNN appearance he accused Pelosi of not moving aggressively on gun-control legislation during her previous time as speaker, her aides lined up gun-control advocates to criticize him.

He annoyed other members of the group by issuing a statement two days before the nominating vote declaring that he was willing to negotiate with Pelosi about the broader leadership team, upending their strategy.

There were other internal clashes: Other members of the rebel group urged Rep. Kathleen Rice (D-N.Y.) to take a more aggressive role as a female face of the anti-Pelosi effort, but she bristled at being asked to step forward as a token woman — especially by Moulton, whom she blamed for strategic missteps.

In the Nov. 28 caucus vote, Pelosi won the nomination on a 203-to-32 vote — a tally that represented a marked improvement from her 2016 election as minority leader but also showed she was vulnerable on the floor if the rebels stuck together.

Shortly after the vote, she met with Moulton, Rice and Ryan in her office. The meeting started badly when Rice thanked Pelosi for inviting the trio; Pelosi was puzzled, according to an aide — unbeknown to Rice, it was Moulton who had indirectly requested the meeting. And Pelosi was further annoyed that the group seemed to have no new offer, just the same talking points she had already heard.

Things did not improve from there: She flatly rejected their request to leave before the next election, held forth on her long record as party leader and sent them out the door thinking there would be no further negotiations.

Another member of the group, Rep. Ed Perlmutter (D-Colo.), thought differently. A former bankruptcy lawyer steeped in complex negotiating, Perlmutter maintained a warm relationship with Pelosi and assiduously avoided Capitol Hill hallway gaggles or cable news appearances.

Over the course of several long phone conversations, he directed the discussion toward what a “transition plan” might entail: A date certain for her departure? No way, Pelosi said. A new leadership election in 2019? Forget about it. But term limits for the Democratic leadership writ large? That was something she was willing to negotiate.

“The willingness by Nancy to talk and work through this . . . that was a very big deal,” Perlmutter said. Could she have prevailed without the term limits deal? “She may have. I mean, her skills are real,” he said.

It was another low-key holdout, Rep. Bill Foster (D-Ill.), a former physicist who made a fortune selling theater lights, who devised the key compromise: While leaders would be entitled to three terms, they could win a fourth term with a two-thirds caucus vote. Pelosi, who has already served two terms as speaker, would not become an immediate lame duck, but younger members would increase their leverage to force a change if necessary.

“That’s a long time,” Pelosi told reporters Thursday about the four-year limit on her coming speakership.

Less comfortable with the proposal is Hoyer, 79 — who told reporters Tuesday as the deal was coming together that Pelosi was “not negotiating for me.” Publicly and in private conversations with Pelosi, Hoyer lashed out at the prospect of term limits and is expected to oppose the proposal when it comes up for a caucus vote early next year.

But Pelosi’s pledge to abide by the limits regardless of whether the caucus ultimately ratifies them was enough to win over the crucial group of rebels. Their final offer was crafted in a 90-minute phone call Tuesday morning — one that took place as Pelosi sparred with President Trump in the Oval Office in a televised tangle that all but ended any real rebellion.

Bearing a one-page document, Perlmutter, Foster and Rep. Linda T. Sánchez (D-Calif.) went straight to Pelosi’s office and, in a brisk meeting, ticked through their demands: A statement calling for “reconciliation, unity and fair treatment” of caucus members; a “leadership development” program open to members and staff; and the term limits pledge.

Pelosi, according to people familiar with the negotiations, accepted all of it but expressed concern about the wording of her statement accepting the deal. Sounding too enthusiastic about the deal, she warned, would needlessly irritate Hoyer and Clyburn.

A day later — after some finishing touches and crossed wires — Pelosi made her statement: “I am comfortable with the proposal, and it is my intention to abide by it whether it passes or not.” Seven rebels endorsed her candidacy.

“The way she did it is why she’s been the leader for so long,” said David Axelrod, former political adviser to President Barack Obama. “It’s incredibly challenging to put together the votes for anything in Congress, and she’s a master at it. She listens and knows each person’s interests and their concerns. And she puts the puzzle together, making concessions where necessary.”

Ryan acknowledged the rebels “ruffled some feathers along the way” but said the term limits concessions they ultimately won were significant: “We put pressure on by saying: ‘We’re not going away just to go away. We’re not going away without some change.’ ”