President Trump did something pretty remarkable Monday night: pivoting off a position he took in the 2016 race, one that’s quite defensible, to one that could earn him a world of political hurt, because he’s decided it’s best for the nation he leads.

The case for just pulling out of Afghanistan isn’t weak. And Trump, by opting to stay in and try yet again for victory, guaranteed himself excruciating future moments with widows and horribly wounded service members, with nothing remotely approaching certainty that it will pay off for America.

But he did it anyway, with an eye on the disastrous results of President Barack Obama’s pullout from Iraq when that war was seemingly won: America can’t let something worse than al Qaeda, worse than ISIS, arise — as it likely would if the United States just walked away.

Yet this was still a Trump speech, not some script he’d been handed, full of his characteristic riffs, and even with a signature concept that America will likely see more of: “principled realism.”

That speaks to the failures of Obama and of President George W. Bush: Trump will pursue American interests, with an eye on American ideals — and a firm awareness of the world as it actually is.

His new approach to Pakistan, his callout to India and his embrace of new Rules of Engagement bespeak a clear sense of facts on the ground and what’s needed for US troops to win.

All in all, this was a much-needed pragmatic realignment of US policy — in this war and beyond. Some honorable peace with elements of the Taliban is possible down the line; India does need to step up if it wants to stop being written off. America may yet have to leave Afghanistan to its own devices, if Afghan leaders won’t do their share.

On Monday, Donald Trump on Monday was an American president doing his job with all his heart, and his head.