Senate Democrats and Republicans reached agreement with the Bush administration yesterday on the terms of new legislation to control the federal government's domestic surveillance program, which includes a highly controversial grant of legal immunity to telecommunications companies that have assisted the program, according to congressional sources.

Disclosure of the deal followed a decision by House Democratic leaders to pull a competing version of the measure from the floor because they lacked the votes to prevail over Republican opponents and GOP parliamentary maneuvers.

The collapse marked the first time since Democrats took control of the chamber that a major bill was withdrawn from consideration before a scheduled vote. It was a victory for President Bush, whose aides lobbied heavily against the Democrats' bill, and an embarrassment for House Speaker Nancy Pelosi (D-Calif.), who had pushed for the measure's passage.

The draft Senate bill has the support of the intelligence committee's chairman, John D. Rockefeller IV (D-W.Va.), and Bush's director of national intelligence, Mike McConnell. It will include full immunity for those companies that can demonstrate to a court that they acted pursuant to a legal directive in helping the government with surveillance in the United States.

Such a demonstration, which the bill says could be made in secret, would wipe out a series of pending lawsuits alleging violations of privacy rights by telecommunications companies that provided telephone records, summaries of e-mail traffic and other information to the government after Sept. 11, 2001, without receiving court warrants. Bush had repeatedly threatened to veto any legislation that lacked this provision.

Senate Democrats successfully pressed for a requirement that the Foreign Intelligence Surveillance Court review the government's procedures for deciding who is to be the subject of warrantless surveillance. They also insisted that the legislation be renewed in six years, Democratic congressional officials said. The Bush administration had sought less stringent oversight by the court and wanted the law to be permanent.

The domestic surveillance issue has been awkward for Democrats since the administration's secret program of warrantless counterterrorism surveillance became public in late 2005. In August, a coalition of Republicans and dissident Democrats passed a measure backed by the White House that put that program on firm legal ground by expressly permitting the government to wiretap foreign targets without a court order, including, under certain circumstances, when those targets are communicating with people in the United States.

But Democratic leaders insisted that the law expire in February, so they could try again to impose more restrictions on the administration's ability to spy domestically. Most Democratic lawmakers and party members -- backed by civil libertarians and even some conservatives -- wanted the new legislation to ensure for example that future domestic surveillance in foreign-intelligence-related investigations would be overseen by the foreign surveillance court. The court was created in response to CIA and FBI domestic spying abuses unmasked in the mid-1970s.

But conservative Democrats worried about Republicans' charges that the Democratic bill extended too many rights to suspected terrorists. "There is absolutely no reason our intelligence officials should have to consult government lawyers before listening in to terrorist communications with the likes of Osama bin Laden, al-Qaeda and other foreign terror groups," said House Minority Leader John A. Boehner (R-Ohio).

The measure "extends our Constitution beyond American soil to our enemies who want to cut the heads off Americans," said Rep. Louie Gohmert (R-Tex.).

An adroit Republican parliamentary maneuver ultimately sank the bill. GOP leaders offered a motion that would have sent it back to the House intelligence and Judiciary committees with a requirement that they add language specifying that nothing in the measure would apply to surveilling the communications of bin Laden, al-Qaeda or other foreign terrorist organizations.

Approval of the motion would have restarted the legislative process, effectively killing the measure by delay. Democratic leaders scrambled to persuade their members to oppose it, but with Republicans accusing Democrats of being weak on terrorism, a "no" vote proved too hard to sell, and so the bill was pulled from the floor.