Two decades after its invasion of Afghanistan, the United States has come to the inevitable conclusion that it must end its military intervention there.

Unable to win against the Taliban insurgency, Washington has finally given up on a military solution and embraced negotiations, dictated largely by those it long deemed terrorist fundamentalist killers "swathed in American blood".

The decision has been a long time coming, but successive US administrations have persistently avoided it, insisting that the US was "making progress", if not outright winning.

The secret documents dubbed the "Afghanistan Papers" released last year revealed a long-standing official policy of deliberately deceiving the American public into thinking everything was fine on the war front when in fact nothing was.

Just like the 1971 "Pentagon Papers" about the war in Vietnam, the new revelations made it clear that this was also an unwinnable war and that it was only a matter of time before the US pulled out.

In this context, the deal reached with the Taliban in Doha this month may soften the blow, but the compromises are no less humbling for the US and quite unsettling for its Afghan allies.

Failing successfully

It took the US and its allies only two months to "liberate" Kabul from the grip of the Taliban and less than two years for the then-Defense Secretary Donald Rumsfeld to declare at a May 1, 2003, news conference in Kabul that "major combat activity" was over.

This was the same day President George W Bush proclaimed rather bombastically that the war in Iraq was "mission accomplished".

The US did disrupt, dismantle and defeat al-Qaeda in Afghanistan, at least militarily, and in 2011 killed its leader Osama bin Laden, but all subsequent military surges and strategies failed to crush or contain the Taliban.

Instead of declaring victory against al-Qaeda and calling it a day, considering that the Taliban took no part in the 9/11 attacks on New York and Washington, the US persisted in its war efforts.

But Afghanistan like Iraq soon turned into a huge quagmire for the US-led coalition. Its tough terrain, tribalism, and the tenacious Taliban, all made it impossible to win the war.

Indeed, the Taliban grew even stronger and deadlier in the second decade of the conflict, exacting a heavy price on the Americans, their coalition and their Afghan allies.

After 18 years of war, some 2,400 US soldiers and more than 150,000 Afghans killed, Washington has finally accepted the reality of its defeat. In the process, the US has spent, not to say wasted, over a trillion or according to some, two trillion dollars on the war, almost 1,000 times more than Afghanistan's GDP.

Once again, the imperial power of the day has lost to indigenous fighters, finally realising that when it fights a relatively weaker party for too long, it also becomes weak.

After Vietnam and Iraq, the US seems to have lost its third major war and the longest of them all.

The only remaining question for the US was how best to avoid a repetition of the fall of Saigon and the humiliating scene of the last US helicopter fleeing South Vietnam.

Saving face

To put an end to continuing losses in blood, pride and treasure, two years ago, the US finally embraced the idea of a negotiated settlement with the Taliban, largely on the armed group's own terms.

The US had wanted the Taliban to start talks first with the Afghan government in order to reach a national accord on the future of the country and governing system, but the Taliban leaders refused to negotiate with the "US puppet" in Kabul. They insisted on direct negotiation with Washington over a US military withdrawal before any dialogue with the Afghan authorities.

The US caved in - or, as former US ambassador to Afghanistan Ryan Crocker called it - surrendered. For a year and a half, it engaged in talks with the Taliban in Doha. It pushed them forward even after US President Donald Trump called them off temporarily after the death of a US soldier in a suicide bombing in September last year.

Last week, an agreement was finalised, at least "in principle".

Before proceeding with the signing, the US insisted that the Taliban agree to a week of reduction in violence to show it controls all of the various armed groups in the country, and commits to denying any hostile group like al-Qaeda a haven in Afghanistan.

The Taliban agreed.

But the armed group refused to commit to a particular American, democratic or liberal vision for the future Afghanistan, and insisted on the release of its imprisoned members before it began negotiations with the central government in Kabul.

Diplomacy once again reflected not the absolute power of either party or the primacy of international law, but the balance of power on the ground.

It is, in fact, a symmetrical diplomatic process with the clear objective of restoring Afghan sovereignty and independence free of foreign forces, no "ifs", no "buts", no "maybes".

The electoral calculus

President Trump began his tenure by talking tough, making threats and even warning of "tens of millions killed" in Afghanistan, but the Taliban leaders were not impressed. They called his bluff through further escalation, believing time was on their side.

As the US election countdown clock ticked, the president became ever more eager to make a deal.

He even floated the idea of a Camp David summit to end the war in Afghanistan, but the Taliban rejected it.

The US president may lack certain qualities, but tenacity is not one of them. He has already made good on his pledge to walk away from important international agreements, erect a wall along the border with Mexico and move the US embassy in Israel from Tel Aviv to Jerusalem.

And now comes the time to reduce US military footprints in the greater Middle East, notably in Afghanistan, especially after Trump deployed more troops to the Gulf region to deter Iran.

If it works out, this deal would be a significant accomplishment for the Trump administration, considering the broad public consensus in the US over the need to end the war in Afghanistan as soon as possible. Bringing to a close the US withdrawal which the Obama administration started but could not finish may well prove to be one of his greatest electoral assets

Lessons learned?

Afghans have lived in horror and devastation for the past four decades, starting with the 1979 Soviet invasion, which ushered in a civil war, and then the US invasion of 2001.

The country's war economy has bred corruption and expanded the narcotics-dominated black market, which produces some 80 percent of the world's opium.

Even if diplomacy succeeds, and the foreign forces leave, it is going to take a miracle for Afghans to reach national reconciliation, rebuild their nation, and put the country on the path to normalcy.

Despite the calming words of deputy Taliban leader Sirajuddin Haqqani in an op-ed in the New York Times last week, many Afghans are afraid of a vengeful, uncompromising Taliban.

The armed group could come back with a vengeance in Kabul and re-impose its ultraconservative ways on Afghan politics and society, facing little resistance from a weak and inept central government unable to protect the few political and social reforms enacted over the past two decades.

A lot will depend on the tenacity of Afghan society and its ability to engage in nation-building on its own, without the dictates of outside powers.

It is said, the wise learn from the mistakes of others, the smart learn from their own mistakes, and the foolish learn from neither.

The Americans may have finally learned their history lesson: all empires, from the Persian and the Mongol to the British and Soviet, have suffered defeat in Afghanistan.

It is the graveyard of empires, stupid.

If the US-Taliban agreement holds, and that is a big IF, the Afghans too will have the opportunity to draw their own lessons.

Will they stop al-Qaeda and ISIS type groups from making Afghanistan their safe haven? Will they begin the process of healing to end the suffering?

May Afghans, especially the Taliban prove as resilient in the pursuit of peace as they have been in waging war.