PORTLAND, Ore. — Early morning clashes between federal agents and protesters intensified Thursday, testing the Trump administration's withdrawal plan and prompting the president to threaten to keep federal forces in the city "until there is safety."

What had been an evening of unity and triumph among protesters — with songs and dancing and people celebrating the departure of federal forces — quickly devolved late Wednesday into another chaotic night of violence and arrests, as federal agents unleashed several rounds of gas and a cloud of pepper spray on the crowd.

“I was literally just commenting that this is the way I wish all protests were when they blasted me,” said Adam Winther, 44, a Navy veteran who was hit in the leg with an exploding flash bang. “People were dancing, there was music, and all of a sudden the gas started going off above us.”

The contingent of more than 100 officers from various federal agencies including the Department of Homeland Security were to start leaving their positions in downtown Portland on Thursday, after an agreement between the White House and Oregon Gov. Kate Brown (D) that would replace them with Oregon State Police troopers.

It was unclear Thursday afternoon if that process had begun. Portland Mayor Ted Wheeler (D) said he was “cautiously optimistic” that federal tactical teams were “poised to leave” their post at the courthouse.

“Progressive cities around the country are standing together to defend their communities against hostile and dangerous interventions,” Wheeler said during a press briefing. “We’re all in agreement that since they arrived, things have only gotten much worse.”

The acting DHS secretary, Chad Wolf, had insisted this week that a federal footprint would remain in Portland until the Trump administration was convinced that Oregon authorities could handle the situation and that federal facilities “will no longer be attacked nightly and set afire.” President Trump on Thursday underscored that message, writing on Twitter that Brown “isn’t doing her job.”

“She must clear out, and in some cases arrest, the Anarchists & Agitators in Portland. If she can’t do it, the Federal Government will do it for her,” Trump tweeted. “We will not be leaving until there is safety.”

In tweets of her own, Brown responded that “we’ve had enough political grandstanding from DC. The President’s plan to ‘dominate’ the streets of American cities has failed.”

She added: “And today, federal troops are preparing to leave downtown Portland. We will protect free speech and the right to protest peacefully. The massive and non-violent protests led by Black Lives Matter activists have inspired the nation. Let’s get to work and make this vision a reality.”

Wednesday night began as most nights here do, with speeches ringing out across a gathered crowd from the steps of the Multnomah County Justice Center, which sits across the street from the courthouse and houses the county jail. Protesters lit up their phones and waved them back and forth, creating a glowing wave that moved in time with the chant “Black lives matter!”

Soon, the crowd began to migrate to stand in front of the Mark O. Hatfield U.S. Courthouse on what Brown has said would be federal agents’ last night protecting the building.

“We are not just fighting against the city, we are not just fighting against Donald Trump,” a speaker said into a microphone. “We are fighting against all of them!”

The weeks of confrontations have developed a regular choreo­graphy. The Federal Protective Service urges demonstrators away from the large reinforced iron fence that surrounds the building. Tear gas and crowd-control munitions are launched to push people back. Protesters respond with leaf blowers to push back the fumes, while some light fireworks or attempt to set fires around the building. Water bottles and other objects are thrown.

But on Wednesday night, newly erected floodlights shone down from the second floor of the courthouse onto the crowd. Just after 11 p.m., federal agents declared the gathering — still thick with moms and veterans and students and office workers — an unlawful assembly. Protesters were soon ducking and dodging tear-gas canisters flying through the air and skittering off the pavement. A federal officer with what appeared to be a fog gun walked up and down the fence line, spraying a caustic chemical at the people standing there.

Flash bangs and stun grenades exploded, sending deafening booms echoing through the city streets. A man with a trumpet played the cavalry charge tune as the pop of pellet guns and rubber bullets punctured the night.

“Stay together, stay tight,” demonstrators called out to each other, their voices muffled by gas masks and respirators.

Just before midnight, federal agents in three different uniforms surrounded the crowd on three sides. FPS vehicles blocked roadways, their red and blue lights flashing.

Protesters ran in a panic, shouting warnings to all who would listen.

“They’ve got us surrounded,” a young woman yelled.

But the officers did not move in. Instead, they stood at a stalemate with protesters on side streets as demonstrators yelled and chanted. Several agents pulled people from the crowd one by one, dragging those arrested into the back of the courthouse through an open metal gate.

Later in the night, several protesters appeared to have been injured. About 12:30 a.m., a man bleeding heavily from the shin was attended to by medics, who wiped down his wound and bandaged his leg. The man said he had been shot with a rubber bullet.

As they retreated, the federal agents fired more tear gas and pepper balls to cover their movements.

“I’ve been coming to this protest for many days. I’ve been standing and observing everything, seeing what’s going on and what’s not going on,” said Azeen Khan, a 31-year-old chef. “Tonight, there were not fireworks going off. Tonight, there was nothing thrown across the fence. Nothing. They just randomly came out and shot some people.”

Around dawn, Portland police swept into two downtown parks used as protest encampments and cleared them out. City workers removed tents and other debris, scrubbed away graffiti and hauled off wrought-iron benches.

Lownsdale Square and the adjacent Chapman Square had served as gathering points for over 60 days.

They were cleared at the request of the Oregon State Police as part of the plan to take over policing efforts from federal forces, according to Wheeler.

The state police will be sending about 100 officers from around the state to Portland to secure the federal courthouse, a group made up primarily of OSP’s special ops but also including command and logistical personnel. State police will be working in conjunction with federal and local authorities, including the Portland Police Bureau, state police communications director Mindy McCartt wrote in an email to The Washington Post.

The “marching orders” from State Police Superintendent Travis Hampton said that the agency is expected to “facilitate free speech to the best of their ability and present a refrained and proportional response to the observed criminal conduct,” McCartt said.

“Community support and communication is obviously imperative for OSP’s involvement to offer any chance at success,” she said.