Officially, America’s relations Pakistan’s military and intelligence services were in a tailspin in August. Furious at having been kept in the dark ahead of the Americans’ May 2 raid on Osama bin Laden’s compound, Pakistan’s military had kept U.S. investigators out of the place until it was scrubbed for evidence and had refused them access to bin Laden’s wives for some time. And the Pakistanis had outed the CIA’s Islamabad station chief, putting his life at risk. Meanwhile, back in America, fears were rising over possible al Qaeda attacks on the 10th anniversary of Sept. 11.

But in the shadows, far from the public rancor, Pakistani-U.S. cooperation quietly continued. In Quetta, the Taliban’s capital in exile, U.S. intelligence was monitoring the cellphone of the presumed planner of any Qaeda anniversary attacks, Younis al-Mauritani, the group’s newly named external operations chief. The Americans’ tracking data—signals intelligence, or sigint, as it’s known in the profession—was being shared in real time with the local branch of Pakistan’s paramilitary Frontier Corps. When his exact location was discovered, the Pakistanis smashed through the doors of his safe house and grabbed him along with two deputies.

Soon he was hundreds of miles away, at a special detention center in Punjab province, under intensive interrogation by a pro-U.S. faction of Pakistan’s Inter-Services Intelligence directorate. The Americans began getting regular reports on potential threats connected to the anniversary. CIA officials were even given an “unofficial” visit to question Mauritani directly.

Many in the U.S. government regarded the capture as a crowning achievement of a decade-long, multibillion-dollar effort to build a secret network of Pakistani security forces, intelligence operatives, counterterrorism fighters, and detention centers. Its objective had been to create a friendlier, more trustworthy alternative to Pakistan’s military and intelligence services.

Now, however, just three months after Mauritani’s capture, the partnership is facing its most dire challenge. Relations between the two countries have been rocked by back-to-back incidents. First came what the media are calling “memogate,” in which President Asif Ali Zardari’s administration is accused of plotting with the U.S. to replace the leadership of Pakistan’s military and intelligence services. And then, on Thanksgiving weekend, a NATO helicopter reported being fired upon by a Pakistani military outpost near the Afghanistan border. The chopper returned fire, killing two dozen Pakistani soldiers.

The reaction inside Pakistan has been white hot, and current and former U.S. intelligence officials tell The Daily Beast they worry the CIA’s alternate security network will be the ultimate casualty. If that happens, America could be left blind to future threats emanating from Pakistan, and the task of rounding up or killing high-value Qaeda remnants could become more difficult, if not impossible.

“We’ve been trying desperately for the last 10 years to build elements of Pakistani society and its national security bureaucracy to support U.S. counterterrorism efforts in the region,” says Rick “Ozzie” Nelson, a former manager at the National Counterterrorism Center. “This latest incident is a major test of that strategy.”

Former CIA director Mike Hayden says he has feared such an outcome for years as he watched U.S.-Pakistani relations drifting apart. “The space where American perceptions of strategic interests and Pakistani perceptions of strategic interests overlap has been diminishing,” he says.

In recent years, the relationship was kept afloat largely by the efforts of one man: Adm. Mike Mullen. Before his retirement as Joint Chiefs of Staff chairman this September, he maintained a personal friendship with his Pakistani counterpart, Gen. Ashfaq Parvez Kayani. At tense moments for the two countries, such as the arrest of CIA contractor Raymond Davis for killing two armed men in Lahore this past January, Mullen would be sent to smooth things over with Pakistan’s Army chief. One U.S. intelligence officer who works on Pakistan refers to Mullen as “the Kayani whisperer”: a man with a special knack for quietly and discreetly influencing Kayani at crucial points.

But the friendship soured in Mullen’s final days. The four-star admiral accused the ISI of supporting direct attacks on U.S. forces in Afghanistan by the Haqqani network, a deadly faction and support network for the Afghan Taliban, according to most accounts. The accusation left a shocked Kayani insisting to the Pakistani media that his old friend was simply misinformed. But things had been unraveling ever since the Davis shooting. The CIA contractor was one of numerous U.S. operatives who worked with elements of the U.S.-aligned shadow forces in Pakistan to target and apprehend terrorists—Pakistan, after all, was the country where bin Laden had been living unmolested for years. Before the shooting, current and recently retired U.S. intelligence officials say, the pro-American shadow network in Pakistan was capturing on average one Qaeda suspect a month. Still, those captures were seldom cleared through the chain of command of the ISI or the Pakistani military, and since the Davis incident the job has gotten much harder and riskier, U.S. officials say.

In the aftermath of the 9/11 attacks, America got valuable assistance from the military under Pakistan’s then-president, Gen. Pervez Musharraf. (It was Musharraf who handpicked Kayani as his replacement as Army chief shortly before stepping down from the presidency in August 2008.) Musharraf’s support enabled the Americans to bring a number of major Qaeda fugitives to justice, including Khalid Sheikh Mohammed, the self-described mastermind of the attacks on the World Trade Center and the Pentagon.

But Pakistan’s cooperation gradually petered out as Qaeda-instigated insurgencies erupted around the country, particularly in the Federally Administered Tribal Areas, the wild mountain region along the Afghan border. Hundreds of Pakistani soldiers were killed before Musharraf finally caved in and signed peace deals with FATA warlords in 2006 and 2007, effectively creating a sanctuary where al Qaeda’s leadership could regroup.

America’s current partnership with the Frontier Corps dates back to the summer of 2008, when U.S. special forces began frequent cross-border raids into the FATA. (Before 2008 such raids were rare.) Since then the corps has helped target senior Taliban and Qaeda leaders for drone strikes, in addition to helping capture senior Qaeda operatives such as Mauritani and providing security for the Shamsi drone base, the headquarters of the CIA’s Pakistan drone operations. This is risky work as well. On Sept. 8, two suicide bombers killed 23 people at the home of Farrukh Shahzad, the deputy commander of the Baluchistan Frontier Corps that captured Mauritani.

Within the ISI, America’s most reliable ally has been the spy service’s division known as the T Wing. It was created largely from scratch in 2006 and 2007, after the Americans mostly gave up trying to work with the ISI’s uncooperative leadership. U.S. officials say their hope was that the T Wing, which conducted Mauritani’s interrogation, might help to offset the pernicious influence of the ISI’s S Wing, the division in charge of managing the Pakistani government’s relationship with Islamic extremist groups such as the Kashmiri separatist Lashkar-e-Taiba and Afghanistan’s Taliban. According to the same officials, America also has embraced and funded units connected to Pakistan’s Interior Ministry, particularly in the corruption-ridden megalopolis of Karachi, where the local police are not considered reliable counterterrorism partners.

Over the past 10 years, Pakistan has received more than $20 billion in public U.S. funding for military and economic assistance. Washington’s secret subsidy of Pakistan’s intelligence and military could be much higher. Presidential candidate Michele Bachmann, a member of the House Intelligence Committee, called attention to the CIA’s extensive secret funding during a recent Republican debate. “The money that we are spending right now is primarily intelligence money to Pakistan,” she declared. “It is helping the United States. Whatever our action is, it must ultimately be about helping the United States and our sovereignty, our safety, and our security.”

That’s not as easy as it may sound. It’s been necessary to pick and choose which elements of Pakistan’s security apparatus America should engage with, says Mark Lowenthal, a former House Intelligence Committee staff director and former CIA assistant director for analysis. “We do this because of the nature of the Pakistani state,” he says. “If it was a coherent government, then when we made a deal with the president or the prime minister, you would know as the orders come down the line they would be obeyed.” Nevertheless, he says, “That is not the nature of Pakistan. You have all these competing power centers. We are not doing this because we are trying to be too clever by half, we are doing this because this is the nature of the state we are dealing with.”

The death of two dozen Pakistani soldiers has made that challenge tougher than ever. “As bad as the U.S.-Pakistan relationship is now, it’s only likely to get worse,” says Bruce Riedel, a former senior CIA official and one of the co-authors of President Obama’s initial Afghanistan-Pakistan strategy. So far, however, no one on either side knows what else to do but keep on.