Garvan Walshe is a former National and International Security Policy Adviser to the Conservative Party

As the rout in Afghanistan came to its shameful conclusion, most of our focus turned on ourselves. It was a war of necessity, provoked by the 9/11 terrorist attacks, and a defeat of choice.

The speed with which the Afghan army collapsed surprised many, but cannot have caught out people who knew how dependent it was on air support. Once American technicians and advisers left, it is hardly suprising that morale in the Afghan security forces, not particularly high in any event, fell away completely.

Our tactical failures have been compounded by the moral consequences of defeat. We’ve abandoned thousands of Afghans who helped the Western mission there, and millions who depended on it for the protection of the most basic human rights, including the women who are now left to rot under the Taliban’s systematic regime of misogyny. The shame belongs to us because we failed to win.

The United States excepted, which has its Special Immigrant Visa programme, we, the countries who sent thousands of soldiers over there 20 years ago, now outdo ourselves in wourking out how to meet the letter of the Refugee Convention while letting the minimum number of unarmed Afghans actually arrive to our shores. Have we become so befuddled by loudmouthed populists that we are unable to find it in our hearts to offer them sanctuary, and in our heads to work out how they can become part of our society?

Much of this has been entirely avoidable. Far from doing our best in an extremely difficult situation, we piled errors upon each other. I won’t list them here, but think it’s important to focus on one which has been particularly fateful: the failure to address the Pakistan dimension at the highest political level.

We intervened in an Afghanistan that had spent decades at war. It had become a battleground between the Soviet Union to the north and Pakistan to the south. Though Pakistan had itself taken an Islamist turn under Zia ul-Haq, the Soviet involvment drew in the United States, and the rivalry with revolutionary Iran drew in Saudi Arabia to support the anti-Soviet Mujahideen, Pakistan’s interest was more geographical than ideological.

Since losing the Western half of itself to Bangladeshi independence, Pakistan’s policy has been driven by the fear of another disastrous war with India. Its overwhelming focus has been on what it calls “strategic depth”, by which it means the ability to retreat to the mountains around the Afghan frontier, in order to wait out a numerically overwhelming Indian advance.

For Pakistan, which was aligned with the West, this clearly needed a friendly, not a pro-Soviet, Afghanistan. The alliance and cultivation of radical Islamic fighters, in which Pakistan’s Interservices Intelligence (ISI) was instrumental, and which would later become the Taliban, was born.

Though Pakistan was formally a close American ally, operated American military equipment, and agreed to allow the United States access to Pakistan for its mission against Osama bin-Laden, it has spent the last two decades playing both sides. It is no coincidence that Osama bin Laden was killed in Abbottabad in Pakistan, not Afghanistan, nor that the Taliban leadership continued to live semi-openly even in Islamabad’s suburbs.

The Taliban were simply too important to Pakistan, because of their role in Afghan politics, to abandon. Indeed, rather than abandon them, it seems that Pakistan has tolerated and, most likely, enabled the funding and equipping of a large force responsible for the deaths of 2,500 Western troops and 45,000 of their Afghan allies.

All this is known in professional circles, but is largely missing from the public debate about the Afghan war, which imagined it as a two-way contest between Islamist rebels, and a Western-supported Afghan government – as though Afghanistan could be insulated from its geographic environment.

In public, though not necessarily in private, it was given less prominence than the mission to eradicate opium production and, after Bin Laden was killed, our attention waivered to the more immediately pressing eruption of Isis.

Addressing the Pakistani dimension would not have been easy. Pakistan faced its own Islamist insurgency on its own side of the Durand line. Imran Khan’s government is weak, and the ISI highly autonomous. Pakistan’s interest in stability on its northern frontier is legitimate, but we had an equally legitinate interest in their not using the Taliban to do so.

Now we’re paying the price for our neglect.