Gen. John R. Allen, right, says goodbye to U.S. and Afghan troops during his last visit to southern Afghanistan. (Photo by Master Sgt. Kap Kim/COMISAF Public Affairs)

Gen. John R. Allen was leaving Helmand province for the last time, his jet flying high above the desert moonscape that saw a bloody American military campaign and then an unprecedented withdrawal of U.S. troops.

Allen arrived here 18 months ago, charged with two seemingly contradictory goals: to defuse an active insurgency and bring home 30,000 U.S. soldiers and Marines. Now, after the departure of a third of his force, it’s Allen’s turn to leave Afghanistan, where he says hard-fought security gains have often failed to bring about effective governance that would ensure long-term stability.

“In some ways, it feels like I’m leaving family behind to an uncertain future,” he said, looking out at the rugged expanse of southern Afghanistan last week.

Since 2001, there have been 11 commanders of U.S. forces in Afghanistan, five of whom, like Allen, also commanded NATO forces. But no other general was forced to focus in equal measure on both fighting a war and withdrawing from it.

That meant putting Afghan soldiers in charge of combat operations even as many observers said they were unprepared. It meant building trust with an Afghan president known for intransigence, and fighting the Taliban while support waned in the United States. Those were the predictable challenges.

Then there were the unexpected tests: the insider attacks, the burning of Korans on a U.S. military base, the Pentagon investigation into a trove of Allen’s e-mails for possible sexual impropriety — which cleared Allen of wrongdoing last week.

As his command was buffeted, Allen impressed subordinate commanders with an ability to straddle Washington’s demands for a military endgame with Afghan demands for long-term reassurance. But he says he recognized that the U.S. drawdown would not coincide with clear-cut victory — in many cases, security gains have only brought more questions.

With 11 days left in his tour, Allen says he’s proud of the growth of the Afghan security forces and the success of NATO’s troop surge in places such as southern Helmand, where four years ago the Taliban operated freely.

But if security is not accompanied by effective governance, the Taliban could return, he says, or criminal networks could corrode gains.

“Now what they face is an absence of governance and a desire for law enforcement and legal stability,” Allen said of civilians in parts of the country that have seen strategic gains.

He has been pressing Afghan officials, including President Ha­mid Karzai, to deliver governance in places where military gains are visible. But in many cases, patronage networks have prevailed or corrupt leaders have remained in power as public confidence diminished.

“General Allen works for his own country. We work for the national interests of our country,” Abdul Karim Khurram, Karzai’s chief of staff, said of Allen and Karzai’s sometimes divergent views.

Meanwhile, there has been pressure from Allen’s own government. The next phase of the drawdown — and questions about Washington’s appetite for a military presence in Afghanistan beyond 2014 — have hovered over his command.

Allen wants to keep between 9,000 and 15,000 U.S. troops in Afghanistan, according to American officials who were not authorized to speak publicly about the subject, while the Obama administration is considering a force of about half that size. White House officials told reporters this month that zero troops might remain.

“When you have political appointees talking about a zero option, it doesn’t make things any easier,” said a U.S. military official on Allen’s staff.

Before Allen departed Helmand last week, the top Afghan commander there, Sayed Malouk, gave him a valedictory speech in a room full of top U.S. and Afghan military brass.

“We have full confidence that you and your other generals will give the best advice to President Obama — to support this mission after 2014,” Malouk said, looking at Allen. “Blood and fight ours, tools and support yours.”

Allen managed a tight-lipped smile while many other officers in the crowd laughed.

“You might have seen me squirming in my seat,” Allen said later, as he returned to Kabul.

Allen won accolades as a three-star general in 2007, when he seized on the tribal dynamics of western Iraq to persuade Sunni elders to resist the insurgency. His handling of the “Anbar Awakening” made him Obama’s first choice to replace Gen. David H. Petraeus in July 2011.

In Afghanistan, Allen did not have the same resources as commanders in Iraq or his predecessors in Kabul. In Washington, he lacked their celebrity. But he still battled a potent insurgency.

About 640 NATO and U.S. troops have been killed in Afghanistan since Allen took over.

“Those are the ones that are with you in the middle of the night, and you can’t sleep when you think about them,” Allen said, his voice breaking.

As Allen and the NATO-led International Security Assistance Force hand the reins to Afghan forces, fatalities have become a grim metric for the state of the transition. More than 1,000 Afghan soldiers died in 2012, about a 20 percent increase from 2011, a marker of who is doing the bulk of the fighting, U.S. and Afghan officials say.

Allen “pivoted the Afghan army and police to take the lead while we were engaged in combat,” said Marc Chretien, Allen’s political adviser, who also served with him in Iraq.

Afghan soldiers have paid dearly, Allen said, and “now the knockout blow needs to be delivered by the Afghan government.”

“Meanwhile, those corps commanders are going to continue to bleed,” he said of the top seven Afghan generals.

Poor governance could result in a Taliban resurgence or a rise in criminal operations, Allen said. Patronage networks and fledgling Afghan institutions are “locked in a death struggle,” he said.

That institutional weakness in part prompted Allen’s recommendation to Obama for a post-2014 U.S. presence. His top priority for beyond 2014, he said, is to deploy a significant number as advisers in the Afghan ministries of Defense and Interior. Then, troop levels permitting, he would assign others to military training centers in Kabul and possibly keep some advisers at regional Afghan police and army commands.

Traveling the country for the last time, Allen was eager to put Afghanistan’s woes in context. There are tensions with Pakistan, he noted, and a recent history dominated by upheavals.

But the context Allen cites most in farewell speeches relates to what he calls “young democracies” – countries with flashes of promise but a great need for support and long, uncertain roads ahead.

“I really believe we can win this thing,” he said, but “winning won’t occur between now and 2014. We will set the conditions to win during that decade of transformation.”