In a nation like Afghanistan, undergoing radical change, every decision seems to yield an array of what-if scenarios. Most recently, it was the Afghan presidential elections: What if the April 5 vote had produced a clear winner? Afghanistan might be celebrating the first popularly mandated transition of power in its history. Its new president would have signed the Bilateral Security Agreement with the U.S., prompting NATO to do the same and ensuring some level of stability for the near term.

Instead, two rivals emerged with no clear winner. One, Ashraf Ghani Ahmadzai, is a Pashtun, Afghanistan’s main ethnic group, concentrated in the east and south of the country. The other, Abdullah Abdullah, is considered a Tajik (though technically, he is half-Pashtun as well), who represents Afghanistan’s Persian-speaking north and west. They duelled in a runoff vote on June 14, the result of which should have been clear and binding. It wasn’t. Instead of preparing for a historic presidential inauguration, Afghans are now facing yet more uncertainty and security struggles. This week, a man dressed as an Afghan soldier opened fire at a military base outside Kabul, killing a U.S. Army major-general and wounding 14.

What’s little known is just how close Afghanistan came to total disaster. In the lead-up to the runoff vote, campaigning took an ugly turn: appeals to ethnicity became more frequent, dangerously raising tensions. The runoff vote was marred with allegations of fraud. Abdullah in particular cried foul, and threatened to set up his own parallel government after preliminary results showed Ahmadzai in the lead by a significant margin.

Abdullah’s followers took to the streets, threatening violence if their leader was not declared president. The tension escalated and, according to a New York Times report, reached its apex with a plan to march on three provincial governorates and take over the presidential palace in Kabul. If that had happened, we would be looking at a very different Afghanistan today—one likely in the early stages of a new civil war.

Credit for the rapid de-escalation goes to U.S. Secretary of State John Kerry, who brokered a last-minute deal that brought Afghanistan back from the brink. Threatening a total withdrawal of U.S. support for the Afghan government, Kerry managed to get both Ahmadzai and Abdullah to agree to a full audit of the runoff results and, once the winner is finally declared, form a unity government. But when that will happen is anyone’s guess. The process has already been delayed twice. The longer the recount takes, the more difficult it will be for the winner to claim legitimacy and begin the real work of rescuing Afghanistan.

Details of the candidates’ agreement are sketchy, but early indications suggest a total overhaul of Afghanistan’s political system, shifting it from a highly centralized presidency to something approaching a more federalist parliamentary model. Whoever loses the election would be given a newly created prime ministerial post and power brokers from both sides would divvy up key ministries between them. (The security forces, according to Afghan officials, would remain untouched for the time being, indicating just how sensitive the ministries of defence and interior, which controls Afghanistan’s controversial spy service, the NDS, are.)

“Afghanistan dodged the bullet for now,” says Graeme Smith, senior analyst for the International Crisis Group (ICG), based in Kabul. “But things will get more difficult down the road when constitutional changes will be needed to institutionalize these changes. Someone will be declared the winner after the audit and there is not a strong history of Afghans giving up power once they have it.”

Indeed, how exactly power will be devolved away from the presidency—and where it goes—remains unclear. Over the past decade, President Hamid Karzai has steadily consolidated his position and embedded the presidential office at virtually every level of national and local politics. No decision is made in Afghanistan without presidential approval. A new system is needed, says Smith.

“What these local and ethnic networks need,” wrote Olivier Roy, one of the world’s leading scholars of Islam and politics, in 2010, “is a distant but benevolent and legitimate state, regarded as a broker and an ally helping to establish a favourable local balance of power and influence.” The agreement brokered by Kerry, if implemented, offers the clearest path yet to Roy’s ideal.

But it is also highly controversial. It paves the way for the Taliban to gain some measure of political control in the areas they dominate—something that has elicited both support and condemnation. Back in 2005, Gen. Rick Hillier, Canada’s chief of Defence staff, described the Taliban as “detestable murderers and scumbags” who “detest our freedoms, our society, our liberties,” a perspective he shared with his American counterparts. For them, there was no other solution to the Afghanistan problem except the utter defeat of the Taliban and the establishment of central government control throughout the country.

Others saw it differently. “These guys are not going to be defeated militarily,” Capt.Angus Matheson, a senior officer in charge of force protection at Camp Julien in Kabul, told Maclean’s back in 2006, in the weeks before Canadian troops were deployed south to Kandahar. “At some point, they will have to be brought into the political process. When and how that happens is up to the Afghans.”

The U.S. administration has come around to that perspective under President Barack Obama. (Stephen Harper’s Conservatives have not. In May 2013, they quietly added the Afghan Taliban to Canada’s list of terrorist entities, nixing any chance of Canadian involvement in a negotiated settlement.) Evidence is now emerging that the Taliban might also be willing to join the political process. According to the ICG’s Smith, there are indications that local Taliban commanders were encouraging people to participate in the runoff vote in favour of Ahmadzai. Other sources in Kandahar have confirmed the rumours.

“Their thinking is that Ashraf Ghani Ahmadzai could offer them a viable negotiating partner,” says one source, a local in Panjwayi, 25 km west of Kandahar City, who acts as a liaison between the central government and Taliban commanders. “They are tired of fighting; the local people are tired. The belief is that if Ghani is serious about national unity then he will give the Taliban a role in government.”

There are still serious hurdles standing in the way. Another reason the Taliban backed Ahmadzai, the source adds, is because he is, like them, Pashtun. “Abdullah is backed by the Northern Alliance commanders,” he says. “They are Persians and the Taliban’s worst enemies. They do not want to see them in power.”

Those ethnic divides remain the wild card over the coming weeks and months, threatening not only to undermine political reconciliation but also fragment the security forces and plunge Afghanistan into civil war. The Afghan national army, meanwhile, is far from unified, says Smith, and the NDS is entirely controlled by the Tajik minority, with a reputation for brutality, in particular against Pashtuns.

The success of any new political process depends now on tumblers falling into the right alignment at just the right time. Each of those tumblers represents a new pivot point around which Afghanistan’s history will be determined. As always, the possibilities are frightening, but for the first time in years they’re a bit encouraging too.