An Afghan National Army soldier searches passengers at a checkpoint near the Marshal Fahim National Defense University, a training complex on the outskirts of Kabul on Aug. 6, 2014. (Shah Marai/AFP/Getty Images)

Maj. Gen. Harold J. Greene, the highest-ranking U.S. military officer killed in a war zone in four decades, died not at the hand of a sworn enemy but from a burst of gunfire by a soldier in an allied army who had been largely paid, trained and equipped with American and NATO support.

It will probably never be known what led the shooter, identified as a man in his 20s, to hide in a bathroom at a military training base near the capital Tuesday, then emerge and open fire on a delegation of visiting American and European military officers, before being shot dead himself.

It was also unclear what provoked two other “insider attacks” this week: a firefight Tuesday between an Afghan police guard and NATO troops near the governor’s office in southern Paktia province, and an incident Wednesday in Uruzgan province in which an Afghan policeman poisoned his colleagues’ food, then shot at least seven of them before fleeing in a police truck, officials said.

But the troubled 11-year history of the post-Taliban Afghan security forces, including the Afghan army, offers an ample range of possible explanations for such deeply disturbing incidents, whether aimed at Afghan cohorts or foreign military dignitaries.

The army, the most professional and popular of the new defense forces, has drawn recruits from across the country who have been expected to replace local and ethnic loyalties with adherence to a national government and its defense. The aim has been to forge an army of about 80,000 men and officers who could be weaned from foreign tutelage by now and prepared to take on the Taliban alone, then gradually grow to as many as 120,000 troops.

From the beginning, however, the project has been plagued with problems. Soldiers have gone AWOL and deserted in high numbers. Ethnic imbalances between officers and troops have been sources of envy and friction. Equipment has been old and expensive to replace.

Perhaps most problematic, the American mentors who have “embedded” with Afghan units were slow to arrive, and Afghan fighting traditions — honed over decades of anti-Soviet guerrilla combat and civil war — have been both more brutal and egalitarian than the orderly American ethos of haircuts, salutes and pre-dawn drills.

In a 2009 report on the state of the Afghan army, the Rand Corp. and the Royal Danish Defense College found that while steady improvements were being made in professional skills and combat readiness, the army was still very much a “work in progress” and would need continued international support for the foreseeable future. Despite significant gains in some areas, the report said, “operational effectiveness remains very much in the balance.”

Five years later, some problems have eased but others have arisen. American military officials report that Afghan troops participate in all combat operations against the Taliban and lead at least half of them. The domestic popularity of the force has grown, pay has increased and desertions have shrunk. But reports of high-level corruption have soured morale below, and enthusiasm for the fight has faltered as Taliban insurgents have become better armed and more rapacious.

One of the most vexing developments has been the spread of insider attacks, in which Afghan personnel have opened fire on their foreign military counterparts. The phenomenon became noticeable in 2008 and surged for the next several years. In 2012, there were 60 such attacks, including the fatal shooting of two American advisers by a government worker inside the Interior Ministry. By June of this year, 87 insider attacks had killed 142 coalition troops and wounded another 165, according to the Long War Journal, an online publication focused on counterterrorism and Islamic radicalism.

The motives behind these attacks have ranged widely. In some cases, insurgents infiltrated the services and waited for the chance to attack foreign troops. In others, Afghan soldiers and police attacked their American trainers after taking offense at certain orders or perceived insults. Some have been angered by civilian bombings or reports of Korans being burned at U.S. bases. Others have professed Taliban sympathies or railed at U.S. foreign policy in the Islamic world.

The fatal attack on Tuesday was an acute embarrassment to the Afghan military leadership, because it occurred inside the Afghan equivalent of the U.S. military academy at West Point, and was aimed at a Western VIP delegation that had come to assess the army’s progress in being able to defend the nation as Western forces prepare to leave.

Afghan officials said the shooter, who used the single name Rafiqullah, had just returned from a patrol around midday and was still carrying his weapon when he concealed himself in a bathroom within close range of the delegation, then opened fire. His weapon, described as either an assault rifle or a machine gun, would have been issued by NATO. More than a dozen people were wounded, including eight Americans, a German general and a top Afghan commander of the training facility.

Officials said there was no indication that he was part of a conspiracy or had Taliban sympathies. But the timing of the attack was particularly sensitive, with presidential elections derailed by charges of fraud and an audit of all 8.1 million ballots repeatedly suspended by disagreements. Afghans are hoping to have a new leader inaugurated in time for a NATO summit in early September, and a stalled bilateral security agreement between Afghanistan and the United States is on hold until a new government takes office in Kabul.

The number and scope of Taliban insurgent attacks has been increasing in recent months, with dozens of deadly incidents involving unusually large numbers of insurgents. Officials have said the Taliban is testing the strength of Afghan security forces as U.S. and NATO troops continue their withdrawal and prepare to place the nation’s defense largely in Afghan hands.

Several analysts in Kabul said the attack exposed deep flaws in the control and competence of Afghan military leaders, who had apparently not prepared adequate security for the foreign visit. They also said it revealed ongoing problems with the army’s lax recruitment policies and faltering efforts to build a loyal, unified fighting force after more than a decade of foreign investment and training.

“This sad event is a major blow to our international alliances, and it shows that we cannot build trustworthy and credible military institutions,” said Javed Kohistani, a military analyst, former Afghan army officer and former national intelligence officer. “Whoever was behind this attack has achieved their highest goal. It is no coincidence that a two-star American general was killed.”

But Martine van Bijlert, of the Afghan Analysts Network, said she believed the shooting was an “act of opportunity” by a soldier who happened to be in the base and was not an indication of any broader conspiracy. A spokesman for the Taliban issued a statement praising the soldier for his “honorable” action in attacking foreign “occupiers” but did not claim any connection.

“Symbolically this is very telling, but whether it is telling about the broader ability of the army and its international relations, I don’t think so,” van Bijlert said. Despite intensified screening efforts and other precautions, she pointed out, “you still can’t know the thoughts and moods of every soldier and recruit. It would be different if this had been a planned and targeted attack, but it looks like this was more a target of opportunity.”