WASHINGTON  After months of wrangling, Democratic and Republican leaders in Congress struck a deal on Thursday to overhaul the rules on the government’s wiretapping powers and provide what amounts to legal immunity to the phone companies that took part in President Bush’s program of eavesdropping without warrants after the Sept. 11 attacks.

The deal, expanding the government’s powers to spy on terrorism suspects in some major respects, would strengthen the ability of intelligence officials to eavesdrop on foreign targets. It would also allow them to conduct emergency wiretaps without court orders on American targets for a week if it is determined that important national security information would otherwise be lost. If approved, as appears likely, the agreement would be the most significant revision of surveillance law in 30 years.

The agreement would settle one of the thorniest issues in dispute by providing immunity to the phone companies in the Sept. 11 program as long as a federal district court determined that they received legitimate requests from the government directing their participation in the program of wiretapping without warrants.

With AT&T and other telecommunications companies facing some 40 lawsuits over their reported participation in the wiretapping program, Republican leaders described this narrow court review on the immunity question as a mere “formality.”