The Senate is poised to vote as early as next week on the USA Freedom Act, legislation that would end the National Security Agency’s bulk collection of records about Americans’ phone calls.

If passed, it may be voted on by the House and signed into law by year’s end, fulfilling one of President Obama’s major asks of Congress: that it adopt such legislation.

Passage would also clear the decks for incoming Senate Majority Leader Mitch McConnell (R-Ky.), who has an ambitious legislative agenda, including trade, taxes and building the long-delayed Keystone XL pipeline.

The bill, after months of tense negotiations, won the support of the intelligence community and civil liberties organizations — a rare consensus by groups that are normally at odds with one another.

Introduced in July, it stalled for months as advocates, including Sen. Patrick J. Leahy (D-Vt.), the Judiciary Committee chairman, sought to overcome concerns that the bill either hindered the intelligence community or did not provide adequate privacy protections.

If it becomes law, the bill would close a chapter in a vigorous, year-and-a-half-long debate over how far the government should go in gathering data on Americans to thwart terrorism and obtain foreign intelligence.

That debate began in June 2013, following a leak of a classified court order to The Guardian newspaper, which revealed that the NSA had been secretly collecting millions of Americans’ call detail records each day from U.S. phone companies for counterterrorism purposes. The agency gathers times, dates and durations of calls, but not their content.

The story was followed by a series of more disclosures about NSA surveillance activities, which were based on documents leaked by former NSA contractor Edward Snowden.

Snowden, who is living under temporary asylum in Moscow, said he shared the classified material with journalists, including at The Washington Post, to shine light on what he considered to be a system of mass surveillance that was growing unchecked.

“The American people are wondering whether Congress can get anything done,” Leahy said Wednesday, after Senate Majority Leader Harry Reid (D-Nev.) filed a procedural motion for the Senate to consider the bill. “The answer is yes. Congress can and should take up and pass the bipartisan USA Freedom Act, without delay.”

The bill requires the government to limit the scope of its data collection, and specifies that the government may not gather in bulk data relating to a particular phone or Internet company or to a broad geographic region, such as a city or an area designated by a zip code or area code.

It would end the current program in which the NSA gathers and stores the records of millions of Americans for five years. Instead the bill requires that the data be held by the phone companies.

It authorizes the use of Section 215 of the Patriot Act to obtain two “hops” of call detail records — or records of phone calls made by the people who were called by the target — as long as the government has reasonable, articulable suspicion that the target number belongs to a terrorist.

The bill also requires the Foreign Intelligence Surveillance Court, in consultation with the Privacy and Civil Liberties Oversight Board, to appoint a panel of special advocates to advance legal positions in support of privacy.