Derek Chollet was an assistant secretary of defense for international security affairs and John Gans was a chief speechwriter for the defense secretary during the Obama administration.

For most of the nation’s history, Americans and the U.S. military have been able to take for granted peaceful transitions of power. People cast their ballots. The votes get counted, a new president forms a team, and a few months later, the military helps to put on a terrific inaugural parade. Such a tradition is no accident: Deliberate choices have long kept the U.S. armed forces subordinate to civilians and separated from politics. Our democracy has endured because both sides prefer it that way.

This aversion to politics has been difficult to maintain in the Trump era. The president calls senior officers “my generals.” He routinely treats speeches before troops as political rallies. And as protests swelled in recent weeks, he threatened to deploy active-duty troops to U.S. cities and had military personnel aggressively break up a peaceful protest in Lafayette Square. He even used the chairman of the Joint Chiefs of Staff, Gen. Mark A. Milley, as a fatigue-clad prop during the now-infamous photo op in front of St. John’s Episcopal Church across from the White House — a stunt in which Milley now regrets participating.

All these incidents make clear that Trump sees the military not as a constitutionally established instrument of government commanded by the president, but as an armed force that exists to serve the president. For more than three years, senior military leaders have taken pains to try to accommodate their apolitical norms with their very abnormal commander in chief. But time and again, Trump has simply rolled over both the brass and their norms, getting what he wants — or close to it — despite concerns voiced in private and regrets aired in the days after.

It is hard to overstate the risks either to the military or the nation’s democracy if that pattern repeats itself in the aftermath of the election in November, when the stakes will be far higher. If Trump loses, military leaders can do nothing to stop him from tweeting that the vote was rigged, claiming an insurrection and threatening a “tougher” response. But they can take proactive steps now to ensure that Trump doesn’t use those in uniform to try to legitimize his rants or suggest the military is taking his side.

As recent weeks have reminded us, it is as easy to sleepwalk into disaster as it is to stroll across Lafayette Square. As such, the brass need to try, as they do in other situations, to think about crises before they materialize and take steps to prevent them. Defending norms after they have been trampled — or apologizing after the fact — does not do much good.

Instead, military leaders need to get ahead of Trump’s temptations and insinuations and vow publicly they will only support a peaceful, democratic transition of power and leave it to the courts and Congress to resolve any electoral disputes. Such proactive steps are about prevention, creating expectations in Washington and around the country to condition Trump from even looking in the military’s direction to support any conspiracy theories or power grabs.

With its power to regulate and fund the military, Congress should pass legislation affirming the rules of the road, starting by acting on calls to amend the 1807 Insurrection Act to make it harder for the president to use active-duty forces for domestic purposes. Congressional oversight committees should also use upcoming hearings to give military leaders further incentive for thinking about this possibility and an opportunity to make public these concerns and commitments. And retired senior officers and former civilian defense leaders need to continue to reaffirm the military’s role in a democratic society and a peaceful transition.

Should prevention fail, however, Milley and other senior uniformed leaders need to prepare for the worst. They must think through their posture in the days after the election to support an orderly transition and continuity of government if the results are unclear or simply not accepted by the president. This planning must be as much about what the chiefs won’t do as about what they will, and include specific guidance to officers who oversee operational, legal and public affairs.

Down the chain of command, uniformed leaders need to know what steps to take — and not to take — at a fraught moment when everything will be tinged with politics and closely examined by both sides. This includes detailed instructions for any post-election engagement with the president, such as what events to participate in, who attends and which uniform to wear to the White House. Milley and his team also need to think ahead about when and why they would use their most serious weapon: resignation, which the chairman reportedly considered after his walk across Lafayette Square.

This is all a distressing possibility — the stuff of bad movies and banana republics. But recent events are worrying. This year’s vote is likely to be close and complicated by the novel coronavirus and a record number of mail-in ballots. Five months before the election, Trump — who still disputes losing the popular vote in 2016 — has already declared it “tainted” and subject to “MASSIVE FRAUD AND ABUSE.”

Fortunately, our military leaders do not have to wait to read what Trump tweets the morning after the election to reassure Americans they will honor their oath to defend the Constitution and the electoral system it establishes. Although our military leaders cannot — and should not — have any role in deciding the next commander in chief beyond casting a ballot, they can take steps now to prevent the president from trying to give them one.

Watch Opinions videos:

Read more: