WASHINGTON — President Trump’s puzzling tweets about a key U.S. spying law threw the House into temporary disarray Thursday, but lawmakers ended up renewing the law – with a new restriction on when the FBI can dig into the communications of Americans swept up in foreign surveillance.

During a hectic morning of House votes and presidential tweets, Trump’s national intelligence director also issued new guidance for how officials can find out the names of Americans whose identities are blacked out in classified intelligence reports.

Trump has said previous rules were far too lax and led to damaging leaks about top aides, a claim fiercely contested by Democrats.

The new guidelines on “unmasking” Americans, however, were a side show to the House showdown over the Foreign Intelligence Surveillance Act, reauthorizing a collection program due to expire on Jan. 19. The bill passed 256-164 and is now headed to the Senate. It would extend for six years the program, which includes massive monitoring of international communications. Trump has said he’ll sign the renewal, but his first tweets Thursday suggested he had suddenly turned against the program.

Trump then spoke by telephone with House Speaker Paul Ryan, according to a Republican familiar with the call.

A short time later, Trump changed his tune.

“This vote is about foreign surveillance of foreign bad guys on foreign land,” he tweeted. “We need it! Get smart!”