Despite Mr. Trump’s campaign rhetoric vilifying Mexicans and focusing on a border wall, embassy officials and our Mexican partners felt after his inauguration that we would be able to continue working well together. But it quickly became impossible to know how to influence the mess in Washington.

On that April afternoon in 2017, I knew that Rex Tillerson, the secretary of state, didn’t want to get involved in Nafta talks and rarely took calls from even a senior ambassador. So I talked to senior career colleagues from the Office of the United States Trade Representative and the Commerce Department, but they knew little more than I did.

As President Peña Nieto joined diplomats and government and military officials on the reviewing stand, he paused to greet me and emphasized that it was imperative that we talk later. When we finally sat down alone, the president, unfailingly polite, was blunt: What the hell was going on? “Your president is going to pull out of Nafta before we’ve even had a chance to sit down and work on this?” he said to me. “This would be a disaster — economically, politically.”

He was right. Nafta, while never a panacea, had helped trade nearly quadruple among the United States, Mexico and Canada; made countless American industries more competitive; and perhaps most important, cemented a shift in our relations with Mexico to the benefit of the United States. Mexicans opened up to the world with Nafta, not just in trade but also politically, with democracy advancing, albeit in fits and starts. Mexican governments became our partners on security, migration and foreign policy, including on terrorism. Pulling out would threaten more than just a productive trade relationship.

All I could tell him was that I was continuing to speak with the White House and hoped cooler heads would prevail. I noted that this was coming just after a spate of negative articles about the first 100 days of the Trump administration. I was learning that critical news reports almost inevitably led the president to fall back on his standard refrains: Build the wall, or Nafta is the worst deal ever.

The draft document to pull out of Nafta was never sent. Why? We’re not really sure. Perhaps because the Mexican foreign minister, Luis Videgaray, engineered a phone call between Presidents Trump and Peña Nieto. Perhaps Mr. Trump’s secretary of agriculture showed him evidence that his rural, agricultural base would be hurt. Or because powerful Republicans in Congress weighed in against ruining an important trading relationship.