– Senior U.S. national security officials, seeking to prevent President Donald Trump from upending a formal policy agreement at last month’s NATO meeting, pushed the military alliance’s ambassadors to complete it before the forum even began.

The work to preserve the North Atlantic Treaty Organization agreement, which is usually subject to intense 11th-hour negotiations, came just weeks after Trump refused to sign off on a communiqué from the June meeting of the Group of 7 in Canada.

The rushed machinations to get the policy done, as demanded by John Bolton, the national security adviser, have not been previously reported. Described by European diplomats and U.S. officials, the efforts are a sign of the lengths to which the president’s top advisers will go to protect a key and long-standing international alliance from Trump’s unpredictable antipathy.

Allied ambassadors said the U.S. officials’ plan worked — to a degree.

Trump did almost blow up the two-day meeting in Brussels that began on July 11. He issued a vague threat that the United States could go its own way if allies resisted his demands for additional military spending. After the gathering, he also questioned a pillar of the alliance: that an attack on one NATO country is an attack on all.

But the approval of the communiqué — renamed for the meeting as a declaration — was critical for the alliance. It ensured that, despite Trump’s rhetorical fireworks, NATO diplomats could push through initiatives, including critical Pentagon priorities to improve allied defenses against Russia.

“The president’s national security team did a good job of salvaging a minimally successful outcome to the NATO summit,” said James G. Stavridis, a retired four-star admiral who also once served as the supreme allied commander for Europe.

But, he added, “it is unfortunate that the president’s apparent personal animus continues to create friction in an alliance that has stood the test of time.”

In June, weeks before the meeting, Bolton sent his demand to Brussels through Kay Bailey Hutchison, the U.S. ambassador to NATO. He wanted the NATO communiqué to be completed early, before the president left for Europe, according to five senior U.S. and European officials who are familiar with the discussions but described them on the condition of anonymity.

Fearful of a repeat of the G-7 disaster — in which Trump refused to sign off on the joint communiqué, escalated a trade war and publicly derided Prime Minister Justin Trudeau of Canada — the emissaries from the NATO countries all agreed.

Two senior European officials said Secretary of State Mike Pompeo and Defense Secretary Jim Mattis were also keen to avoid another confrontation similar to the G-7, and the NATO declaration was completed days before leaders set foot in Brussels.

It achieved several goals critical to NATO officials.

Against Russian objections, the military alliance would formally invite Macedonia to join. It would establish an Atlantic Command post, hosted by the United States in Norfolk, Va., to coordinate a swift alliance response in the event of, for instance, a war in Europe between Russia and NATO allies.

And, most important, allies pledged to build up their militaries and provide 30 mechanized battalions, 30 air squadrons and 30 combat vessels, all ready to use in 30 days or less, by 2020 — a force to quickly respond to any attack on an alliance member.

Approving the communiqué, even rebranded as a declaration, was critical to moving the alliance forward over the coming years.

The new agreement has given U.S. national security officials the ability to assure the public, and skittish allies, that the country’s commitment to the alliance remains intact — no matter any anti-NATO statements from Trump.