WASHINGTON — President Trump struck an agreement last week to require thousands of Central American migrants to seek asylum in Guatemala before coming to the United States. But the White House ceremony to hail what the president called a “landmark” measure — one that could transform the American immigration system — almost did not happen.

Just two hours earlier, Secretary of State Mike Pompeo had criticized the agreement in a closed-door meeting with Mr. Trump and Kevin K. McAleenan, the acting secretary of homeland security, and several other aides, according to three senior administration officials briefed on a debate that ensued in the Oval Office.

Mr. Pompeo called the agreement flawed and a mistake, and he told Mr. Trump that the Guatemalan government did not have the ability to carry out its terms, said the three officials, who spoke on the condition of anonymity to discuss delicate deliberations.

Mr. McAleenan pushed back, according to two of the officials. He argued that the accord, like similar ones in Europe, would stem the flow of immigrants, which has so infuriated Mr. Trump.