Afghanistan's rival candidates have reached a breakthrough agreement for a complete audit of their contested presidential election and a national unity government.
The deal brokered by US secretary of state John Kerry offers a path out of what threatened to be a debilitating political crisis for Afghanistan, with both candidates claiming victory and talking of setting up competing governments.
Such a scenario could have dangerously split the fragile country's government and security forces at a time the US is pulling out most of its troops and the Taliban continues to wage a fierce insurgency.
Instead, former finance minister Ashraf Ghani and former foreign minister Abdullah Abdullah consented to abide by an internationally supervised audit of all eight million ballots in the presidential election and a national unity government once the results are announced.
The audit is expected to take a "number of weeks" and would begin with the ballot boxes in Kabul. Ballots from the provinces are to be flown by helicopter to the capital by US and international forces and examined on rolling basis.
Observers from each campaign as well as international monitors will be involved in the oversight of the review, and the candidate with the most votes will be declared the winner and become president.
Both candidates agreed to respect the result, and the winner will immediately form a government of national unity. The inauguration, which had been scheduled for August 2, will be postponed.
Mr Abdullah, who spoke first at the news conference announcing the breakthrough, said the election created "serious challenges".
But he praised Mr Ghani for contributing to the agreement over how the audit would be conducted and the framework for a unity government to be established once the victor is determined.
Mr Ghani returned the compliments, lauding his competitor's patriotism and commitment to a dialogue that promotes national unity.
"Stability is the desire of everyone," he said. "Our aim is simple: We've committed to the most thorough audit" in history. Such a process would remove any ambiguity about the result, he added.
Mr Ghani noted that Hamid Karzai had reluctantly agreed to stay on as president until the new government formed for the good of the country.
The prolonged uncertainty about the outcome of the election has jeopardised a central plank of president Barack Obama's strategy to leave behind a stable state after the withdrawal of most US troops at the end of the year.
Preliminary run-off results, released earlier this week against US wishes, suggested a massive turnaround in favour of the one-time World Bank economist Mr Ghani, who had lagged significantly behind Mr Abdullah in first-round voting.
Mr Abdullah, a senior leader of the Northern Alliance that battled the Taliban before the US-led invasion in 2001, claimed massive ballot-rigging. He was runner-up to Mr Karzai in a fraud-riddled 2009 presidential vote before he pulled out of that run-off, and many of his supporters see him being cheated for a second time. Some, powerful warlords included, have spoken of establishing a "parallel government".
Mr Kerry met for a second day with the pair after discussions yesterday proved inconclusive, even though both candidates have acknowledged fraud in the election and agreed in principle to a UN investigation. He also met Mr Karzai and the UN chief in Afghanistan, Jan Kubis.
The bitter dispute over who is Mr Karzai's rightful successor has alarmed Afghanistan's US and Western benefactors, creating a political crisis that risks undermining more than a decade of efforts to build an Afghan government capable of fighting the Taliban on its own and snuffing out terrorist groups like al Qaida.
Extended instability would have more immediate consequences for Afghanistan. If no process had been established and both Mr Ghani and Mr Abdullah attempted to seize power, the government and security forces could have split along ethnic and regional lines.
The winner amid all the chaos could have been the Taliban, whose battle against the government persists despite the United States spending hundreds of billions and losing more than 2,000 lives since invading the country after the September 11, 2001, terrorist attacks.
Mr Kerry warned that much work remained to be done, adding: "This will be still a difficult road because there are important obligations required and difficult decisions to be made."