KABUL — The Taliban and the United States had just launched a seven-day agreement to reduce violence and possibly end America's longest war, but U.S. and Afghan officials wondered whether the Taliban would keep its end of the deal.

Then a rocket crashed into Bagram air base within the first hour of the “reduction in violence” period that began at midnight last Saturday. That was followed by dozens of other attacks across the country, including Taliban ambushes that killed at least six people in the northern province of Balkh.

Afghan officials said violence was down about 80 percent nationwide. But the attacks prompted Army Gen. Austin “Scott” Miller, the top U.S. commander in Afghanistan, to direct his staff to speak to Taliban leaders about restraining their fighters.

“The message that I passed is that if they can’t get that under control, we’re going to start shooting,” Miller said in a staff meeting, referring to the situation in Balkh. “That’s a very tough decision if we have the opportunity to end violence.”

A second rocket landed on Bagram on Tuesday, hitting a fire station. Miller said it was “pretty close to hitting coalition” members, something that “would be one of the red lines that is hard to come back from despite all the work.”

The behind-the-scenes moments, observed by The Washington Post, illustrate how U.S. military leaders are attempting to bring to a close the longest war in American history, ahead of the expected signing of a peace deal with the Taliban on Saturday.

For U.S. defense officials, the deal allows the United States to keep a foothold in Afghanistan depending upon the conditions on the ground, monitor whether the Taliban seeks peace and carry out strikes against the Islamic State militants, who are at war with the Taliban, too. It also could leave U.S. forces vulnerable to some casualties.

Some 2,400 U.S. service members have been killed in Afghanistan since 2001, and more than 20,000 wounded. The U.N. Assistance Mission in Afghanistan found that more than 100,000 civilians have been killed and wounded there in the past decade.

By early this week, U.S. military officials had grown confident that bloodshed with the Taliban could remain low through a signing deal. Notably, there were few attacks in provinces like Helmand, Paktika and Nangahar, all of which have been insurgency hotbeds, and civilians celebrated in the streets in some locations.

In Balkh, where attacks caught U.S. attention, it is believed that a Taliban commander was operating without the approval of his leaders, said one senior U.S. military official, speaking on the condition of anonymity because of the sensitivity of the issue. The commander needed to “pick up his game” and meet the terms of the seven-day deal, a message that was relayed to Taliban leaders overseeing him, the senior U.S. official said.

The violence reduction — something short of a full cease-fire — opened the door for the signing this Saturday of a historic peace deal that calls for the United States to withdraw several thousand of the roughly 12,000 service members there now within months and more as conditions on the ground allow.

On Friday afternoon in Washington, President Trump announced that Secretary of State Mike Pompeo will witness a signing of the agreement with the Taliban while Defense Secretary Mark T. Esper issues a joint declaration with the government of Afghanistan.

“If the Taliban and the government of Afghanistan live up to these commitments, we will have a powerful path forward to end the war in Afghanistan and bring our troops home,” Trump said. “These commitments represent an important step to a lasting peace in a new Afghanistan, free from Al Qaeda, ISIS and any other terrorist group that would seek to bring us harm.”

Trump was previously close to signing a deal with the Taliban in September at Camp David but canceled after the Taliban claimed responsibility for a suicide bombing in Kabul that killed a U.S. soldier, Sgt. 1st Class Elis Angel Barreto Ortiz, and about a dozen other people.

The agreement will be a first step on a road still rife with hazards. They include overcoming distrust on all sides, patching together a toxic political situation among senior Afghan government officials and dealing with irreconcilable Taliban members who may join the Islamic State out of disgust with the deal.

There is also concern that the Islamic State will step up its attacks. The group asserted responsibility for a bombing Thursday in Kabul in which at least nine people were wounded when explosives on a bicycle detonated.

Additional negotiations between the Afghan government and the Taliban could take months or years, or fall apart. The two sides will likely need to address such issues as how territory is controlled, what power the Taliban gets, the release of prisoners, and how to safeguard women’s rights and other protections gained since the fall of the Taliban in 2001.

This account of the U.S. military’s week in Afghanistan is based on several days of interviews with U.S. and Afghan officials, including Miller, as they grappled with a potential new era.

Miller, who rose to become a general during a career in some of the nation’s most secretive military units, said in a rare 40-minute interview Thursday that his discussion with Taliban leaders was “pragmatic and serious.” The group, he said, was serious about meeting its obligations and seeing the United States do the same.

“In terms of what I’ve seen, I’m satisfied that the Taliban made a good-faith effort,” Miller said, speaking in his office in Kabul. “The violence we have seen is sporadic. In some cases, it appears to be harassing, although there have been a couple of fatalities associated with violence — and those are things that we will talk to the Taliban about directly.”

The expectation, Miller said, is that the Taliban will continue to reduce its attacks. In one positive sign that U.S. officials observed, Taliban officials told their fighters to hold fire in a message that was distributed on WhatsApp.

Miller and his staff began reducing forces from about 14,000 last year and are expected to cut again from about 12,000 to 8,600 within months as part of the deal. The Taliban is likely to highlight that the agreement calls for the eventual departure of all U.S. forces, while U.S. and Afghan government officials will emphasize that any withdrawal will be conditions-based.

A spokesman for Ghani, Sediq Sediqqi, disputed that the deal between the United States and the Taliban is about the withdrawal of U.S. forces.

“It is going to be an opportunity for the Taliban to abandon violence, break ties with Al Qaeda/other terror groups and embrace a political settlement pursuing a lasting peace,” he tweeted.

In Washington, a group of Republican lawmakers sent a letter to Esper and Pompeo on Wednesday raising alarm about the plan.

The lawmakers included Afghanistan veterans Michael Waltz (Fla.) and Dan Crenshaw (Tex.) , who was wounded there while serving as a Navy SEAL. They highlighted the Taliban’s bloody history and questioned whether the militants will meet their obligations in the agreement.

“They will accept nothing less than a full-scale U.S. withdrawal from Afghanistan as they seek to establish their ‘Islamic Emirate,’ ” the lawmakers wrote. “Our withdrawal would then allow terrorist groups in Afghanistan to grow stronger and establish safe havens from which to plot attacks against us.”

Meanwhile, a disagreement among Afghan officials could threaten further negotiations.

An independent election council declared this month that Ghani had won his reelection bid. But his rival, Abdullah Abdullah, rejected the results and pledged to form a parallel government. Protests, mostly against Ghani, broke out across northern Afghanistan, where Abdullah is allied with Gen. Abdurrashid Dostum, a power broker and former vice president under Ghani.

U.S. officials expressed concern about the political crisis. Miller and special U.S. envoy Zalmay Khalilzad met with both parties, and privately with each other. Ghani eventually agreed to postpone his inauguration, which was to take place Thursday, for two weeks.

Miller, asked about the situation, said it will be “necessary” for Ghani and Abdullah to “resolve their political differences in a positive way as they move forward to the next step.” Doing so, Miller said, will allow them to have a “united front” as they negotiate with the Taliban.

Some U.S. officials have grumbled that the United States gave away too much to the Taliban in negotiations, but Miller applauded Khalilzad and his team for their work.

“We would not be where we are without him,” the general said. “We have from the very beginning been very intertwined.”

On Thursday, Miller and Khalilzad also met with senior Taliban officials in the Qatari city of Doha, where the Taliban maintains an office.

Amid the violence reduction, Miller visited with U.S. and Afghan troops in several locations.

At a base in Kabul that is home to U.S. and Afghan special operations forces, the general observed as Afghanistan’s acting defense minister, Asadullah Khalid, praised Afghan soldiers for their role in securing the Taliban’s commitment to reducing violence. He hoped for a full cease-fire later, he said.

Khalid said Afghan forces must remain apolitical as Abdullah and Ghani discuss their political futures.

On Wednesday, Miller and Khalid walked through the streets of downtown Kabul without helmets or body armor in an effort to demonstrate that the violence reduction was holding.

They greeted shopkeepers and passersby warmly, posing for selfies under the watchful eye of a small group of armed U.S. Special Operations personnel on Miller’s security team.

Miller traveled by helicopter to Camp Dahlke in the eastern province of Logar on Tuesday and met with Green Berets and conventional U.S. soldiers who advise Afghan forces.

In a room of bushy-bearded Green Berets, Miller asked: How many times have you deployed here?

One after another, the soldiers answered: Three, four, even seven times.

“In a perfect world, if all is working, we may have fired our last shots, at least against the Taliban,” Miller said. “Maybe — unless they attack us first. It’s the Taliban’s choice.”

Correction: An earlier version of this story incorrectly described an attack at Bagram air base last weekend. The fire station at the base was hit in another attack later  in the week.