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 warrantless eavesdropping program after the Sept. 11 attacks.

The deal, expanding the government’s powers in some key respects, would allow intelligence officials to use broad warrants to eavesdrop on foreign targets and conduct emergency wiretaps without court orders on American targets for a week if it is determined important national security information would be lost otherwise. If approved, as appears likely, it 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 determines that they received legitimate requests from the government directing their participation in the warrantless wiretapping operation.

With some AT&T and other telecommunications companies now 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.”