The fact that a person was merely seen entering or leaving a building was not enough to permit the police to stop someone, “even if the building is located in a high-crime area, and regardless of the time of day,” the judge ruled. Nor was it enough for an officer to conduct a stop simply because the officer had observed the person move furtively, Judge Scheindlin said. (The forms that the police fill out after each street stop offer “furtive” movements as a basis for the stop.)

The police commissioner, Raymond W. Kelly, criticized the ruling, contending that the program, also known as Clean Halls, gave residents of the Bronx buildings “a modicum of safety for less prosperous tenants. Their landlords explicitly requested this extra level of protection.”

“Today’s decision unnecessarily interferes with the department’s efforts to use all of the crime-fighting tools necessary to keep Clean Halls buildings safe and secure,” he added.

Paul J. Browne, the department’s chief spokesman, said the program led to several recent arrests for illegal guns. On Dec. 16, the police arrested a man with a handgun on the rooftop of a residential building in the Bronx.

On Nov. 21, officers recovered a handgun after observing the gun’s butt protruding from a man’s jacket pocket as they patrolled a fourth-floor hallway in a building on East 220th Street.

Judge Scheindlin called for a hearing to discuss possible remedies to the issues she raised. At that hearing, she said, she will consider requiring the Police Department to create a formal written policy “specifying the limited circumstances in which it is legally permissible to stop a person outside a TAP building on a suspicion of trespass,” revise the training of officers and alter some of the training literature and videos used to teach officers how to conduct lawful stops.

The ruling followed a seven-day hearing in October during which nine black and Latino residents testified about being stopped while leaving their homes or visiting friends and relatives as guests. With testimony by plaintiffs and police witnesses, it was the first hearing of its type in any of the three stop-and-frisk cases before Judge Scheindlin, and the testimony evidently shaped her conclusions.