MORE than 13 years after NATO launched its invasion of Afghanistan following the attacks of September 11th, its International Security Assistance Force (ISAF) this week lowered the flag at its headquarters in Kabul, the capital. The ceremony brought to a formal end ISAF’s combat mission in Afghanistan. It was a moment for reflection on what went wrong and what went right for the West’s military intervention, and a moment also to consider what kind of future beckons for a country that has suffered nearly four decades of war and insurgency.

This week’s ceremony marked no dramatic change in the security landscape. Since a peak in 2011 of 140,000 foreign troops, over two-thirds of them American, the number has fallen steadily to 13,000. That is about the size of force that will stay on to “train, advise and assist” the Afghan National Security Forces (ANSF) for another year at least. It is pretty much what they have been doing since mid-2013, when the ANSF took the lead in fighting the Taliban in every province.

President Barack Obama has said that he wants all American troops out of Afghanistan by the end of 2016, but that plan could well change. He has recently reversed a short-sighted decision not to provide air support for the ANSF after the end of 2014. The “assist” bit of operation Resolute Support, as the new mission is called, had looked threadbare. Now NATO will continue to provide ground-attack, logistical and limited medical-evacuation support for Afghan forces until their own air force can fill the gap—at least a couple of years away. Special forces are also likely to carry out some counter-terrorism operations in partnership with the Afghans.

A chief reason for the rethink is that the violence generated by the Taliban insurgency remains uncomfortably high. Although the 340,000-strong Afghan forces are performing well, they are suffering heavy casualties. Over 4,600 have been killed in action this year, a 6.5% rise on 2013. The violence has not dropped off much with the end of this year’s traditional fighting season. The Taliban now appear to be concentrating on mounting terror attacks in cities, especially Kabul, which create news headlines and are aimed at forcing out foreign civilians and frightening investment away.

Yet while the Taliban have very clearly not been defeated militarily, politically they have been routed. There was a good turnout in two rounds of the presidential election earlier this year—with nearly two-fifths of the votes cast by women—despite Taliban attempts at intimidation and disruption. The government of national unity which resulted after much wrangling and allegations of fraud is in the view of some a vindication of the West’s intervention, whatever its cost in money and lives.

Both President Ashraf Ghani, a former World Bank technocrat, and Abdullah Abdullah, Mr Ghani’s former rival and now prime minister in all but name, are committed to breaking with the seedy legacy of Mr Ghani’s capricious predecessor, Hamid Karzai. How well they will work together remains uncertain (there is still haggling over cabinet posts). But they say they are united over an agenda that includes rooting out corruption, improving the still-awful state of basic services such as health care and criminal justice, re-emphasising women’s rights and attempting to reset relations with Pakistan.

A recent nationwide opinion poll gave Mr Ghani an 84% approval rating. Even in the south, where the Taliban holds greatest sway, 76% were satisfied with his performance. Mr Ghani has also transformed sentiment among foreign donors of the aid on which Afghanistan’s fragile economy depends. At an aid conference in London in early December, America’s secretary of state, John Kerry, described Mr Ghani’s reform programme as “an extraordinary moment of transformation”.

Mr Ghani’s approach is emblematic of a society that is a world away from the wretched, near-failed state that the country had become by 2001, following Soviet invasion and withdrawal, civil war and, from 1996, rule by the Taliban. Peter Thomsen, George H.W. Bush’s special envoy to Afghanistan, has recently argued that whatever its mistakes, the American-led intervention reversed a slide into chaos and put an end to extremist tyranny and terrorist incubation.

Yet the mistakes also have to be acknowledged—and learned from. Perhaps the biggest was not starting the hard job of training a proper Afghan army until late-2009 rather than after the invasion, when relative calm prevailed for several years. However, when Taliban insurgents, with help from Pakistan, began returning in large numbers and the fighting intensified, it meant that more and more foreign troops were needed to prevent them from taking over large swathes of the country. That played into the Taliban narrative of foreign occupation. Much debate has taken place about how counter-insurgency campaigns should be conducted, but one thing is certain: to be effective, indigenous troops must be in the lead, supported by foreign forces—not the other way round.

The other great mistake was to underestimate the corrosive effects of Pakistan’s playing of a double game, appearing to make itself the West’s indispensable ally against jihadist terrorism—while providing the Afghan Taliban with sanctuary, training and money. The delusion that Pakistan was, for all its trickiness, basically “on our side” made it almost impossible to achieve a lasting victory against the Taliban.

Now co-operate

Much now depends on whether Pakistan, Afghanistan and America see the need to improve their fraught triangular relationship. Some change does appear to be taking place. Pakistan’s army, which still calls most of the shots in the country, is now led by a general, Raheel Sharif, who some say is a breath of fresh air (last month he was feted during a two-week stay in America). Unlike his predecessors, General Sharif appears to see jihadists, principally in the form of Pakistan’s own Taliban, as the country’s greatest threat, and has sought the help of the Americans in countering it.

In recent weeks an unprecedented upsurge in operations against militants has taken place on both sides of the Afghanistan-Pakistan border. The Americans have been using drones to hammer Pakistani Taliban sanctuaries in Kunar in Afghanistan, while the Pakistanis have, for the first time, targeted the Haqqani network, responsible for plotting some of the most daring recent attacks on Kabul from its base in Pakistan’s North Waziristan tribal area.

For his part, Mr Ghani is trying to acknowledge long-standing Pakistani concerns ignored by Mr Karzai. Confidence-building measures include an agreement to send Afghan officers for training in Pakistan, and a bilateral agreement on managing the border. Mr Ghani has also refused to blame Pakistan for recent militant attacks and is keen to agree on a programme to improve energy and transport links.

Little would boost Afghanistan’s prospects more than an end to Pakistan’s ultimately self-defeating meddling. It is far too soon to know whether Mr Ghani can succeed in his plan to establish something more constructive in the two countries’ relations, above all, persuading Pakistan to help bring the Afghan Taliban to the peace table. Should Mr Ghani be able to achieve that, it would certainly be time to talk about a new era.