Exactly three years after it was declared, the Islamic State is now near defeat. The Iraqi Army has liberated Mosul, the largest city under ISIS control, while a Syrian militia has penetrated the Old City section of Raqqa, the capital of the pseudo-caliphate. U.S. air strikes—at a cost of more than thirteen million dollars a day—plus Army advisers and teams of Special Forces were pivotal in both campaigns, launched late last fall. But it is far too soon to celebrate. Since the rise of jihadi extremism four decades ago, its most enduring trait, through ever-evolving manifestations, is its ability to reinvent and revive movements that appeared beaten.

Osama bin Laden slunk out of Afghanistan in disgrace, in 1989, after his miscalculations contributed to the deaths of thousands of Arab fighters. He was expelled from Sudan and lost his Saudi citizenship in the nineteen-nineties. He reëmerged to carry out the 2001 attacks on the World Trade Center and the Pentagon, but then was forced to flee Afghanistan. He abandoned his own fighters at Tora Bora, to go into hiding. A decade later, he was killed in a lightning raid by Navy SEALs. His body was dumped at sea.

“Each time, Al Qaeda has seemed doomed to fail but has actually recovered and come back stronger,” Ali Soufan recounts in his exceptional new book, “The Anatomy of Terror: From the Death of bin Laden to the Rise of the Islamic State.” Today, Soufan argues, Al Qaeda is more dangerous than ever. “This new model has not only survived its founder’s death; it has expanded its membership exponentially.”

ISIS has followed a similar pattern. In its first iteration in Iraq, the group was “on the brink of collapse in 2007 and 2008—its senior leadership either dead, in hiding, or in prison,” Soufan notes. Tens of thousands of Iraqi tribesmen, backed by the United States, had turned on it. Its jihadi ranks were decimated to a few dozen men forced into the underground. Within seven years, however, its new leader, Abu Bakr al-Baghdadi, had reorganized and rebranded his group. By 2015, it had attracted more than thirty thousand fighters, from a hundred countries, to fight for the first modern caliphate. Despite an appalling death rate on the battlefield, of roughly ten thousand fighters a year, thousands more kept coming.

To mark the ouster of ISIS from Mosul, Iraq has announced a week of celebrations. Baghdadi long ago fled Mosul, where he first declared the caliphate; the Russians claimed to have killed him during an air strike in Syria. His fate is unclear, but the ISIS experiment in governance—over an area the size of Indiana or of the country of Jordan—has definitively failed. Iraqi Prime Minister Haider al-Abadi is visiting Mosul today in a show of restored sovereignty. In a statement, Abadi “congratulated the heroic fighters and Iraqi people for the great victory.” The U.S.-led coalition is scheduled to meet in Washington this week to discuss stabilization in liberated areas of Iraq and Syria.

Yet, Soufan warns, “the growing strength of its ‘provinces,’ particularly in Libya, suggests that, like Al Qaeda before it, the Islamic State may be poised to evolve into a multinational umbrella group with a number of franchises.” At its height, ISIS spawned eight branches, with thirty-seven wilayats, or “provinces”—from Algeria to the Caucasus, Afghanistan to Yemen—though some are weak, dormant, or have been crushed. The conditions that originally spawned militant jihadism still plague Iraq, Syria, and much of the world’s most volatile region.

Militant jihadism is a subject that Soufan, a Lebanese-American, knows well. A former F.B.I. special agent, he has been tracking Al Qaeda since bin Laden’s first declaration of war on the United States, two decades ago. He assembled evidence that linked Al Qaeda’s first attacks against the United States, in 1998, when it hit two U.S. embassies in Kenya and Tanzania, to bin Laden. At the time of the 9/11 attacks, he was among only a handful of Arabic speakers in the F.B.I., and he subsequently interrogated Al Qaeda prisoners. (Lawrence Wright profiled him for The New Yorker in 2006.)

One of the most intriguing themes of Soufan’s second book—his first, “The Black Banners,” was a Times best-seller—is the discord, feuding, and even deadly infighting among the jihadis themselves. It has been a subplot for decades. The biggest schism today is between Al Qaeda and ISIS, the world’s two deadliest terrorist groups. It erupted in February, 2014, after ISIS started seizing territory. Baghdadi claimed supremacy in the jihadi world; he believed ISIS was “a real governing entity and therefore superior to any mere jihadi organization, including its parent group, Al Qaeda,” Soufan recounts. Baghdadi began to defy Al Qaeda’s orders, including the order to stop murdering fellow-Muslims and to coöperate with other franchises. He grew more murderous in the name of his emerging caliphate.

In a message posted online, Al Qaeda chief Ayman al-Zawahiri unleashed his fury, publicly denouncing Baghdadi and disclaiming responsibility for ISIS actions. Zawahiri, who had succeeded Osama bin Laden, later explained that it was better to have ten decent followers than “scores of thousands making the umma [Muslim community] hate them, their deeds and their behaviors.”

Once parent and child, the two groups began to loathe each other almost as much as they hated their common enemy in the West. Three years later, it is still one of the least understood—or exploited—aspects of the global jihad.

The split is important in understanding what may happen after the caliphate’s collapse. Al Qaeda and ISIS were not simply competing for power and position in the world of jihadism; they differed profoundly over strategy. Al Qaeda counselled patience on the road to establishing a caliphate. Its three-pronged strategy was outlined in the treatise “The Management of Savagery,” which was published in its magazine Sawt al-Jihad. The first phase is fomenting chaos and exploiting the power vacuums that follow. The second phase is mobilizing popular support for fundamentalist Salafi rule, based on the ways of life and governance when the faith was founded, by the Prophet Mohammed, in the seventh century. For Al Qaeda, an Islamic state “could never be sustainable until a majority of its citizens could be convinced that it was the right way to govern,” Soufan explains. Only in the final phase can jihadi groups create a caliphate. Bin Laden repeatedly denied permission for Al Qaeda branches to declare an Islamic emirate or try to govern. He even opined that the return of the caliphate might not happen in his lifetime.

ISIS had a different blueprint. It plotted to seize land and then forcibly compel Muslims to join its ranks—on pain of death. Baghdadi “could not care less about consensus building,” Soufan writes. The ISIS Emir grew bolder in his claims, in mid-2014, when his fighters seized more land than the Prophet Mohammed initially ruled at the time of Islam’s founding.

The two movements, “with their competing absolutist positions, had reached an impasse; it was increasingly clear that . . . this jihad was not big enough for both of them,” Soufan says. “Today, with the split between Al Qaeda and the Islamic State, we are witnessing a clash within the Salafi-jihadi strain itself, a division not about the end goal—both sides believe in a restored caliphate—but how quickly to get there.”

Jihadism has long been riddled with rifts not only between groups but also within them—even inside Al Qaeda. Saif al-Adel, an Egyptian and one of bin Laden’s top-three aides, assembled the cell that attacked two U.S. embassies in Africa in 1998. Yet he—like most of the Shura Council that advised bin Laden—opposed the September 11th attacks, in 2001, warning that the repercussions were too dangerous. Within the Taliban, some supported bin Laden, but others opposed him; today, it has factions for and against negotiating with the U.S.-backed government in Kabul. ISIS has experienced splits as well. Documents captured in raids on ISIS safe houses or left behind have included letters—some with complaints, others questioning merciless tactics.