But the procedures that the F.B.I. initially proposed simply called for it to keep track of all queries of the repository, without distinguishing which sought information about Americans and which were focused on foreigners. Judge Boasberg rejected that idea as inadequate, and the appeals court upheld his ruling.

The judge also wanted F.B.I. agents, before reviewing any emails of Americans that turned up in a search, to document in writing how that search term — like an email address or a phone number — met the standard of being likely to return foreign intelligence information or criminal evidence. The F.B.I. initially resisted that idea, too, but after the appeals court said it had to rewrite its procedures to track queries better, it accepted the other new mandate, too.

The idea of requiring agents to document their rationales for searching for an American’s information emerged from several recent episodes in which the Justice Department reported to the court that the F.B.I. had conducted improperly sweeping searches of the repository. Those events formed the backdrop to the fight over the new rules for querying the surveillance repository.

Specifically, F.B.I. agents had carried out several large-scale searches for Americans who generically fit into broad categories — like they were F.B.I. employees or contractors — so long as agents had a reason to believe that someone within that category might have relevant information. But there has to be an individualized reason to search for any particular American’s information.

The documents’ description of episodes involving large-batch queries that violated the rules were heavily redacted, so it was difficult to discern what the agents had been trying to learn. The discussion of the F.B.I.’s search for tens of thousands of identifiers involving its own work force also said that the bureau’s general counsel had raised objections beforehand. (The results were never scrutinized, the ruling said.)

Judge Boasberg wrote that the searches involving large numbers of queries using Americans’ identifiers raised a “serious concern” that agents either did not understand the standard or were indifferent to it.

The ruling also mentioned two searches in December 2017 that were flagged as problematic — one involving a query for messages containing any of 6,800 Social Security numbers, and the other involving 1,600 queries whose details were censored. Several other episodes in February 2018 apparently involved F.B.I. efforts to identify Americans whom it could try to develop into potential confidential sources of information, the rulings showed.