As the great British spymaster Sir Claude Wade warned on the eve of the 1839 invasion, “There is nothing more to be dreaded or guarded against, I think, than the overweening confidence with which we are too often accustomed to regard the excellence of our own institutions, and the anxiety that we display to introduce them in new and untried soils.” In this early critique of democracy promotion, he concluded, “Such interference will always lead to acrimonious disputes, if not to a violent reaction.”

Just as Britain’s inability to cope with the Afghan uprising of 1841-2 stemmed from leadership failures and the breakdown of ties between the British envoy and Shah Shuja, the strained and uneasy relationship of NATO leaders with Mr. Karzai has been a crucial factor in America’s failures in the latest imbroglio.

Afghanistan is so poor that the occupation can’t be financed through natural resource wealth or taxation. Today, America is spending more than a $100 billion a year in Afghanistan: it costs more to keep Marine battalions in two districts of Helmand than America is providing to the entire nation of Egypt in military and development assistance. And then, as now, the decision to withdraw troops has turned on factors with little relevance to Afghanistan, namely the state of the occupier’s troubled economy and the vagaries of politics back home.

History never repeats itself exactly, and there are some important differences between what is taking place in Afghanistan today and what took place during the 1840s. There is no unifying figure at the center of the resistance, recognized by all Afghans as a symbol of legitimacy and justice: Mullah Omar is no Dost Mohammed or Wazir Akbar Khan, and the tribes have not united behind a single leader as they did in the 1840s.

Moreover, the goals of the conservative, defensive tribal uprising that brought colonial rule to an end were very different from those of today’s Taliban, who wish to reimpose an imported Wahhabi ideology on Afghanistan’s diverse religious cultures. And most important, Mr. Karzai has tried to establish a broad-based, democratic government, which, for all its many flaws and prodigious corruption, is still much more representative and popular than the regime of Shah Shuja ever was.

Mr. Karzai is keen to learn the lessons of his forebears’ failures. When my book came out in India in January, he got hold of a copy and read it. “Our so-called current allies behave to us just as the British did to Shah Shuja,” he told me. “They have squandered the opportunity given to them by the Afghan people.”

Mr. Karzai believes that Shah Shuja didn’t stress his independence enough, and he made clear that in his own last year in office he is going to act in such a way that he will never be remembered as anyone’s puppet.