It was no secret, by then, that Trump had grown disenchanted with his first chief of staff, Reince Priebus, who presided over a team consumed by squabbling factions, endless leaks and overbroad walk-in privileges for presidential face time. (During one Oval Office meeting Trump had with New York Times reporters in April, no fewer than 20 people came and went.) In an interview with The Times last December, Kelly said that he told Trump around this time that he did not believe the president “was being well served by the staff” in some respects. A month and a half later, Kelly recalled, Trump called and said, “I need you to be chief.”

Kelly set out first to slay the meandering, oversize meetings he loathed. “I see these people,” he used to tell staff members at Homeland Security, after returning from the White House. “I don’t even know who they are or what they do.” Almost immediately, he sought to institute new rules: Meetings were to be tight, targeted and surprise-free. Once, Vice President Mike Pence showed up for one unexpectedly. “You guys have the meeting,” Kelly grumbled, walking off, according to a White House official who witnessed the exchange.

For decades, the White House chief of staff’s mandate has been a kind of tough love — the capacity to close the door to the Oval Office and tell the president what he does not want to hear. “Above all, you are the honest broker of information,” Chris Whipple, the author of “The Gatekeepers,” a history of White House chiefs, told me. True to this template, Kelly clamped down on the free flow of information to Trump, who once rifled through Breitbart articles and conspiracy-stuffed printouts with impunity. Some executive riffraff was expelled altogether. “He fired me like a gentleman,” says Anthony Scaramucci, who lasted 11 days as communications director and scolds anyone who suggests it was 10. Those who dared attempt an unsanctioned chat with the president could expect a Kelly follow-up: “You want to be chief of staff?”

The early purges, which included the exits of the advisers Stephen K. Bannon and Sebastian Gorka, restored a measure of good will toward Kelly among Democrats. But again, Kelly’s kinship with Trump on immigration was underestimated. “Part of that is the Marine in him, part of that is the Irish guy in Boston who believes that in the end, you really do have to abide by the laws,” Panetta, a friend of Kelly’s, told me. “I think that’s what’s coming out now.”

In November, as Homeland Security was set to extend residency permits for tens of thousands of Hondurans living in the United States, Kelly made an 11th-hour plea to the department’s acting secretary to reconsider the move. When the administration debated lowering the annual cap on refugees — should it stay at 110,000? Fall to 50,000, the minimum recommended by Defense and State Department officials? Land somewhere in between? — Kelly offered his take: If it were his call, he said, the number would be between zero and one. The administration settled on 45,000.

Even as Kelly has driven out the most flamboyant West Wing agitators — Scaramucci, Omarosa Manigault Newman, Bannon, Gorka — it has not gone unnoticed that Stephen Miller, the 32-year-old senior policy adviser and Trump’s nativist id on immigration policy since the campaign, has thrived on Kelly’s watch. “He turns out to have been more hawkish than I might have expected,” Mark Krikorian, the executive director of the hard-line Center for Immigration Studies, told me of Kelly. “It’s a pleasant surprise.”

In January, several Senate moderates believed they were close to securing Trump’s support for a compromise measure protecting the young undocumented immigrants known as Dreamers, in exchange for border-security funding and other policy adjustments. Trump invited some of them to a televised summit at the White House, where he told them he would approve any legislation they brought him. “I’m not going to say, ‘Oh, gee, I want this, or I want that,’ ” he said. “I’ll be signing it.” He even assured Senator Dianne Feinstein, Democrat of California, that he welcomed a stand-alone bill protecting the Dreamers from deportation — anathema to the negotiating position of Republicans in Congress, some of whom rushed to dissuade him at the table.