A YEAR ago, as Ashraf Ghani (above, right) was about to declare his candidacy to become Afghanistan’s president, he told confidants that his dream was to win the election and then persuade the man he perceived to be his biggest threat, Abdullah Abdullah (above, left) to join his government. On September 21st Mr Ghani got his wish, though not in the way he would have wanted. Since the second round of the presidential election on June 14th and the allegations by the loser, Dr Abdullah, of “industrial-scale fraud”, the country has been locked in a political crisis. Now, a deal between the two men has been struck that commits them to sharing power and its spoils. Just possibly, a more hopeful chapter in Afghanistan’s turbulent recent history is about to open.

Mr Ghani, a 64-year-old anthropologist and former World Bank official who served his presidential predecessor, Hamid Karzai, as finance minister, will be inaugurated as president. Dr Abdullah, a 54-year-old former foreign minister and longtime de facto opposition leader, will take on a newly created role as chief executive officer, which looks much like the job of prime minister.

Unless Dr Abdullah decides to anoint someone else for that role, which seems unlikely, both will be inaugurated along with their deputies in Kabul, the capital, on September 29th. For the West, particularly America, it will be a welcome end to the 13-year rule of Mr Karzai, who was often mercurial and around whom a corrupt cabal grew. He was constitutionally barred from standing again.

The power-sharing compromise between Mr Ghani and Dr Abdullah was proposed and brokered by the American secretary of state, John Kerry, after Dr Abdullah had cried foul play in the run-off. (He had seen an election stolen from him once already, in 2009.) Mr Ghani attributed the turnaround in his electoral fortunes after the first round, which Dr Abdullah led, to more effective campaigning, particularly in his eastern and south-eastern stronghold, and to help from his vice-presidential nominee, General Abdul Rashid Dostum, a thuggish warlord, who brought over a large chunk of the ethnic Uzbek vote in the north. Dr Abdullah blamed it on a conspiracy involving the Ghani team, Mr Karzai and a partisan election commission.

Many in Afghanistan’s political elite had long wanted to reduce the overwhelming powers of the presidency, and liked Mr Kerry’s proposal. Yet others decried the secrecy of the backroom deal. What was the point of Afghans risking Taliban wrath to go out and vote in huge numbers if the run-off candidates carved up power between them in the end? And after Dr Abdullah’s accusations of voting fraud and his demands for a full audit, it sticks in the craw that it was out of deference to him that the audit’s final result was not made public. Presumably, the audit confirmed both that he had lost and that his side had also committed fraud.

Even so, the outcome is probably the best possible one in a country with such messy politics and pressing concerns. The long stand-off was harming the economy and emboldening the Taliban to make more inroads, notably in Helmand province in the south. And it threatened to reopen old ethnic divisions, even prompting fears of a new civil war.

The four-page document Mr Ghani and Dr Abdullah have signed summarises how the unity government is to function. In effect, Dr Abdullah will run the government and chair a new council of ministers. For his part, Mr Ghani’s powers under the constitution will remain and the new chief executive will be answerable to him. Despite understandable scepticism, the arrangement could work, sort of. In Afghanistan, political alliances are fluid, and pragmatism usually trumps principle. (For instance, branding him a “known killer” did not prevent Mr Ghani last year from putting General Dostum on his ticket.)

There will certainly be road bumps. The first could be the allocation of the most coveted ministries and senior government posts. Both General Dostum and AttaMohammad Noor, the powerful governor of Balkh province in the north who played the role of Dr Abdullah’s attack dog after the disputed vote, must somehow be kept happy. Still, moving these old warlords aside to make room for fresher, more technocratic faces in government must be a long-term aim. Elsewhere, swift steps need to be taken to prove that this new government is serious about lessening its addiction to foreign aid. That means action to increase government revenues and an end to the culture of impunity for high-level corruption. The political impasse only added to the country’s economic fragility. Mr Ghani is already writing to world leaders seeking help (money, inevitably) to kick-start his reforms.

His immediate task is to sign a bilateral security agreement (BSA) with America, which Mr Karzai capriciously refused to do. A BSA would pave the way for 9,800 American troops and about 4,000 from other NATO countries to stay on past December to “train, advise and assist” Afghan security forces. It also locks in the commitment by the United States and its allies to keep picking up the $5.5 billion annual tab for the country’s 350,000-strong army and police force, which is now doing almost all the fighting and dying in the long counter-insurgency against the Taliban.

The political uncertainty has coincided with and contributed to what is likely to be the bloodiest fighting season of the 13-year conflict. The United Nations says that civilian casualties, mostly caused by the Taliban, are running at record levels. The Afghan army and police have been dying at a rate of about 100 a week, says General Joseph Dunford, outgoing commander of the NATO-led security mission.

Moreover, while Afghan security forces have been fighting well, the Taliban have been taking advantage of the withdrawal of foreign air power to mount large-scale attacks across the country. Yet the insurgents still have little popular support. If the new government can deal with the country’s most pressing needs—jobs, local justice and governance—the Taliban may find themselves forced to come to the negotiating table at last.