When General Raheel Sharif was appointed as Pakistan’s Army chief last week, a flurry of profiles described the new occupant of the country’s most powerful office as a “moderate” and “professional” soldier, with “no interest in politics.” In a country that has spent half its history under military rule, this is a polite way of saying that General Sharif is unlikely to overthrow the government. For Prime Minister Nawaz Sharif, who selected the new Army chief, this disinclination to political involvement may have been among General Sharif’s prime qualifications—Nawaz’s previous term in office came to an abrupt end, in 1999, when he was ousted by General Pervez Musharraf, whom he had handpicked to head the Army.

Nawaz Sharif’s election earlier this year marked a milestone for Pakistan: for the first time, an elected civilian government completed a full five years in office and made an orderly transfer of power to its successor. For much of those five years, speculation swirled that the government, headed by Asif Ali Zardari, the husband of the former Prime Minister Benazir Bhutto, would not survive. Now it appears that Sharif—whose party enjoys a large majority in Parliament—should be able to complete another full term of his own. If that happens, the door to further military coups, which has been slowly creaking closed, might even be firmly shut.

Future historians looking back on this period will undoubtedly focus much of their attention on the role played by General Ashfaq Parvez Kayani, who retired last Thursday, after six years as Pakistan’s Chief of Army Staff—making him the longest-serving Army chief never to take over the country. When Kayani was appointed, in 2007, U.S. officials gushed that he was “a soldier’s soldier,” a bit of praise meant to contrast him with the swaggering style of his predecessor, Musharraf. This is how Kayani would like to be remembered: as the man who made a decisive break with the past, let democracy proceed without an Army takeover, and remained primly aloof from politics. But the legacy he leaves behind is a more ambivalent one.

While Kayani deserves credit for declining to abbreviate the term of Zardari’s government, he took the unusual step of extending his own. During his long tenure, he carved out a new, sleeker role for the Army chief, retreating from active involvement in politics even as the generals kept their control of foreign policy, national security, and certain sectors of the economy—poking the elected civilians, when necessary, from behind a thin veil of non-interference. Kayani made discreet but decisive interventions from backstage, asserting his clout when he felt civilian leaders had drifted astray. He led sweeping offensives against some militants at home, but balked at taking on others. Above all, he jealously guarded the Army’s traditional interests, while almost sundering the country’s longstanding alliance with the United States.

Kayani spent three years as Pakistan’s intelligence chief under Musharraf; the two men worked closely together, and Kayani gleaned his own lessons from Musharraf’s decline. Many observers noted the differences between them: when he made rare public appearances, Kayani spoke in full, sometimes baroque Urdu sentences, never straying from his prepared remarks; Musharraf was partial to a staccato mix of unscripted English and Urdu utterances, often unburdened by the rules of grammar.

Unlike his voluble predecessor, Kayani was purposefully discreet in private meetings. “He was taciturn,” said Vali Nasr, who had several meetings with the Army chief during his tenure as an adviser to Richard Holbrooke, then the U.S. Special Representative to Afghanistan and Pakistan. “He was very reserved. He also has a tendency to mumble.” At times, Nasr said, questions posed to Kayani would be answered instead by one of the other generals in the room—insuring that the chief “seemed inaccessible.” This is how Kayani wanted to be seen: low-profile, uninterested in politics, and immersed in his duties as soldier-in-chief.

Where Musharraf, a former commando, had led the Army in politics for nine years, Kayani made a tactical withdrawal. Among his first moves was to shut down the Inter-Services Intelligence agency’s political cell, and to withdraw uniformed officers from their posts inside the government. Kayani saw his principal task as restoring the Army’s public image after the ruinous Musharraf years: he was keenly sensitive to public opinion and reluctant to take any steps that might threaten the Army’s popularity.

Before mounting campaigns against militants in the Swat Valley and South Waziristan, Kayani was careful to seek broad backing. Musharraf’s rule had seen the Army assailed by conservatives and religious hardliners for fighting “America’s war” against fellow-Muslims; slain soldiers, in some cases, were denied proper burials when their bodies were returned to their villages. Kayani wanted the civilians to take ownership of Pakistan’s internal war, lending it more legitimacy than it had possessed under military rule, and giving the Army a layer of additional protection against public hostility. At the same time, he pursued an aggressive public-relations strategy that included photo-op visits with soldiers, celebrations of the memory of Army martyrs, and speeches peppered with references to Islam and nationalism.

There was little incentive for Kayani to involve himself in day-to-day management of the government. The Army was in no position to repair Pakistan’s energy crisis or rouse its torpid economy; these were matters better left to civilians, who could face the blame for their failures while the Army worked to rebuild its own reputation. But this strategic distance did not mean that Kayani was content to let the government operate without pressure from the Army.

In 2008, when the government made a clumsy attempt to bring the I.S.I. under the control of the Interior Ministry, the generals objected, and the proposal was withdrawn within twenty-four hours. Later that year, another trial balloon, to send the intelligence chief to India to help with investigations in the wake of the terrorist attack on Mumbai, was swiftly shot down. In 2009, when a march led by lawyers to reinstate Pakistan’s Chief Justice, Iftikhar Chaudhury—who had twice been sacked by Musharraf—was on its way to Islamabad, Kayani stepped in and forced the government to capitulate.

Several weeks ago, Yousaf Raza Gilani, who served as prime minister from 2008 to 2012, told an interviewer that the most difficult moment of his tenure was the Army’s furious reaction to the Kerry-Lugar-Berman Act, which proposed to triple non-military aid to Pakistan. The Army opposed the bill’s “humiliating” conditions, particularly its insistence on civilian authority over the military. These nerve-jangling standoffs weakened the government, and, in the end, it decided to cede control of security and foreign policy to the Army to insure its own political survival.

For good measure, Kayani was also offered an unprecedented second term as Army chief. In July, 2010, four months before Kayani was expected to retire, Gilani appeared briefly on national television. In recognition of his “excellent military leadership qualities and pro-democracy views,” Gilani said, in yielding tones that were at odds with the apparent enthusiasm of his speechwriter, Kayani would remain in his position for another three years.

But Kayani’s delayed retirement may have helped to circumscribe his power; even as it entrenched his authority, it made a military takeover less likely. “The extension created resentment within the Army,” said Farzana Shaikh, of the Royal Institute of International Affairs in London. Other generals were unlikely to have consented to an indefinite extension of Kayani’s power. A coup, Nasr said, would also have imperilled Pakistan’s already deteriorating relations with the U.S., hobbled Pakistan’s capacity to protect its interests in Afghanistan, and may even have forced it to disengage from the war against the Pakistani Taliban in its own northwest.