Gen. Sir Richard Dannatt’s brave call for an early British withdrawal from Iraq contained one logical flaw. It did not apply to Afghanistan, he said, because foreign troops were invited by the Kabul government. This gave them a different status from coalition forces in Iraq, “which is why I have much more optimism that we can get it right in Afghanistan”. It was an odd remark since US and British forces have a standing invitation from the Baghdad government. There is a clear parallel with Afghanistan, just as there is in his core arguments: Britain’s presence in Iraq is exacerbating the security problems, and “we are in a Muslim country and Muslims’ views of foreigners in their country are quite clear”.

Both points apply to Afghanistan, where a combination of rising nationalism, impatience with Kabul’s selection of corrupt governors, anger at the coalition’s military tactics, and disappointment with its failure to improve basic services, is creating a tide of resistance. Afghan history shows that foreign interventionists, especially non-Muslims, only have a small window of time to show they are doing good. It runs out fast, particularly in the Pashtun south, the traditional heartland of opposition.

Five years after the Taleban were bombed out of power, Afghanistan is falling into the same morass of bloodletting as Iraq. Security has collapsed. A third of the country is “racked by violent insurgency”, in Kofi Annan’s words. Suicide bombings are on the rise, with 230 people killed last month.

The Taleban are resurgent. British forces are taking casualties in clashes that Brig. Ed Butler, the outgoing commander of UK forces, calls more ferocious than anything in Iraq. A retired US general, Barry McCaffrey, reported this spring that, unlike Iraq’s insurgents, the Taleban operate in battalion-sized units of 400 men, equipped with “excellent weapons and field equipment” and new technology for roadside bombs.

The conflict’s intensification reinforces the case, argued by a minority in the West after 9/11, that military attack would not solve the Taleban — or Al-Qaeda — problem. In Washington and London the desire to eliminate Al-Qaeda was wrongly combined with seeking regime change in Kabul — a goal the Security Council never authorized. A propaganda campaign demonized the Taleban so as to justify their removal as a victory, even though Osama Bin Laden might not be found.

Afghanistan: The Mirage of Peace, an excellent study by Chris Johnson and Jolyon Leslie, two aid workers with long experience of working there, describes how under the Taleban security was better than it was before or after. They also point out that the Bonn accords that followed regime change failed to provide for the demilitarization of the warlords or a role for middle-ranking supporters of the Taleban. Just as the wholesale purge of all Baathists rather than just the leadership alienated an important sector of society in Iraq, ousting every Taleban follower created serious problems in Afghanistan, although the damage was different. In Afghanistan Taleban supporters were tribal and rural. But they represented a large swathe of the Pashtun population.

After Bonn they watched and waited. Had foreign troops and all the pledges of aid for Hamid Karzai’s government produced quick benefits, Afghanistan’s new deal might have stuck. Little was done, and the Taleban was able to regroup.

Two years ago Karzai brought some former Taleban leaders into the fold. A few were elected to Parliament last year. But the only way to restore security in the Pashtun south is a comprehensive accommodation with tribal leaders, mullahs, former mujahedeen, and the Taleban forces they are related to.

This is the tack Pakistan’s Pervez Musharraf has been using in the troubled Pashtun provinces of Waziristan that border Afghanistan. He has switched to making deals, withdrawing forces in return for undertakings of no Taleban attacks on Pakistani government buildings and no parallel administration. Religious scholars, tribal elders and Pakistani officials will monitor the deal.

Musharraf explained it last month in words that echo Gen. Dannatt’s: “On our side of the border, there will be a total uprising if a foreigner enters that area. We will never allow any foreigners into that area. It’s against the culture of the people there.”

There are encouraging signs that the message is getting through in Afghanistan. A cease-fire brokered by tribal leaders has brought peace on the Musharraf model in the Musa Qala district of Helmand.

Such a deal can be replicated throughout the south, there is hope. Britain and NATO will never achieve military victory or “pacify” Afghanistan. Local reconciliation and power sharing are the only basis on which job creation and rural development can at last go forward. In this task foreign armies have no place.