Photo: Brendan Smialowski/AFP/Getty Images

One of the myths about President Trump is that he is attracted to anybody who praises him (and repelled from anybody who criticizes him) in an automatic and unthinking fashion, like an amoeba moving toward the light. The reality, to the consternation of advisers who have sought to tame and limit him, is a bit more complicated. As Trump is remaking his administration, most recently through the resignation of Defense Secretary James Mattis, he is proving that mere flattery is not enough to command his loyalty. He is learning to sift through the fake Trumpists and identify the real ones.

The acid test of the flattery theory is the president’s response to this week’s right-wing revolt over the budget and the wall. Conservatives pelted him with abuse and scorn over his plan to keep the government open. (Ann Coulter titled her column, “Gutless President in Wall-less Country.”) But rather than lash out at the people who were flaying him, Trump took their criticisms to heart and changed course.

Trump grasps that, however viciously Ann Coulter may express her occasional disagreements with Trump, and however politely Mattis may couch his, Coulter is operating out of some deeper loyalty to him, and Mattis is not.

The well-justified terror felt by establishment Republicans is that the method they believed they had devised to contain him is failing. The protective cordon built around the infantile president has broken, and Mattis represented its most important element. It was Mattis who stood between Trump and his most irreversible mistakes (the kinds that involve wars.) And it was Mattis who provided a diplomatic link to American allies, aghast at Trump’s nationalism and affinity for Russia. Trump is drawn toward a white nationalist axis with Vladimir Putin at the expense of America’s traditional democratic allies. Mattis has been the one to quietly reassure them.

The terms of abuse Trump fixed upon his Defense Secretary are telling. He was initially impressed with the general’s machismo, but eventually came to see him as a “moderate” or even, worse still, a “Democrat.” While the first description is questionable, and the second wrong, Trump was onto something. Mattis’s dissent was ideological. Trump sees western leaders as rivals and Putin as a friend. Mattis sees it the other way around.

It is almost certainly not coincidental that this moment of purification, of distilling his administration down to its most loyal cadres, is also the moment of maximum peril. Trump understands that a failure to fulfill his most famous campaign promise, building the wall, would have devastating consequences. (He has “warned in private conversations with Republican lawmakers that they all would get ‘crushed’” in 2020 if they don’t have this achievement.) And because he needs 60 votes to fund the wall, and hence Democratic support, he finds himself in the desperate and almost hopeless position of needing Democrats to give him the thing he needs to win reelection. He is shutting down the government out of angry panic.

More significantly, he and his family are encircled by multiple investigations. The Trump Foundation, a vehicle for looting, has been shut down. The Trump organization is being investigated for multiple crimes (tax fraud, business fraud, campaign finance violations, and, of course, collusion with Russia) by prosecutors at two levels of government — the state of New York, federal attorneys in the Southern District of New York, and the special counsel.

It is in response to the special counsel probe that Trump is remaking his administration most carefully. He installed a loyalist as Acting Attorney General, the comical hack Matthew Whitaker, and Whitaker has ignored the advice of ethics officials at the Department and refused to recuse himself from the Mueller probe. Trump’s nominee to fill the position permanently, William Barr, has staked out an extreme anti-Mueller stance.

All this is occurring as questions about Trump’s basic fitness for office, and even his loyalty, that laid partially submerged, are rising to the surface. Is Trump too beholden to hidden financial interests to look after the public good? Is he mentally capable of understanding politics and policy well enough to make competent decisions? Is his worldview even compatible with democratic values?

Republican professionals had hoped to stagger through four years without fully confronting these questions. Mattis’s resignation letter is mild by the standards of public commentary, but scorching by the standards of a cabinet official. He paints Trump as indifferent or even hostile to the free world, and attracted to the “authoritarian model” of Russia and China.

For a Secretary of Defense to publicly confront the president who appointed him in this way would be historic, if so many similar things had not happened already. Trump’s National Security Adviser H.R. McMaster subtly warned about the administration’s resolve to resist Russian aggression. Trump’s former Secretary of State Rex Tillerson has depicted him as a moron. Fired FBI director James Comey has presented Trump as essentially a mob boss handed the powers of the presidency.

For all his mental limitations, Trump does have a mobster’s talent for identifying character (or, more precisely, the lack of it). Trump is surrounding himself with lackeys who are untroubled by his corruption and immorality. And Trump grasps that he cannot win a fight like this without the rabid support of his ethnonationalist base and its media. For all the childish emotions that surround his actions, we can discern in them a pattern — a survival strategy.